FFmpeg Invalid data found when processing input
生活随笔
收集整理的這篇文章主要介紹了
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;}@Overridepublic 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();}}}}總結
以上是生活随笔為你收集整理的FFmpeg Invalid data found when processing input的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Manifest merger fail
- 下一篇: sa结构组网方式_5g组网SA和NSA的