Java工程使用ffmpeg進(jìn)行音視頻格式轉(zhuǎn)換的實現(xiàn)
ws.schild簡介
JAVE (Java Audio Video Encoder)是一個純Java的音視頻編碼器和解碼器庫,它是基于FFmpeg。JAVE庫提供了一些簡單易用的API,用于音頻和視頻格式的轉(zhuǎn)換、編碼、解碼等操作。它對于一些基本的音視頻處理任務(wù)來說是一個不錯的選擇。
這些庫都是基于FFmpeg的,并允許在Java中處理音頻和視頻文件。使用它們可以避免直接調(diào)用外部的FFmpeg命令行工具,而是通過Java API來實現(xiàn)音頻格式轉(zhuǎn)換等操作。
ws.schild是目前主流的對視頻和音頻進(jìn)行轉(zhuǎn)碼、裁剪以及提取操作的JAVE工具包。
一、主要工具類簡介
1. VideoAttributes
VideoAttributes是ws.schild工具包中對視頻屬性設(shè)置的重要工具類,是ws.schild實現(xiàn)對視頻操作的重中之重,同時也是應(yīng)用最多的一個類。
使用心得:(下文中video為該類的實例化對象)
1.video的Quality(視頻質(zhì)量)屬性對轉(zhuǎn)碼后視頻的大小有很大的影響,并且對轉(zhuǎn)碼的時間也有一定的影響,主要影響視頻質(zhì)量,參數(shù)類型是整形,數(shù)值越小表示質(zhì)量越高。
2.video的size(視頻尺寸)屬性對轉(zhuǎn)碼后視頻的大小有較大的影響,并且對轉(zhuǎn)碼的時間也有一定的影響,參數(shù)類型是VideoSize。
3.video的BitRate(比特率)屬性對轉(zhuǎn)碼視頻的大小和時間都影響較大,主要影響視頻的流暢程度,一般設(shè)置的需要大一些,如100k(針對已測試過的視頻,不敢保證普遍性)。
4.video的Codec(編解碼器)屬性一般設(shè)置為mpeg4或者h(yuǎn)264。
5.video的FrameRate(幀率)屬性不要設(shè)置的太低,一般設(shè)置為15及以上,如果設(shè)置太小視頻會不流暢。
2. AudioAttributes
AudioAttributes是ws.schild工具包中對音頻屬性設(shè)置的重要工具類。
二、使用步驟
1. 導(dǎo)入ws.schild的jar包
maven地址:https://mvnrepository.com/artifact/ws.schild/jave-all-deps
在maven庫里可以看到ws.schild其下有好幾個包
- jave-core
- jave-nativebin-win32
- jave-nativebin-win64
- jave-nativebin-linux32
- jave-nativebin-linux64
- jave-nativebin-osx64
jave-all-deps 是最完整的,它的pom.xml引用了所有版本的文件
看名字就能看出,jave-core是核心包,其它幾個是windows、linux、maxosx系統(tǒng)下的本地文件。其實nativebin里面就是一個類似ffmpeg.exe的文件,程序在運行時拼接參數(shù),再調(diào)用對應(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>
確定運行平臺后可以將其他包排除,如服務(wù)在Linux系統(tǒng)跑,就可以將Windows、macos平臺的包排除。
<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. 實現(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("總耗時:"+ (end-start) +"ms"); } }
3. 實現(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. 實現(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ù)啟動后ws.schild工具包會將與運行系統(tǒng)匹配的ffmpeg可執(zhí)行文件,一般Windows系統(tǒng)是將ffmpeg-amd64-3.3.1.exe下載到此路徑下:C:\Users\xxx\AppData\Local\Temp\jave (xxx是你的用戶文件)。
2、用java代碼拼接ffmpeg指令的參數(shù),以及源文件路徑和目標(biāo)文件路徑,而后調(diào)用encode語句將指令發(fā)給ffmpeg執(zhí)行。
ffmpeg指令的參數(shù)講解可以看看上一篇文章:ffmpeg安裝及音頻轉(zhuǎn)換指令應(yīng)用(win10)
綜上,ws.schild 工具包可以非常方便的幫助我們實現(xiàn)對視頻和音頻的轉(zhuǎn)碼和裁剪等操作,并且可以通過設(shè)置不同參數(shù)來滿足不同的需求。但是就目前的測試結(jié)果而言,不同的編碼格式對于參數(shù)的敏感度可能不同,具體場景應(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)換的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java ffmpeg音視頻轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決@RequestBody接收json對象報錯415的問題
這篇文章主要介紹了解決@RequestBody接收json對象報錯415的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06一小時迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API
這篇文章主要介紹了一小時迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Java動態(tài)數(shù)組添加數(shù)據(jù)的方法與應(yīng)用示例
這篇文章主要介紹了Java動態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實例形式詳細(xì)分析了Java動態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11Springboot使用SPI注冊bean到spring容器的示例代碼
這篇文章主要介紹了Springboot使用SPI注冊bean到spring容器,主要包括mydriver接口,mysqldriver實現(xiàn)過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10