1. 程式人生 > >在java中使用FFmpeg處理視訊與音訊

在java中使用FFmpeg處理視訊與音訊

FFmpeg是一個非常好用的視訊處理工具,下面講講如何在java中使用該工具類。

一、首先,讓我們來認識一下FFmpeg在Dos介面的常見操作

1.拷貝視訊,並指定新的視訊的名字以及格式

        ffmpeg.exe -i old.mp4 new.avi

2.將視訊和音訊結合,並指定視訊的長度(7秒),同時生成結合之後的視訊檔案

        ffmpeg.exe -i tsd.mp4 -i "周筆暢+-+最美的期待.mp3" -t 7 -y new.avi

3.使用ffmpg生成視訊截圖(對第一秒的畫面作為截圖)-vframes表示幀數 

        ffmpeg.exe -ss 00:00:01 -y -i 視訊.mp4 -vframes 1 new.jpg


二、在java中使用FFmpeg

   引入 FFMpegUtil工具類

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import 
java.util.List; /** * FFMPEG 的相關操作 * * @author Administrator */ public class FFMpegUtil { //Windows下 ffmpeg.exe的路徑 // private static String ffmpegEXE = "D:\\Downloads\\ffmpeg-20180528-ebf85d3-win64-static\\bin\\ffmpeg.exe"; //Linux與mac下 ffmpeg的路徑 private static String ffmpegEXE = "/developer/ffmpeg-4.0/bin/ffmpeg"
; /** * @param videoInputPath 視訊的輸入路徑 * @param videoOutPath 視訊的輸出路徑 * @throws Exception */ // 拷貝視訊,並指定新的視訊的名字以及格式 // ffmpeg.exe -i old.mp4 new.avi public static void convetor(String videoInputPath, String videoOutPath) throws Exception { List<String> command = new ArrayList<String>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add(videoOutPath); ProcessBuilder builder = new ProcessBuilder(command); Process process = null; try { process = builder.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 使用這種方式會在瞬間大量消耗CPU和記憶體等系統資源,所以這裡我們需要對流進行處理 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(); } } /** * @param videoInputPath 原視訊的路徑 * @param audioInputPath 音訊的路徑 * @param videoOutPath 視訊與音訊結合之後的視訊的路徑 * @param time 視訊的長度 ,單位為 s * @throws Exception */ // 將視訊和音訊結合,並指定視訊的長度,同時生成結合之後的視訊檔案 // ffmpeg.exe -i tsd.mp4 -i "周筆暢+-+最美的期待.mp3" -t 7 -y new.avi public static void convetor(String videoInputPath, String audioInputPath, String videoOutPath, double time) throws Exception { List<String> command = new ArrayList<String>(); command.add(ffmpegEXE); command.add("-i"); command.add(videoInputPath); command.add("-i"); command.add(audioInputPath); command.add("-t"); command.add(String.valueOf(time)); command.add("-y"); command.add(videoOutPath); ProcessBuilder builder = new ProcessBuilder(command); Process process = null; try { process = builder.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 使用這種方式會在瞬間大量消耗CPU和記憶體等系統資源,所以這裡我們需要對流進行處理 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(); } } /** * @param time_coverimg 視訊的第幾秒作為封面圖 * @param videoInputPath 視訊的路徑 * @param frame 幀數 * @param coverOutputPath 視訊的封面圖的路徑 * @throws Exception */ // ffmpeg.exe -ss 00:00:01 -y -i 視訊.mp4 -vframes 1 new.jpg public static void convetor(String time_coverimg, String videoInputPath, int frame, String coverOutputPath) throws Exception { List<String> command = new ArrayList<String>(); command.add(ffmpegEXE); command.add("-ss"); command.add(time_coverimg); command.add("-y"); command.add("-i"); command.add(videoInputPath); command.add("-vframes"); command.add(String.valueOf(frame)); command.add(coverOutputPath); ProcessBuilder builder = new ProcessBuilder(command); Process process = null; try { process = builder.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 使用這種方式會在瞬間大量消耗CPU和記憶體等系統資源,所以這裡我們需要對流進行處理 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) { // String videoInputPath = "G:/videos-resources/180525DFH9X09GR4/video/2018052920010792217.mp4"; // String coverOutputPath = "G:/videos-resources/180525DFH9X09GR4/video/2018052920014289695.jpg"; // try { // convetor("00:00:01", videoInputPath, 1, coverOutputPath); // } catch (Exception e) { // e.printStackTrace(); // } // } }
==============================================================================================

該工具類其實是通過java來執行Dos介面的命令,有了這個工具類,我們就可以呼叫該類中的convetor來應對不同的需求,當然,這裡只介紹了FFmpeg中的部分功能,如果你想在java中使用FFmpeg的其他功能也很簡單,請參考該類,繼續過載該類中的convetor方法,將dos介面的命令搬到該類中來執行即可。