FFmpeg介紹
多媒體視頻處理工具FFmpeg有非常強大的功能包括視頻采集功能、視頻格式轉(zhuǎn)換、視頻抓圖、給視頻加水印等。
在我們實際應用中,對視頻格式轉(zhuǎn)換、音視頻合并、截圖、加水印等功能用的很多,我在里面就使用了音頻與視頻的合并功能。
下面介紹下具體的使用
FFmpeg的安裝
下載
官網(wǎng)下載: http://ffmpeg.org/download.html
安裝yasm
官網(wǎng)下載:http://yasm.tortall.net/Download.html
安裝yasm
上傳至Linux準備安裝,解壓、安裝
tar -xvzf yasm-1.3.0.tar.gz
cd yasm-1.3.0/
./configure
make
make install
安裝ffmpeg
上傳至Linux準備安裝,解壓、安裝
tar -xvzf ffmpeg-4.2.2.tar.gz
cd ffmpeg-4.2.2/
./configure --enable-shared --prefix=/opt/ffmpeg
make (編譯過程會有點長)
make install (會把ffmpeg相關(guān)執(zhí)行程序、頭文件、lib庫安裝在/opt/ffmpeg/下)
創(chuàng)建一個文件并寫入lib路徑
即可執(zhí)行命令:vim /etc/ld.so.conf.d/ffmpeg.conf
然后添加一行內(nèi)容:/opt/ffmpeg/lib
之后保存并退出(esc : wq) 然后執(zhí)行 ldconfig 使配置生效,
現(xiàn)在再次執(zhí)行./ffmpeg -version 顯示就正常了
配置軟鏈接 (ln -s 源文件 目標文件)
ln -s /opt/ffmpeg/bin/ffmpeg /usr/local/bin/ffmpeg
執(zhí)行ffmpeg出現(xiàn):

到此就安裝好了,通過 ffmpeg的命令使用,感受他的強大吧
在項目中怎么去使用???
/**
*
* @param videoSouce 視頻地址
* @param audioPath 音頻地址
* @param targetPath 合成的文件地址
*/
public static boolean mergeAudioAndVideo(String videoSouce,String audioPath,String targetPath){
Process process = null;
// 這個是獲取ffmpegpath 目標機的安裝路徑
String ffmpegPath = new DefaultFFMPEGLocator().getFFMPEGExecutablePath();
try {
String command =ffmpegPath + " -i " + videoSouce + " -i " + audioPath
+ " -y " + targetPath;
process = Runtime.getRuntime().exec(command);
//process = Runtime.getRuntime().exec(new String[]{"sh","-c",command});
process.waitFor();
// 使用這種方式會在瞬間大量消耗CPU和內(nèi)存等系統(tǒng)資源,所以這里我們需要對流進行處理
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();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
上面代碼 就能在項目里面操作FFmpeg了,本質(zhì)上海是操作ffmpeg命令
上面的命令模式為:
ffmpeg -i 5.webm -i 56.wav -y kb.mp4 合并音視頻文件同時轉(zhuǎn)碼為mp4格式
ffmpeg命令參考:https://blog.csdn.net/wenmingzheng/article/details/88373192