FFmpeg Invalid data found when processing input

说一下自己用ffmpeg合并视频出现的bug吧   直接上代码

/**
 * 批量转换某文件夹的视频   转换为ts文件 并生成txt文件
 * */


String videoPathList = "C:\\Users\\Administrator\\Desktop\\video\\";
File file = new File(videoPathList);

String txt = "C:\\Users\\Administrator\\Desktop\\video\\test.txt";
FileWriter fw = new FileWriter(new File(txt));
BufferedWriter bw = new BufferedWriter(fw);

if (null != file) {
    File[] fileList = file.listFiles();
    if (null != fileList && fileList.length > 0) {
        for (int i = 0; i < fileList.length; i++) {
            if (StrUtil.endWith(fileList[i].getName(), ".mp4")) {
                System.out.println(fileList[i].getName());
                String sourcePath = videoPathList + fileList[i].getName();
                String targetPath = videoPathList + "ts\\" + StrUtil.removeSuffix(fileList[i].getName(), ".mp4") + i + ".ts";
                if (mp4ToTs(sourcePath, targetPath)) {
                    bw.write("file " + "'" + targetPath + "'" + "\t\n");
                } else {
                    System.out.println("失败");
                    break;
                }
                System.out.println("成功");
            }
        }



        if (mp4Merge(txt, videoPathList + "ts\\aa.mp4")) {
            System.out.println("完成");
        } else {
            System.out.println("失败");
        }


    }
}

bw.close();
fw.close();

这段代码运行会直接报 Invalid data found when processing input  然后我去寻找这个文件的时候发现当中是有内容的  并且也真实存在这个文件  这时候我就有点疑惑为什么会报文件没有的异常  直到debug的时候我打开了这个txt文件  我才发现文件那时并没有真正的写入进去  只有close以后txt文件才算完成  但是在txt文件完成之前我就已经去合成了以后  抛出这个异常  

稍微修改一下就能正常的合成了

/**
 * 批量转换某文件夹的视频   转换为ts文件 并生成txt文件
 * */


String videoPathList = "C:\\Users\\Administrator\\Desktop\\video\\";
File file = new File(videoPathList);

String txt = "C:\\Users\\Administrator\\Desktop\\video\\test.txt";
FileWriter fw = new FileWriter(new File(txt));
BufferedWriter bw = new BufferedWriter(fw);

if (null != file) {
    File[] fileList = file.listFiles();
    if (null != fileList && fileList.length > 0) {
        for (int i = 0; i < fileList.length; i++) {
            if (StrUtil.endWith(fileList[i].getName(), ".mp4")) {
                System.out.println(fileList[i].getName());
                String sourcePath = videoPathList + fileList[i].getName();
                String targetPath = videoPathList + "ts\\" + StrUtil.removeSuffix(fileList[i].getName(), ".mp4") + i + ".ts";
                if (mp4ToTs(sourcePath, targetPath)) {
                    bw.write("file " + "'" + targetPath + "'" + "\t\n");
                } else {
                    System.out.println("失败");
                    break;
                }
                System.out.println("成功");
            }
        }
bw.close(); 
fw.close();



        if (mp4Merge(txt, videoPathList + "ts\\aa.mp4")) {
            System.out.println("完成");
        } else {
            System.out.println("失败");
        }


    }
}


顺便把合并类贴一下



public class Trans2 {

    static String ffmpegPath = "G:\\ffmpeg-4.4-essentials_build\\bin\\ffmpeg.exe";


    /**
     * 视频合成
     */

    public static boolean mp4Merge(String TxtPath, String targetPath) {
        List<String> cutpic = new LinkedList<>();
        cutpic.add(ffmpegPath);
        cutpic.add("-f");
        cutpic.add("concat");
        cutpic.add("-safe");
        cutpic.add("0");
        cutpic.add("-i");
        cutpic.add(TxtPath);

        cutpic.add("-c");
        cutpic.add("copy");
        cutpic.add("-y");
        cutpic.add(targetPath);


        System.out.println(cutpic.toString());
        return getMp4(cutpic, targetPath);
    }

    /**
     * mp4转换为ts
     */
    public static boolean mp4ToTs(String sourcePath, String targetPath) {
        // 创建一个List集合来保存命令
        List<String> cutpic = new LinkedList<>();
        cutpic.add(ffmpegPath);
        cutpic.add("-i");
        cutpic.add(sourcePath);
        cutpic.add("-y");

        cutpic.add("-c");
        cutpic.add("copy");//使用默认参数   增加转换速度
        cutpic.add("-acodec");
        cutpic.add("copy");
        cutpic.add(targetPath);
        return getMp4(cutpic, targetPath);
    }




    /**
     * ffmpeg运行cmd命令窗口生成mp4方法
     */
    private static boolean getMp4(List<String> cutpic, String imageGenerationPath) {
        try {

            Process process = new ProcessBuilder(cutpic).start();

            // 消耗缓冲区中的错误流
            new PrintStream(process.getErrorStream()).start();
            // 消耗缓冲区中的输入流
            new PrintStream(process.getInputStream()).start();

            process.waitFor();
            if (process.exitValue() != 0) {
                System.out.println("发生了错误==============错误为" + process.exitValue() + "::::::::::::::::::错误文件为" + imageGenerationPath);
                return false;
            }
            System.out.println(("生成完成,位置为:" + imageGenerationPath));

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 内部类继承线程清理缓冲区
     */
    private static class PrintStream extends Thread {
        private InputStream stream;

        StringBuffer contentNum = new StringBuffer();

        public PrintStream(InputStream stream) {
            this.stream = stream;
        }

        @Override
        public void run() {
            try {
                while (null != stream) {
                    int content = stream.read();
                    if (content != -1) {
                        contentNum.append((char) content);
                    } else {
                        break;
                    }
                }
                System.out.println("FFmpeg  日志返回为" + contentNum.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值