音频处理工具ffmpeg的简单使用
一、去官网下载
传送门:http://ffmpeg.org
二、使用ffmpeg将视频.mp4转成音频.avi
三、截取视频的封面
public void getCover(String videoInputPath, String coverOutputPath) throws IOException, InterruptedException { // ffmpeg.exe -ss 00:00:01 -i spring.mp4 -vframes 1 bb.jpg List<String> command = new java.util.ArrayList<String>(); command.add(ffmpegEXE); // 指定截取第1秒 command.add("-ss"); command.add("00:00:01"); command.add("-y"); command.add("-i"); command.add(videoInputPath); command.add("-vframes"); command.add("1"); command.add(coverOutputPath); for (String c : command) { System.out.print(c + " "); } ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); InputStream errorStream = process.getErrorStream(); InputStreamReader inputStreamReader = new InputStreamReader(errorStream); BufferedReader br = new BufferedReader(inputStreamReader); String line = ""; while ( (line = br.readLine()) != null ) { } if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } }
四、将视频和音乐合并在一起,即为视频选一首背景音乐
public void convertor(String videoInputPath, String mp3InputPath, double seconds, String videoOutputPath) throws IOException { //ffmpeg.exe -i dormitory.mp4 -i bgm.mp3 -t 6 -y 新的视频.mp4 List<String> command = new ArrayList<String>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add("-i"); command.add(mp3InputPath); command.add("-t"); command.add(String.valueOf(seconds)); // command.add("-strict"); //linux环境下如果截图需要添加下面两个参数 // command.add("-2"); command.add("-y"); command.add(videoOutputPath); ProcessBuilder processBuilder = new ProcessBuilder(command); Process process = processBuilder.start(); //如果内存中太多流,会把当前线程卡住,需要清理 InputStream errorStream = process.getErrorStream(); InputStreamReader inputStreamReader = new InputStreamReader(errorStream); BufferedReader br = new BufferedReader(inputStreamReader); String line = ""; while((line=br.readLine()) != null){ } if(br != null){ br.close(); } if(inputStreamReader != null){ inputStreamReader.close(); } if(errorStream != null){ errorStream.close(); } }
相关推荐
dingwun 2020-11-16
wangdaren 2020-08-15
wqiaofujiang 2020-07-05
PGzxc 2020-07-04
ShoppingChen 2020-06-25
cherayliu 2020-06-17
83096129 2020-06-08
83096129 2020-06-08
PGzxc 2020-06-01
ShoppingChen 2020-05-29
cherayliu 2020-05-11
ShoppingChen 2020-05-11
83096129 2020-05-10
PGzxc 2020-05-07
cherayliu 2020-04-27
cherayliu 2020-04-11