溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Java中使用FFmpeg 處理視頻文件

發(fā)布時間:2021-03-20 16:24:46 來源:億速云 閱讀:297 作者:Leah 欄目:編程語言

怎么在Java中使用FFmpeg 處理視頻文件?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

FFmpeg是一套可以用來記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計算機程序。采用LGPL或GPL許可證。它提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案。它包含了非常先進(jìn)的音頻/視頻編解碼庫libavcodec,為了保證高可移植性和編解碼質(zhì)量,libavcodec里很多code都是從頭開發(fā)的。

FFmpeg在Linux平臺下開發(fā),但它同樣也可以在其它操作系統(tǒng)環(huán)境中編譯運行,包括Windows、Mac OS X等。這個項目最早由Fabrice Bellard發(fā)起,2004年至2015年間由Michael Niedermayer主要負(fù)責(zé)維護(hù)。許多FFmpeg的開發(fā)人員都來自MPlayer項目,而且當(dāng)前FFmpeg也是放在MPlayer項目組的服務(wù)器上。項目的名稱來自MPEG視頻編碼標(biāo)準(zhǔn),前面的"FF"代表"Fast Forward"。

首先說明,我是在 https://ffmpeg.zeranoe.com/builds/ 這個地方下載的軟件,Windows 和 Mac 解壓之后即可使用。具體代碼如下:

package cn.bridgeli.demo;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author BridgeLi
 * @date 2020/2/29 15:40
 */
public class FfmpegTest {

 private static final String OS = System.getProperty("os.name").toLowerCase();
 private static final String FFMPEG_PATH = "/Users/bridgeli/ffmpeg-20200216-8578433-macos64-static/bin/ffmpeg";

 @Test
 public void testFfmpeg() {

 String inputWavFile = "/Users/bridgeli/inputWavFile.wav";
 String inputMp3File = "/Users/bridgeli/inputMp3File.mp3";
 String inputMp4File = "/Users/bridgeli/inputMp4File.mp4";
 String outMergeMp3File = "/Users/bridgeli/outMergeMp3File.mp3";
 String outMergeMp3AndMp4File = "/Users/bridgeli/outMergeMp3AndMp4File.mp4";
 String outConcatMp3File = "/Users/bridgeli/outConcatMp3File.mp3";

 // 拼接
 String command = null;
 if (OS.contains("mac") || OS.contains("linux")) {
 command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex [0:0][1:0]concat=n=2:v=0:a=1[a] -map [a] " + outConcatMp3File;
 } else if (OS.contains("windows")) {
 command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex \"[0:0][1:0]concat=n=2:v=0:a=1[a]\" -map \"[a]\" " + outConcatMp3File;
 }
 // 合并(視頻和音頻)
// String command = FFMPEG_PATH + " -i " + inputMp4File + " -i " + outConcatMp3File + " -c:v copy -c:a aac -strict experimental " + outMergeMp3AndMp4File;
 // 合并
// String command = FFMPEG_PATH + " -i " + inputMp3File + " -i " + inputWavFile + " -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 " + outMergeMp3File;
 System.out.println(command);

 Process process = null;
 try {
 process = Runtime.getRuntime().exec(command);
 } catch (IOException e) {
 e.printStackTrace();
 }

 if (null == process) {
 return;
 }

 try {
 process.waitFor();
 } catch (InterruptedException e) {
 e.printStackTrace();
 }

 try (InputStream errorStream = process.getErrorStream();
 InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
 BufferedReader br = new BufferedReader(inputStreamReader)) {

 String line = null;
 StringBuffer context = new StringBuffer();
 while ((line = br.readLine()) != null) {
 context.append(line);
 }

 System.out.println("error message: " + context);
 } catch (IOException e) {
 e.printStackTrace();
 }

 process.destroy();
 }
}

關(guān)于怎么在Java中使用FFmpeg 處理視頻文件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI