使用Java實(shí)現(xiàn)視頻與音頻轉(zhuǎn)碼
需求
把a(bǔ)pe,ios,dsf,dff,ape,flac等音頻格式轉(zhuǎn)換為mp3,wav音頻格式,因?yàn)榇蟛糠忠魳凡シ牌骱蚳tml的< audio>標(biāo)簽都支持mp3和wav格式。
wav音樂格式是無(wú)損音樂,其他都是有損音樂格式或者無(wú)損音樂的壓縮版。
把其他視頻格式轉(zhuǎn)換成HTML5能播放的視頻格式,也可獲取上傳視頻的時(shí)長(zhǎng),網(wǎng)頁(yè)視頻播放只支持一下三種:
| 格式 | 解釋 |
|---|---|
| Ogg | 帶有 Theora 視頻編碼和 Vorbis 音頻編碼的 Ogg 文件 |
| MPEG4 | 帶有 H.264 視頻編碼和 AAC 音頻編碼的 MPEG 4 文件 |
| WebM | 帶有 VP8 視頻編碼和 Vorbis 音頻編碼的 WebM 文件 |
Jave介紹
JAVE(Java Audio Video Encoder),是一個(gè)包涵ffmpeg項(xiàng)目庫(kù)。開發(fā)這可以運(yùn)用它去實(shí)現(xiàn)音頻(Audio)與視頻(Video)文件的轉(zhuǎn)碼。
官方文檔:http://www.sauronsoftware.it/projects/jave/manual.php
Jave中幾個(gè)重要的類
1.Encoder類
// 音頻轉(zhuǎn)換格式類
Encoder encoder = new Encoder();
轉(zhuǎn)碼函數(shù):
// 轉(zhuǎn)碼函數(shù) public void encode(File source, File target, EncodingAttributes attributes, EncoderProgressListener listener);
參數(shù)說(shuō)明:
- source:需要轉(zhuǎn)碼的源文件
- target : 需轉(zhuǎn)型成的目標(biāo)文件
- attributes:包含編碼所需數(shù)據(jù)的參數(shù)
- listener :可選參數(shù),實(shí)現(xiàn)該監(jiān)聽器可調(diào)用該接口中的方法,下面有說(shuō)明
2.EncodingAttributes類
// 設(shè)置轉(zhuǎn)碼屬性
EncodingAttributes attrs = new EncodingAttributes();
方法列表:
// 設(shè)置轉(zhuǎn)碼音頻,添加音頻轉(zhuǎn)碼時(shí)所需音頻屬性 public void setAudioAttributes(AudioAttributes audioAttributes) // 設(shè)置轉(zhuǎn)碼視頻,添加視頻轉(zhuǎn)碼時(shí)所需音頻屬性 public void setVideoAttributes(VideoAttributes videoAttributes) //設(shè)置轉(zhuǎn)碼格式 public void setFormat(String format) //設(shè)置轉(zhuǎn)碼偏移位置,自定義轉(zhuǎn)碼開始時(shí)間 public void setOffset(Float offset) //設(shè)置轉(zhuǎn)碼持續(xù)時(shí)間,自定義轉(zhuǎn)碼持續(xù)時(shí)間 public void setDuration(Float duration)
3.AudioAttributes類:音頻相關(guān)屬性
// 設(shè)置音頻屬性
AudioAttributes audio = new AudioAttributes();
方法列表:
// 設(shè)置編碼器 public void setCodec(String codec) // 設(shè)置音頻比特率 public void setBitRate(Integer bitRate) // 設(shè)置音頻節(jié)錄率 public void setSamplingRate(Integer bitRate) // 設(shè)置聲音頻道 public void setChannels(Integer channels) // 設(shè)置音頻音量 public void setVolume(Integer volume)
4.VideoAttributes 類:視頻相關(guān)屬性
// 設(shè)置視頻屬性 VideoAttributes video = new VideoAttributes();
方法列表:
// 設(shè)置編碼器 public void setCodec(String codec) // 設(shè)置標(biāo)簽(通常用多媒體播放器所選擇的視頻解碼) public void setTag(String tag) // 設(shè)置視頻比特率 public void setBitRate(Integer bitRate) // 設(shè)置視頻幀率 public void setFrameRate(Integer bitRate) // 設(shè)置視頻大小 public void setSize(VideoSize size)
5.MultimediaInfo類:媒體文件信息
//創(chuàng)建媒體信息對(duì)象
File source = new File("D:/test.mp4");
MultimediaInfo info = encoder.getInfo(file);
方法列表:
// 獲得文件格式 public String getFormat(); // 獲得時(shí)間(ms) public long getDuration(); // 獲得音頻對(duì)象 public AudioInfo getAudio(); // 獲得視頻對(duì)象 public VideoInfo getVideo();
6.EncoderProgressListener接口:監(jiān)測(cè)轉(zhuǎn)碼操作
public interface EncoderProgressListener {
//源文件信息
void sourceInfo(MultimediaInfo var1);
//增長(zhǎng)千分率
void progress(int var1);
//轉(zhuǎn)碼信息提示
void message(String var1);
}
實(shí)例
介紹:把除mp3和wav外的音樂格式轉(zhuǎn)換成wav音樂格式
/**
* 轉(zhuǎn)化音頻格式
* @param oldFormatPath : 原音樂路徑
* @param newFormatPath : 目標(biāo)音樂路徑
* @return
*/
public static boolean transforMusicFormat(String oldFormatPath, String newFormatPath) {
File source = new File(oldFormatPath);
File target = new File(newFormatPath);
// 音頻轉(zhuǎn)換格式類
Encoder encoder = new Encoder();
// 設(shè)置音頻屬性
AudioAttributes audio = new AudioAttributes();
audio.setCodec(null);
// 設(shè)置轉(zhuǎn)碼屬性
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("wav");
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
System.out.println("傳喚已完成...");
deleteFile(oldFormatPath);
return true;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InputFormatException e) {
e.printStackTrace();
} catch (EncoderException e) {
e.printStackTrace();
}
return false;
}
/**
* 刪除原音頻文件
* @param filePath : 原文件路徑
* @return
*/
public static boolean deleteFile(String filePath){
File file = new File(filePath);
// 路徑為文件且不為空則進(jìn)行刪除
if (file.isFile() && file.exists()) {
// 文件刪除
file.delete();
return true;
}
return false;
}
/**
* 判斷格式是否需要轉(zhuǎn)換
* @param musicName
* @return
*/
public static boolean checkMusicFormat(String musicName){
if (musicName== null || "".equals(musicName)){
return false;
}
String suffix = musicName.substring(musicName.lastIndexOf(".")+1);
if ("mp3".equals(suffix) || "wav".equals(suffix)){
return false;
}
return true;
}
總結(jié)
轉(zhuǎn)換音頻格式步驟:
- 創(chuàng)建轉(zhuǎn)換格式類Encoder
- 設(shè)置音頻屬性,通過(guò)AudioAttributes類完成
- 設(shè)置轉(zhuǎn)碼屬性,通過(guò)EncodingAttributes類完成
- 調(diào)用Encoder類的encode方法轉(zhuǎn)碼
視頻格式轉(zhuǎn)換類似。
到此這篇關(guān)于使用Java實(shí)現(xiàn)視頻與音頻轉(zhuǎn)碼的文章就介紹到這了,更多相關(guān)Java視頻與音頻轉(zhuǎn)碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Properties實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)驅(qū)動(dòng)
這篇文章主要介紹了基于Properties實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)驅(qū)動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Netty與Spring Boot的整合實(shí)現(xiàn)
這篇文章主要介紹了Netty與Spring Boot的整合的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)
這篇文章主要介紹了Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
spring cloud gateway請(qǐng)求跨域問(wèn)題解決方案
這篇文章主要介紹了spring cloud gateway請(qǐng)求跨域問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

