Java工程使用ffmpeg進(jìn)行音視頻格式轉(zhuǎn)換的實(shí)現(xiàn)
ws.schild簡(jiǎn)介
JAVE (Java Audio Video Encoder)是一個(gè)純Java的音視頻編碼器和解碼器庫(kù),它是基于FFmpeg。JAVE庫(kù)提供了一些簡(jiǎn)單易用的API,用于音頻和視頻格式的轉(zhuǎn)換、編碼、解碼等操作。它對(duì)于一些基本的音視頻處理任務(wù)來(lái)說(shuō)是一個(gè)不錯(cuò)的選擇。
這些庫(kù)都是基于FFmpeg的,并允許在Java中處理音頻和視頻文件。使用它們可以避免直接調(diào)用外部的FFmpeg命令行工具,而是通過(guò)Java API來(lái)實(shí)現(xiàn)音頻格式轉(zhuǎn)換等操作。
ws.schild是目前主流的對(duì)視頻和音頻進(jìn)行轉(zhuǎn)碼、裁剪以及提取操作的JAVE工具包。
一、主要工具類(lèi)簡(jiǎn)介
1. VideoAttributes
VideoAttributes是ws.schild工具包中對(duì)視頻屬性設(shè)置的重要工具類(lèi),是ws.schild實(shí)現(xiàn)對(duì)視頻操作的重中之重,同時(shí)也是應(yīng)用最多的一個(gè)類(lèi)。
使用心得:(下文中video為該類(lèi)的實(shí)例化對(duì)象)
1.video的Quality(視頻質(zhì)量)屬性對(duì)轉(zhuǎn)碼后視頻的大小有很大的影響,并且對(duì)轉(zhuǎn)碼的時(shí)間也有一定的影響,主要影響視頻質(zhì)量,參數(shù)類(lèi)型是整形,數(shù)值越小表示質(zhì)量越高。
2.video的size(視頻尺寸)屬性對(duì)轉(zhuǎn)碼后視頻的大小有較大的影響,并且對(duì)轉(zhuǎn)碼的時(shí)間也有一定的影響,參數(shù)類(lèi)型是VideoSize。
3.video的BitRate(比特率)屬性對(duì)轉(zhuǎn)碼視頻的大小和時(shí)間都影響較大,主要影響視頻的流暢程度,一般設(shè)置的需要大一些,如100k(針對(duì)已測(cè)試過(guò)的視頻,不敢保證普遍性)。
4.video的Codec(編解碼器)屬性一般設(shè)置為mpeg4或者h(yuǎn)264。
5.video的FrameRate(幀率)屬性不要設(shè)置的太低,一般設(shè)置為15及以上,如果設(shè)置太小視頻會(huì)不流暢。
2. AudioAttributes
AudioAttributes是ws.schild工具包中對(duì)音頻屬性設(shè)置的重要工具類(lèi)。
二、使用步驟
1. 導(dǎo)入ws.schild的jar包
maven地址:https://mvnrepository.com/artifact/ws.schild/jave-all-deps
在maven庫(kù)里可以看到ws.schild其下有好幾個(gè)包
- jave-core
- jave-nativebin-win32
- jave-nativebin-win64
- jave-nativebin-linux32
- jave-nativebin-linux64
- jave-nativebin-osx64
jave-all-deps 是最完整的,它的pom.xml引用了所有版本的文件
看名字就能看出,jave-core是核心包,其它幾個(gè)是windows、linux、maxosx系統(tǒng)下的本地文件。其實(shí)nativebin里面就是一個(gè)類(lèi)似ffmpeg.exe的文件,程序在運(yùn)行時(shí)拼接參數(shù),再調(diào)用對(duì)應(yīng)的ffmpeg去執(zhí)行。
首先在pom文件中導(dǎo)入第三方j(luò)ar包:
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>3.3.1</version>
</dependency>確定運(yùn)行平臺(tái)后可以將其他包排除,如服務(wù)在Linux系統(tǒng)跑,就可以將Windows、macos平臺(tái)的包排除。
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>3.3.1</version>
<exclusions>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win32</artifactId>
</exclusion>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
</exclusion>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-osx64</artifactId>
</exclusion>
<exclusion>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-osxm1</artifactId>
</exclusion>
</exclusions>
</dependency>2. 實(shí)現(xiàn)視頻的轉(zhuǎn)碼
將視頻轉(zhuǎn)碼為H.264編碼(包括音頻轉(zhuǎn)碼):
/**
* 視頻轉(zhuǎn)碼
* @param videoSource
* @param videoTarget
* @return true or false
*/
public static boolean videoToVideo(String videoSource, String videoTarget) {
// Date time = new Date();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
// System.out.println(simpleDateFormat.format(time));
long start = System.currentTimeMillis();
File source = new File(videoSource);
File target = new File(videoTarget);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
audio.setBitRate(236000 / 2);
audio.setChannels(2);
audio.setSamplingRate(8000);
VideoAttributes video = new VideoAttributes();
video.setCodec("h264");
video.setBitRate(1000000);
video.setFrameRate(25);
video.setQuality(4);
// video.setSize(new VideoSize(720, 480));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(encoder.getUnhandledMessages());
return false;
}finally {
// time = new Date();
// System.out.println(simpleDateFormat.format(time));
long end = System.currentTimeMillis();
System.out.println("總耗時(shí):"+ (end-start) +"ms");
}
}
3. 實(shí)現(xiàn)音頻的轉(zhuǎn)碼
將音頻轉(zhuǎn)碼為mp3編碼:
audioPath可以為amr、wav、m4r等其他音頻格式的文件。
/**
* 音頻轉(zhuǎn)換為mp3格式,audioPath可更換為要轉(zhuǎn)換的音頻格式
* @param audioPath
* @param mp3Path
*/
public static void toMp3(String audioPath,String mp3Path){
File source = new File(audioPath);
File target = new File(mp3Path);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(128000);
audio.setChannels(2);
audio.setSamplingRate(44100);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat("mp3");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs);
} catch (EncoderException e) {
e.printStackTrace();
}
}4. 實(shí)現(xiàn)視頻文件轉(zhuǎn)為音頻文件
將視頻轉(zhuǎn)為音頻:
/**
* 視頻文件轉(zhuǎn)音頻文件
* @param videoPath
* @param audioPath
* return true or false
*/
public static boolean videoToAudio(String videoPath, String audioPath){
File fileMp4 = new File(videoPath);
File fileMp3 = new File(audioPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(128000);
audio.setChannels(2);
audio.setSamplingRate(44100);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat("mp3");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
MultimediaObject mediaObject = new MultimediaObject(fileMp4);
try{
encoder.encode(mediaObject,fileMp3,attrs);
Log.info("File MP4 convertito MP3");
return true;
}catch (Exception e){
Log.error("File non convertito");
Log.error(e.getMessage());
return false;
}
}
總結(jié)
執(zhí)行原理大致是:
1、服務(wù)啟動(dòng)后ws.schild工具包會(huì)將與運(yùn)行系統(tǒng)匹配的ffmpeg可執(zhí)行文件,一般Windows系統(tǒng)是將ffmpeg-amd64-3.3.1.exe下載到此路徑下:C:\Users\xxx\AppData\Local\Temp\jave (xxx是你的用戶(hù)文件)。
2、用java代碼拼接ffmpeg指令的參數(shù),以及源文件路徑和目標(biāo)文件路徑,而后調(diào)用encode語(yǔ)句將指令發(fā)給ffmpeg執(zhí)行。
ffmpeg指令的參數(shù)講解可以看看上一篇文章:ffmpeg安裝及音頻轉(zhuǎn)換指令應(yīng)用(win10)
綜上,ws.schild 工具包可以非常方便的幫助我們實(shí)現(xiàn)對(duì)視頻和音頻的轉(zhuǎn)碼和裁剪等操作,并且可以通過(guò)設(shè)置不同參數(shù)來(lái)滿(mǎn)足不同的需求。但是就目前的測(cè)試結(jié)果而言,不同的編碼格式對(duì)于參數(shù)的敏感度可能不同,具體場(chǎng)景應(yīng)該具體分析。
參考鏈接:
https://github.com/a-schild/jave2
http://www.dbjr.com.cn/program/315429ev9.htm
https://blog.csdn.net/liuchongming/article/details/106994861
到此這篇關(guān)于Java工程使用ffmpeg進(jìn)行音視頻格式轉(zhuǎn)換的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java ffmpeg音視頻轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決@RequestBody接收json對(duì)象報(bào)錯(cuò)415的問(wèn)題
這篇文章主要介紹了解決@RequestBody接收json對(duì)象報(bào)錯(cuò)415的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
一小時(shí)迅速入門(mén)Mybatis之bind與多數(shù)據(jù)源支持 Java API
這篇文章主要介紹了一小時(shí)迅速入門(mén)Mybatis之bind與多數(shù)據(jù)源支持 Java API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法與應(yīng)用示例
這篇文章主要介紹了Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java動(dòng)態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
Springboot如何通過(guò)自定義工具類(lèi)獲取bean
這篇文章主要介紹了Springboot通過(guò)自定義工具類(lèi)獲取bean方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java中Stream?Filter多條件篩選過(guò)濾代碼舉例
這篇文章主要給大家介紹了關(guān)于Java中Stream?Filter多條件篩選過(guò)濾的相關(guān)資料,Java Stream中的filter方法可以使用多個(gè)條件來(lái)過(guò)濾數(shù)據(jù),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-12-12
Springboot使用SPI注冊(cè)bean到spring容器的示例代碼
這篇文章主要介紹了Springboot使用SPI注冊(cè)bean到spring容器,主要包括mydriver接口,mysqldriver實(shí)現(xiàn)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
SpringBoot項(xiàng)目整合Redis教程詳解
這篇文章主要介紹了SpringBoot項(xiàng)目整合Redis教程詳解,Redis?是完全開(kāi)源的,遵守?BSD?協(xié)議,是一個(gè)高性能的?key-value?數(shù)據(jù)庫(kù)。感興趣的小伙伴可以參考閱讀本文2023-03-03

