欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java利用ffmpeg合并多个视频文件

程序员文章站 2022-04-12 22:44:40
java利用ffmpeg合并多个视频文件1.首先要用到ffmpeg,可自行下载,本人用的是这个2.先把视频转成 ts文件3.再把ts文件合成/**** 参数:List fromVideoFileList 需要合并的多视频url地址以List存放String ffmpeg 此处是ffmpeg 配置地址,可写死如“E:/ffmpeg/bin/ffmpeg.exe”String NewfilePath 合并后的视频存放地址,如:E:/mergevideo.mp4*/public class...

java利用ffmpeg合并多个视频文件
1.首先要用到ffmpeg,可自行下载,本人用的是这个:https://pan.baidu.com/s/1_eauFOaO7rx9zSzGkEQ-3w
提取码:6sg3
复制这段内容后打开百度网盘手机App,操作更方便哦
2.先把视频转成 ts文件
3.再把ts文件合并

		//工具类
		/**
		** 参数:
		*	**List<String> fromVideoFileList 需要合并的多视频url地址以List存放**
		*	**String ffmpeg 此处是ffmpeg 配置地址,可写死如“E:/ffmpeg/bin/ffmpeg.exe”**
		*	**String NewfilePath 合并后的视频存放地址,如:E:/mergevideo.mp4***
		*/
		public class ZipCompressor {
			public static void convetor(List<String> fromVideoFileList, String ffmpeg,
					String NewfilePath) throws IOException {
				new Thread(
						() -> {
							try {
								List<String> voidTS = new ArrayList<>();
								Process process = null;
								ProcessBuilder builder = null;
								List<String> command = null;
								for (int i = 0; i < fromVideoFileList.size(); i++) {
									String fromVideoFile = fromVideoFileList.get(i);
									command = new ArrayList<String>();
									command.add(ffmpeg);
									command.add("-y");
									command.add("-i");
									command.add(fromVideoFile);
									command.add("-vcodec");
									command.add("copy");
									command.add("-bsf:v");
									command.add("h264_mp4toannexb");
									command.add("-f");
									command.add("mpegts");
									command.add(fromVideoFile.substring(0,
											fromVideoFile.lastIndexOf(".")) + ".ts");
									builder = new ProcessBuilder(command);
									voidTS.add(fromVideoFile.substring(0,
											fromVideoFile.lastIndexOf("."))
											+ ".ts");
									try {
										process = builder.start();
										InputStream errorStream = process
												.getErrorStream();
										InputStreamReader inputStreamReader = new InputStreamReader(
												errorStream);
										BufferedReader br = new BufferedReader(
												inputStreamReader);
										String line = "";
										StringBuffer sb = new StringBuffer();
										while ((line = br.readLine()) != null) {
											sb.append(line);
										}
										String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
										Pattern pattern = Pattern
												.compile(regexDuration);
										Matcher m = pattern.matcher(sb.toString());
										System.out.println(sb.toString());
										br.close();
										inputStreamReader.close();
										errorStream.close();
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
								List<String> dos = new ArrayList<>();
								StringBuffer tsPath = new StringBuffer();
								tsPath.append(ffmpeg);
								tsPath.append(" -i ");
								tsPath.append("concat:");
								for (int t = 0; t < voidTS.size(); t++) {
									if (t != voidTS.size() - 1) {
										tsPath.append(voidTS.get(t) + "|");
									} else {
										tsPath.append(voidTS.get(t));
									}
								}
								tsPath.append(" -vcodec ");
								tsPath.append(" copy ");
								tsPath.append(" -bsf:a ");
								tsPath.append(" aac_adtstoasc ");
								tsPath.append(" -movflags ");
								tsPath.append(" +faststart ");
								tsPath.append(NewfilePath);
								Process pr = Runtime.getRuntime().exec(
										tsPath.toString());
								process.getInputStream();
								pr.getOutputStream().close();
								pr.getInputStream().close();
								pr.getErrorStream().close();
								try {
									pr.waitFor();
									Thread.sleep(1000);
									pr.destroy();
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								//删除生成的ts文件
								for (String filePath : voidTS) {
									File file = new File(filePath);
									file.delete();
									pr.destroy();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}).start();
			}
		}
				
				//测试类
				public class test {
					public static void main(String[] args) throws IOException {
						String ffmpeg="E:/ffmpeg/bin/ffmpeg.exe";
						String NewfilePath  = "E:/mergevideo.mp4"; 
						fromVideoFileList.add("E:/video/cg2.mp4");
						fromVideoFileList.add("E:/video/cg1.mp4");
						ZipCompressor.convetor(fromVideoFileList, ffmpeg,NewfilePath );
					}
				}

本文地址:https://blog.csdn.net/qq_45443327/article/details/109626746

相关标签: ffmpeg java