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)音樂文件路徑 * @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)音樂文件夾路徑 * @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:UserstarzanDesktopvideo','C:UserstarzanDesktopaudio'); } }
運(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題3. JavaScript實(shí)現(xiàn)頁(yè)面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例4. 解決Android Studio 格式化 Format代碼快捷鍵問題5. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis6. Java使用Tesseract-Ocr識(shí)別數(shù)字7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)9. Springboot 全局日期格式化處理的實(shí)現(xiàn)10. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)
