生活随笔
收集整理的這篇文章主要介紹了
java 通过ffmpeg 将海康视频转码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
保存海康的視頻成MP4格式的視頻,最后無法在網頁上播放,或者干脆本地無法打開;
既然使用ffmpeg作為視頻轉碼工具,首先需要有這個;
idea下FFmpeg存放的位置,或者可以自定將其配置到環境變量中,我這邊是為了防止在其他地方部署每次都要配置這個環境變量,所以直接放在了資源文件夾下面;
public class VideoUtils {private static Logger log
= Logger.getLogger(VideoUtils.class);private static String ffmpegPath
= null;public static String getFFPath() {if (ffmpegPath
!= null) {return ffmpegPath
;}ResourcePatternResolver resolver
= new PathMatchingResourcePatternResolver();try {Resource resource
= resolver
.getResource("ffmpeg/ffmpeg.bat");InputStream stream
= resource
.getInputStream();String targetFilePath
= resource
.getFile().getParent();return targetFilePath
;} catch (IOException e
) {return null;}}public static boolean processMP4(String inputPath
, String outputPath
) {long start
= System.currentTimeMillis();log
.info("接收到視頻文件,開始對文件進行轉碼...");if(StringUtils.isBlank(inputPath
)){return null;}StringBuilder commend
= new StringBuilder();String ffPath
= getFFPath();
commend
.append(" ffmpeg ");commend
.append(" -i ");commend
.append(inputPath
);commend
.append(" -c copy ");commend
.append(" -an ");commend
.append(outputPath
);try {File dir
= new File(ffPath
);String[] cmd
= new String[]{"cmd", "/c", commend
.toString()};Process p
= Runtime.getRuntime().exec(cmd
, null, dir
);BufferedReader buf
= null; String line
= null;buf
= new BufferedReader(new InputStreamReader(p
.getInputStream()));StringBuffer sb
= new StringBuffer();while ((line
= buf
.readLine()) != null) {System.out
.println(line
);sb
.append(line
);continue;}int ret
= p
.waitFor();log
.info("結束視頻轉碼,共耗時:" + (System.currentTimeMillis() - start
) +"耗秒");return true;} catch (Exception e
) {System.out
.println(e
);return false;}}public static void delOriginalFile(String filePath
) {File file
= new File(filePath
);if (file
.exists())file
.delete();}public static void main(String[] args
) {String input
= "E:\\202110091408390859.mp4";String output
= "E:\\new3.mp4";System.out
.println(processMP4(input
, output
));delOriginalFile(input
);}
}
總結
以上是生活随笔為你收集整理的java 通过ffmpeg 将海康视频转码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。