您好,欢迎来到微智科技网。
搜索
您的当前位置:首页java利用ffmpeg追加合并视频文件

java利用ffmpeg追加合并视频文件

来源:微智科技网

java利用ffmpeg追加合并视频文件

	/**
     * 合并视频文件
     * @param videoPathList
     */
    public void mergeVideo(List<String> videoPathList) {

        if (videoPathList.size() > 1){

            String ffmpegPath = CaptureConfig.getFfmpegPath();// 此处是配置地址,可自行写死如“D:\\ffmpeg\\ffmpeg.exe”
            String txtPath = "";
            try {
                int index = videoPathList.get(0).lastIndexOf(".");
                String newMergePath = videoPathList.get(0).substring(0, index) + "new" + videoPathList.get(0).substring(index, videoPathList.get(0).length());
                txtPath = videoPathList.get(0).substring(0, index) + ".txt";
                FileOutputStream fos = new FileOutputStream(new File(txtPath));
                for (String path : videoPathList){
                    fos.write(("file '" + path + "'\r\n").getBytes());
                }
                fos.close();

                StringBuffer command = new StringBuffer("");
                command.append(ffmpegPath);
                command.append(" -f");
                command.append(" concat");
                command.append(" -safe");
                command.append(" 0");
                command.append(" -i ");
                command.append(txtPath);
                command.append(" -c");
                command.append(" copy ");// -c copy 避免解码,提高处理速度
                command.append(newMergePath);

                start(command.toString());

                // 删除产生的临时用来存放每个视频文件路径的txt文件
                File txtFile = new File(txtPath);
                txtFile.delete();

                // 删除原视频文件
                for (String filePath : videoPathList){
                    File file = new File(filePath);
                    file.delete();
                }

                // 合成的新视频文件重命名为原视频文件的第一个文件名
                File newFile = new File(newMergePath);
                File oldFile = new File(videoPathList.get(0));
                newFile.renameTo(oldFile);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void start(String command){
        try {
            final Process process = Runtime.getRuntime().exec(command);
            LOGGER.info("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("output:" + 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("err:" + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            err.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

            // 等待命令子线程执行完成
            process.waitFor();
            process.destroy();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  1. 测试类
public static void main(String[] args) {

        long begin = System.currentTimeMillis();

        FileOperateServiceImpl impl = new FileOperateServiceImpl();
        // 合并视频文件
       List videoPathList = new ArrayList<>();
       videoPathList.add("E:\\111.flv");
       videoPathList.add("E:\\222.flv");
       videoPathList.add("E:\\333.flv");
       impl.mergeVideo(videoPathList);

        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务