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界面的命令搬到該類中來運行即可。