java利用ffmpeg合併多個視頻文件

java利用ffmpeg合併多個視頻文件
1.首先要用到ffmpeg,可自行下載,本人用的是這個:https://pan.baidu.com/s/1_eauFOaO7rx9zSzGkEQ-3w
提取碼:6sg3
複製這段內容後打開百度網盤手機App,操做更方便哦
2.先把視頻轉成 ts文件
3.再把ts文件合併




java

//工具類
		/** ** 參數: * **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 );
					}
				}
相關文章
相關標籤/搜索