java使用ffmpeg进行多个视频合并

1、导言

参考 使用ffmpeg合并视频文件的三种方法

项目需要使用FFmpeg进行MP4视频合并

2、代码

package com;

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

public class aaa {
    private static final String ffmpegPath = "D:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe";
    private static final SimpleDateFormat sdf = 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:\\VideoRec\\he1.mp4");
        list.add("D:\\VideoRec\\he2.mp4");
        String outputDir = "D:/VideoRec/";
        String output = "D:/VideoRec/he3.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\" -c copy -bsf:a 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 + " " + sdf.format(new Date()));
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-----操作失败" + command);
            return -1;
        }
    }

    private static void readInputStream(InputStream inputStream) {
        new Thread(() -> {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line1 = null;
                while ((line1 = br1.readLine()) != null) {
                    if (line1 != null) {
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
posted @ 2021-02-14 17:53  一只桔子2233  阅读(2217)  评论(0编辑  收藏  举报