FFMPEG 两个视频合并

参考 添加链接描述
注意两个视频的格式要一样
FFMPEG 两个视频合并_第1张图片

package com.app;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
 
public class Convert {
    private static final String ffmpegPath = "D:\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe";
    private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    public static void main(String[] args) {
        M4();
    }
 
    private static void M4() {
        List list = new ArrayList<>();
        list.add("D:\\v\\2.mp4");
        list.add("D:\\v\\1.mp4");
        String outputDir = "D:/v/";
        String output = "D:/v/merged.mp4";
        mergeVideo(list, outputDir, output);
    }
 
    public static String mergeVideo(List<String> list, String outputDir, String outputFile) {
        try {
            String format1 = "%s -i %s -c copy -bsf:v h264_mp4toannexb -f mpegts %s";
            String command1 = String.format(format1, ffmpegPath, list.get(0), outputDir + "input1.ts");
            String command2 = String.format(format1, ffmpegPath, list.get(1), outputDir + "input2.ts");
 
            String format3 = "%s -i \"concat:%s|%s\"  -acodec copy -vcodec copy -absf aac_adtstoasc -movflags +faststart %s";
            String command3 = String.format(format3, ffmpegPath, outputDir + "input1.ts", outputDir + "input2.ts", outputFile);
 
            if (execCommand(command1) > 0 && execCommand(command2) > 0 && execCommand(command3) > 0) {
                File file1 = new File(outputDir + "input1.ts");
                File file2 = new File(outputDir + "input2.ts");
                file1.delete();
                file2.delete();
              
                return "1";
            } else {
                return "0";
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-----合并失败!!!!!!" + outputFile);
            return "0";
        }
    }
 
    private static Integer execCommand(String command) {
        try {
            Process process = Runtime.getRuntime().exec(command);
            //获取进程的标准输入流
            final InputStream is1 = process.getInputStream();
            //获取进城的错误流
            final InputStream is2 = process.getErrorStream();
            //启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
            readInputStream(is1);
            readInputStream(is2);
            process.waitFor();
            process.destroy();
            System.out.println("-----操作成功" + command + " " + sdf1.format(new Date()));
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-----操作失败" + command);
            return -1;
        }
    }
 
    private static void readInputStream(InputStream inputStream) {
    
    	new Task(inputStream).run();
      
    }
}


class Task extends Thread{
	public InputStream is;
	public Task(InputStream inputStream) {
		this.is = inputStream;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		//super.run();
		BufferedReader br1 = new BufferedReader(new InputStreamReader(this.is));
        try {
            String line1 = null;
            while ((line1 = br1.readLine()) != null) {
                if (line1 != null) {
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
            	this.is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
}

上面的运行起来卡顿

package com.app;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

import com.google.common.io.Files;

public class Convert {
	private static final String ffmpegPath = "D:\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe";
	private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) {
		M4();
	}

	private static void M4() {
		List list = new ArrayList<>();
		list.add("D:\\v\\2.mp4");
		list.add("D:\\v\\1.mp4");
		String outputDir = "D:/v/";
		String output = "D:/v/merged.mp4";
		// mergeVideo(list, outputDir, output);
	}

	public static String mergeVideo(List<String> list, String outputDir, String outputFile, String endFileName) {
		try {
			System.out.println("outputDir:" + outputDir);
			System.out.println("outputFile:" + outputFile);
			for (String file : list) {
				System.out.println("f:" + file);
			}

			String format1 = "%s -i %s -vcodec copy -bsf:v h264_mp4toannexb -f mpegts %s";
			String command1 = String.format(format1, ffmpegPath, list.get(0), outputDir + "input1.ts");
			String command2 = String.format(format1, ffmpegPath, list.get(1), outputDir + "input2.ts");

			String format3 = "%s -i \"concat:%s|%s\"  -vcodec copy  -absf aac_adtstoasc -movflags  +faststart %s";
			String command3 = String.format(format3, ffmpegPath, outputDir + "input1.ts", outputDir + "input2.ts",
					outputFile);

			if (execCommand(command1) > 0 && execCommand(command2) > 0 && execCommand(command3) > 0) {
				File file1 = new File(outputDir + "input1.ts");
				File file2 = new File(outputDir + "input2.ts");
				file1.delete();
				file2.delete();
				Files.copy(new File(outputFile), new File(endFileName));

				return "1";
			} else {
				System.out.println();
				return "0";
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("-----合并失败!!!!!!" + outputFile);
			return "0";
		}
	}

	public static Integer execCommand(String command) {
		System.out.println("cmd:" + command);
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(command);
			// 获取进程的标准输入流
			final InputStream is1 = process.getInputStream();
			// 获取进城的错误流
			final InputStream is2 = process.getErrorStream();
			// 启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
			// readInputStream(is1);
			// readInputStream(is2);

			new Thread() {
				public void run() {
					BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
					try {
						String line1 = null;
						while ((line1 = br1.readLine()) != null) {
							if (line1 != null) {
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							is1.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();

			new Thread() {
				public void run() {
					BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
					try {
						String line2 = null;
						while ((line2 = br2.readLine()) != null) {
							if (line2 != null) {
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							is2.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();

			process.waitFor();
			process.destroy();
			System.out.println("-----操作成功" + command + " " + sdf1.format(new Date()));
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("-----操作失败" + command);

			return -1;
		}
	}

	private static void readInputStream(InputStream inputStream) {

		// new Task(inputStream).run();

	}
}

class Task extends Thread {
	public InputStream is;

	public Task(InputStream inputStream) {
		this.is = inputStream;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		// super.run();
		System.out.println(Thread.currentThread().getName());
		BufferedReader br1 = new BufferedReader(new InputStreamReader(this.is));
		try {
			String line1 = null;
			while ((line1 = br1.readLine()) != null) {
				if (line1 != null) {
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				this.is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

你可能感兴趣的:(ffmpeg,音视频,毕业设计,计算机毕业设计,java,课程设计)