您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用Java實(shí)現(xiàn)視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
本功能實(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");
}
}
以上是“如何使用Java實(shí)現(xiàn)視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。