Java使用ffmpeg对音频和视频进行合并。
需求:将多个avi格式视频跟一个mp3文件进行合并为mp4格式的视频。
步骤:
- 将多个avi合并为一个;
- 将合并后的avi跟mp3合并为一个avi;
- 将avi转换为mp4格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| /** * @author:四个空格-https://www.4spaces.org * 合并多个Avi文件 * @param ffmpegPath 合并工具路径 * @param txtPath 待合并的文件每行一个保存的txt文件里 * @param outPath 合并后的avi保存路径 */ public void mergeAvis(String ffmpegPath,String txtPath,String outPath){ List<String> commend = new ArrayList<String>(); commend.add(ffmpegPath); commend.add("-f");//合成音视频 commend.add("concat"); commend.add("-i"); commend.add(outPath); commend.add("-c"); commend.add("copy"); commend.add(outPath); try { ProcessBuilder builder = new ProcessBuilder(commend); builder.command(commend); Process p = builder.start(); doWaitFor(p); p.destroy(); } catch (Exception e) { e.printStackTrace(); } }
/** * 将avi格式的视频转换为mp4格式 * @param ffmpegPath 合并工具路径 * @param aviPath 待转换的avi路径 * @param mp4Path 转换后的mp4保存路径 */ public void changeAviToMp4(String ffmpegPath,String aviPath,String mp4Path){ List<String> commend = new ArrayList<String>(); commend.add(ffmpegPath); commend.add("-i"); commend.add(aviPath); commend.add(mp4Path); try { ProcessBuilder builder = new ProcessBuilder(commend); builder.command(commend); Process p = builder.start(); doWaitFor(p); p.destroy(); } catch (Exception e) { e.printStackTrace(); } }
/** * 实现视频(avi)和音频(mp3)的合并 * @param ffmpegPath 合并工具路径 * @param mp3Path 需要合并的mp3路径 * @param aviPath 需要合并的avi路径 * @param outPath 合并后的avi保存路径 */ public void mergeVideoAndAudio(String ffmpegPath,String mp3Path,String aviPath,String outPath){ List<String> commend = new ArrayList<String>(); commend.add(ffmpegPath); commend.add("-i"); commend.add(mp3Path); commend.add("-i"); commend.add(aviPath); commend.add("-acodec"); commend.add("copy"); commend.add("-vcodec"); commend.add("copy"); commend.add(outPath); try { ProcessBuilder builder = new ProcessBuilder(commend); builder.command(commend); Process p = builder.start(); doWaitFor(p); p.destroy(); } catch (Exception e) { e.printStackTrace(); } }
public void createAviTextFile(String textFilePath) { // 首先判断是否存在,不存在则创建 File file = new File(textFilePath); if (!file.exists()) { try { if (file.createNewFile()) { System.out.println("创建单个文件" + textFilePath + "成功!"); } else { System.out.println("创建单个文件" + textFilePath + "失败!"); } } catch (IOException e) { e.printStackTrace(); } } }
/** * 将要合并的avi写入txt文件(格式:file '/path/to/yourfile/video-00001.ts'),单个写入,每行一个 * @param textFilePath */ public void writeAviToTextFile(String textFilePath,String content) { try { File writename = new File(textFilePath); writename.createNewFile(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(writename, true))); out.write(content+"\r\n"); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
|
待合并视频文件示例txt
1 2 3 4 5
| file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00001.ts' file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00002.ts' file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00003.ts' file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00004.ts' file 'D:\ffmpeg-20190225-f948082-win64-static\bin\qifeng\video-00005.ts'
|
评论系统未开启,无法评论!