java ffmpeg(Windows/Linux)擷取視訊做封面
一、Windows版本
1、下載
前往FFmpeg官網http://ffmpeg.org/download.html下載Windows版本FFmpeg(我下載ffmpeg-4.0-win64-shared)
2、解壓下載後解壓到本地(我的解壓目錄D:\wedive_file\ffmpeg)
二、Linux版本
1、下載
前往FFmpeg官網http://ffmpeg.org/download.html下載Linux版本FFmpeg(我下載ffmpeg-snapshot.tar)
前往http://yasm.tortall.net/Download.html 下載yasm(彙編器,有用)(我下載yasm-1.3.0.tar)
(1)將Linux版本FFmpeg上傳Linux解壓(tar -xjvf ffmpeg-snapshot.tar.bz2);
(2)將彙編器yasm上傳至Linux解壓(tar -xvzf yasm-1.3.0.tar.gz);
(3)安裝彙編器yasm
cd yasm-1.3.0;
./configure;
make install;
(4)yasm安裝成功後,回到FFmpeg解壓目錄(此步驟編譯過程有點長,請耐心等待,我也是這麼等待過來的哦)
./configure --enable-shared --prefix=/monchickey/ffmpeg;(prefix後面是安裝目錄)
make install;
(5)安裝成功後,前往安裝目錄cd /monchickey/ffmpeg/檢視,發現有bin,include,lib,share這4個目錄,其中bin是ffmpeg主程式二進位制目錄,include是C/C++標頭檔案目錄,lib是編譯好的庫檔案目錄,share是文件目錄,然後進入bin目錄,執行 ./ffmpeg -version 檢視當前版本的詳細資訊,預設情況下一般會報libavdevice.so.57: cannot open shared object file: No such file or directory,原因是lib目錄未載入到連結到系統庫中,系統ld目錄列表在/etc/ld.so.conf中,開啟檔案會發現,裡面引用了/etc/ld.so.conf.d/下面所有的.conf檔案,比如mariadb-x86_64.conf我們只需要建立一個檔案並寫入lib路徑即可,執行命令: vim /etc/ld.so.conf.d/ffmpeg.conf 然後新增一行內容: /monchickey/ffmpeg/lib 之後儲存並退出,然後執行 ldconfig 使配置生效,現在再次執行 ./ffmpeg -version 顯示就正常了
三、Java程式碼
/** * java呼叫FFmpeg擷取視訊做封面入口 * @param videoPath 視訊路徑 * @param ffmpegPath FFmpeg安裝路徑(Windows使用) * @param coverPath 封面儲存路徑 */ public static void processImgMain(String videoPath,String ffmpegPath,String coverPath){ if(ProjectConfig.ENV.equals(ProjectConfig.PROJECT_DEVLELOP)){//此處判斷是否Windows,自己更改一下唄 processImg(videoPath,ffmpegPath,coverPath); }else { String command = "/monchickey/ffmpeg/bin/./ffmpeg -ss 00:00:00 -i " + videoPath + " -f image2 -y " + coverPath; processImgCmd(command); } }
/** * 擷取視訊封面 java Windows版本 * @param videoPath 視訊路徑 * @param ffmpegPath ffmpeg.exe存放路徑 擷取工具 * @param imgPath 封面存放路徑 * @return */ public static boolean processImg(String videoPath,String ffmpegPath,String imgPath){ File file = new File(videoPath); if (!file.exists()) { System.err.println("路徑[" + videoPath + "]對應的視訊檔案不存在!"); return false; } List<String> commands = new ArrayList<String>(); commands.add(ffmpegPath); commands.add("-i"); commands.add(videoPath); commands.add("-y"); commands.add("-f"); commands.add("image2"); commands.add("-ss"); commands.add("0");//這個引數是設定擷取視訊多少秒時的畫面 //commands.add("-t"); //commands.add("0.001"); commands.add("-s"); commands.add("700x525"); commands.add(imgPath); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); builder.start(); System.out.println("擷取成功"); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * FFmpeg擷取視訊封面Linux版本 * @param command 執行cmd命令 */ public static void processImgCmd(String command){ try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); InputStream stderr = proc.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String line = null; System.out.println("<ERROR>"); while ((line = br.readLine()) != null){ System.out.println(line); } System.out.println("</ERROR>"); int exitVal = proc.waitFor(); System.out.println("Process exitValue: " + exitVal); } catch (Throwable t) { System.out.println(t); t.printStackTrace(); } }