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

java利用ffmpeg合并多个视频文件
1.首先要用到ffmpeg,可自行下载。本人用的是这个:https://pan.baidu.com/s/1_eauFOaO7rx9zSzGkEQ-3w
提取码:6sg3
2.先把视频转成 ts文件
3.再把ts文件合并

//测试类
				public class test {
					public static void main(String[] args) throws IOException {
						String ffmpeg="E:/ffmpeg/bin/ffmpeg.exe";
						String NewfilePath  = "E:/mergevideo.mp4"; 
						List fromVideoFileList=new ArrayList();
						fromVideoFileList.add("E:/video/cg2.mp4");
						fromVideoFileList.add("E:/video/cg1.mp4");
						ZipCompressor.convetor(fromVideoFileList,        ffmpeg,NewfilePath );   
					}
				}
	//工具类
	/**
	** 参数:
	*	**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();
		}
	}

代码写之不易,麻烦兄弟们支持一下! 您的支持是我最大的动力!
在这里插入图片描述

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值