java使用ffmpeg處理視頻的方法
FFmpeg是一套可以用來記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計(jì)算機(jī)程序。采用LGPL或GPL許可證。它提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案。
官網(wǎng)鏈接http://ffmpeg.org/
1.下載并解壓windows版本安裝包
2.windows本地使用命令行測(cè)試
1.修改格式測(cè)試(轉(zhuǎn)碼)
- 將需要修改的視頻A.avi 提前放在bin目錄下
- 在bin目錄下cmd進(jìn)入命令行
- 輸入命令完成轉(zhuǎn)碼成B.mp4
ffmpeg.exe -i A.avi -y B.mp4
2.視頻音頻結(jié)合測(cè)試
- 將需要修改的視頻A.avi和bgm.mp3 提前放在bin目錄下
- 在bin目錄下cmd進(jìn)入命令行
- 輸入命令完成合并成8秒的new.avi
ffmpeg.exe -i A.avi -i bgm.mp3 -t 8 -y new.avi
3.java中建立工具測(cè)試類
package com.xc.utils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class FFMpegTest { private String ffmpegEXE; public FFMpegTest(String ffmpegEXE) { super(); this.ffmpegEXE = ffmpegEXE; } public void convertor(String videoInputPath, String videoOutputPath) throws Exception { // ffmpeg -i input.mp4 -y output.avi List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add("-y"); command.add(videoOutputPath); for (String c : command) { System.out.print(c + " "); } ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); InputStream errorStream = process.getErrorStream(); InputStreamReader inputStreamReader = new InputStreamReader(errorStream); BufferedReader br = new BufferedReader(inputStreamReader); String line = ""; while ( (line = br.readLine()) != null ) { } if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } public static void main(String[] args) { FFMpegTest ffmpeg = new FFMpegTest("C:\\ffmpeg\\bin\\ffmpeg.exe"); try { ffmpeg.convertor("C:\\a.mp4", "C:\\b.avi"); } catch (Exception e) { e.printStackTrace(); } } }
package com.xc.utils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class MergeVideoMp3 { private String ffmpegEXE; public MergeVideoMp3(String ffmpegEXE) { super(); this.ffmpegEXE = ffmpegEXE; } public void convertor(String videoInputPath, String mp3InputPath, double seconds, String videoOutputPath) throws Exception { // ffmpeg.exe -i A.avi -i bgm.mp3 -t 7 -y new.avi List<String> command = new ArrayList<>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add("-i"); command.add(mp3InputPath); command.add("-t"); command.add(String.valueOf(seconds)); command.add("-y"); command.add(videoOutputPath); // for (String c : command) { // System.out.print(c + " "); // } ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); InputStream errorStream = process.getErrorStream(); InputStreamReader inputStreamReader = new InputStreamReader(errorStream); BufferedReader br = new BufferedReader(inputStreamReader); String line = ""; while ( (line = br.readLine()) != null ) { } if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } public static void main(String[] args) { MergeVideoMp3 ffmpeg = new MergeVideoMp3("C:\\ffmpeg\\bin\\ffmpeg.exe"); try { ffmpeg.convertor("C:\\a.avi", "C:\\bgm.mp3", 7.1, "C:\\javaNew.mp4"); } catch (Exception e) { e.printStackTrace(); } } }
總結(jié)
到此這篇關(guān)于java使用ffmpeg處理視頻的方法的文章就介紹到這了,更多相關(guān)java ffmpeg處理視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis新增save結(jié)束后自動(dòng)返回主鍵id詳解
這篇文章主要介紹了mybatis新增save結(jié)束后自動(dòng)返回主鍵id詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12使用JMX監(jiān)控Zookeeper狀態(tài)Java API
今天小編就為大家分享一篇關(guān)于使用JMX監(jiān)控Zookeeper狀態(tài)Java API,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Java使用POI導(dǎo)出Excel(二):多個(gè)sheet
這篇文章介紹了Java使用POI導(dǎo)出Excel的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Java測(cè)試題 實(shí)現(xiàn)一個(gè)注冊(cè)功能過程解析
這篇文章主要介紹了Java測(cè)試題 實(shí)現(xiàn)一個(gè)注冊(cè)功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10