java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換
本功能實(shí)現(xiàn)需要用到第三方j(luò)ar包 jave,JAVE 是java調(diào)用FFmpeg的封裝工具。
spring boot項(xiàng)目pom文件中添加以下依賴
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>3.1.1</version> </dependency> <!-- 以下依賴根據(jù)系統(tǒng)二選一 --> <!-- win系統(tǒng)平臺(tái)的依賴 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-win64</artifactId> <version>3.1.1</version> </dependency> <!-- linux系統(tǒng)平臺(tái)的依賴 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux64</artifactId> <version>3.1.1</version> </dependency>
Java單類實(shí)現(xiàn)代碼,復(fù)制到Spring boot項(xiàng)目中,用idea編輯器 主方法運(yùn)行。
import ws.schild.jave.Encoder; import ws.schild.jave.EncoderException; import ws.schild.jave.MultimediaObject; import ws.schild.jave.encode.AudioAttributes; import ws.schild.jave.encode.EncodingAttributes; import java.io.File; import java.util.Arrays; public class VideoToAudio { //要輸出的音頻格式 private static String outputFormat="mp3"; /** * 獲得轉(zhuǎn)化后的文件名 * @param sourceFilePath : 源視頻文件路徑 * @return */ public static String getNewFileName(String sourceFilePath) { File source = new File(sourceFilePath); String fileName=source.getName().substring(0, source.getName().lastIndexOf(".")); return fileName+"."+outputFormat; } /** * 轉(zhuǎn)化音頻格式 * @param sourceFilePath : 源視頻文件路徑 * @param targetFilePath : 目標(biāo)音樂(lè)文件路徑 * @return */ public static void transform(String sourceFilePath, String targetFilePath) { File source = new File(sourceFilePath); File target = new File(targetFilePath); // 設(shè)置音頻屬性 AudioAttributes audio = new AudioAttributes(); audio.setCodec(null); // 設(shè)置轉(zhuǎn)碼屬性 EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat(outputFormat); attrs.setAudioAttributes(audio); try { // 音頻轉(zhuǎn)換格式類 Encoder encoder = new Encoder(); MultimediaObject mediaObject=new MultimediaObject(source); encoder.encode(mediaObject, target, attrs); System.out.println("轉(zhuǎn)換已完成..."); } catch (EncoderException e) { e.printStackTrace(); } } /** * 批量轉(zhuǎn)化音頻格式 * @param sourceFolderPath : 源視頻文件夾路徑 * @param targetFolderPath : 目標(biāo)音樂(lè)文件夾路徑 * @return */ public static void batchTransform(String sourceFolderPath, String targetFolderPath) { File sourceFolder = new File(sourceFolderPath); if(sourceFolder.list().length!=0){ Arrays.asList(sourceFolder.list()).forEach(e->{ transform(sourceFolderPath+"\\"+e, targetFolderPath+"\\"+getNewFileName(e)); }); } } public static void main(String[] args) { batchTransform("C:\\Users\\tarzan\\Desktop\\video","C:\\Users\\tarzan\\Desktop\\audio"); } }
運(yùn)行結(jié)果截圖
測(cè)試結(jié)果
視頻格式為mp4,大小約6.65MB,轉(zhuǎn)為音頻格式MP3,大小約1.60MB,轉(zhuǎn)化時(shí)間1s左右。
到此這篇關(guān)于java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)java 視頻轉(zhuǎn)音頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jdbc實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了jdbc實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02springboot實(shí)現(xiàn)跨域的五種方式總結(jié)
在Spring Boot中實(shí)現(xiàn)跨域,可以采用全局跨域和局部跨域兩種方式,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)跨域的五種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01IntelliJ IDEA 2021 Tomcat 8啟動(dòng)亂碼問(wèn)題的解決步驟
很多朋友遇到過(guò)IntelliJ IDEA 2021 Tomcat 8啟動(dòng)的時(shí)候出現(xiàn)各種奇葩問(wèn)題,最近有童鞋反映IntelliJ IDEA 2021 Tomcat 8啟動(dòng)亂碼,正好我也遇到這個(gè)問(wèn)題,下面我把解決方法分享給大家需要的朋友參考下吧2021-06-06SpringCloud Eureka服務(wù)的基本配置和操作方法
Eureka是Netflix開(kāi)源的一個(gè)基于REST的服務(wù)治理框架,主要用于實(shí)現(xiàn)微服務(wù)架構(gòu)中的服務(wù)注冊(cè)與發(fā)現(xiàn),Eureka是Netflix開(kāi)源的服務(wù)發(fā)現(xiàn)框架,用于在分布式系統(tǒng)中實(shí)現(xiàn)服務(wù)的自動(dòng)注冊(cè)與發(fā)現(xiàn),本文介紹SpringCloud Eureka服務(wù)的基本配置和操作方法,感興趣的朋友一起看看吧2023-12-12