ffmpeg 多个视频合成一个视频

ffmpeg 多个视频合成一个视频


    /**
     * 合成视频
     *
     * @param out_path 输出视频地址
     * @param list     源视频地址列表
     * @return
     */
    public static String mergeVideo(String out_path, List<String> list) {
        try {
            String ffmpegPath = getPath();
            List<String> txt = new ArrayList<>();
            //将mp4转为ts
            for (int i = 0; i < list.size(); i++) {
                StringBuilder builder = new StringBuilder();
                builder.append(ffmpegPath);
                builder.append(" -i ");
                builder.append(list.get(i) + " ");
                builder.append("-vcodec copy ");
                builder.append("-acodec copy ");
                builder.append("-vbsf h264_mp4toannexb ");
                int index = list.get(i).lastIndexOf(".");
                String ts_path = list.get(i).substring(0, index) + ".ts";
                txt.add(ts_path);
                if (FileHandleUtil.isExists(ts_path)) {
                    FileHandleUtil.delete(ts_path);
                }
                builder.append(ts_path);
                start(builder.toString());
            }

            //生成txt文件
            int index = list.get(0).lastIndexOf(".");
            String txtPath = list.get(0).substring(0, index) + ".txt";
            FileOutputStream fos = new FileOutputStream(new File(txtPath));
            for (String path : txt) {
                fos.write(("file '" + path + "'\r\n").getBytes());
            }
            fos.close();

            //合成视频
            StringBuilder builder = new StringBuilder();
            builder.append(ffmpegPath);
            builder.append(" -f concat -safe 0 ");
            builder.append("-i ");
            builder.append(txtPath + " ");
            builder.append("-acodec copy ");
            builder.append("-vcodec copy ");
            builder.append("-absf aac_adtstoasc ");
            builder.append(out_path);
            VideoUtil.start(builder.toString());
            //删除txt文件
            FileHandleUtil.delete(txtPath);
            //删除ts文件
            for (String path : txt) {
                FileHandleUtil.delete(path);
            }
            return out_path;
        } catch (Exception e) {
            logger.error("合成视频异常 {}", e);
        }
        return "";
    }

    /**
     * 执行ffmpeg命令
     *
     * @param command 命令
     */
    public static void start(String command) {
        try {
            final Process process = Runtime.getRuntime().exec(command);
            System.out.println(StrUtil.format("start run cmd {}", command));
            //⚠️此处代码是因为如果合并大视频文件会产生大量的日志缓存导致线程阻塞,最终合并失败,所以加两个线程处理日志的缓存,之后再调用waitFor方法,等待执行结果。
            new Thread() {
                @Override
                public void run() {
                    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = null;
                    try {
                        while ((line = in.readLine()) != null) {
                            System.out.println(System.currentTimeMillis() + ":" + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            new Thread() {
                @Override
                public void run() {
                    BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    String line = null;
                    try {
                        while ((line = err.readLine()) != null) {
                            System.out.println(System.currentTimeMillis() + ":" + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            err.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            // 等待命令子线程执行完成
            process.waitFor();
            process.destroy();
        } catch (IOException e) {
            logger.error("执行命令异常 {}", e);
        } catch (InterruptedException e) {
            logger.error("执行命令异常 {}", e);
        }
    }

经实践得经验!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值