java視頻轉換那些事

來源:互聯網
上載者:User

標籤:start   weblogic   cat   info   api   原因   ogg   integer   als   

 ?java視頻轉換的兩種方式,基本上都是基於ffmpeg外掛程式,目前網上對java視頻轉換基本上都是調用外掛程式,拚命令,來實現視頻格式的轉換,然而這種方式在本地是好用的,但是在weblogic伺服器上運行時,就會發生問題,主要原因是window和linux系統不同,所使用ffmpeg外掛程式的格式有所不同.如果在Weblogic伺服器上運行,需要去ffmpeg官網下載linux版本的外掛程式.

    首先,記錄下第一種使用ffmpeg外掛程式方式進行視頻格式轉換的代碼地址:

  https://www.cnblogs.com/tohxyblog/p/6640786.html

?    其次,主要記錄下使用jave jar包進行視頻格式轉換的方法,因為調用jar包的方式,不需要考慮不同作業系統的問題,在視頻格式轉換時,jave官方給的API都是英文的,不是很好理解,需要注意的是視頻分為兩部分:音頻和映像,其中映像部分有視頻格式和視頻編碼,目前小視頻以及直播都是以H264編碼格式在解碼視頻的,所以如果需要轉換為mp4格式的視頻檔案時,如果在網站上顯示黑屏但是有聲音,主要原因就是視頻編解碼格式不匹配.所以下面我在轉換方法中運用的是flv格式,主要是因為flv不涉及此方面問題,但是缺點就是,flv產生的視頻會大於原視頻檔案,造成網路傳輸時效率降低.

 

private boolean process(String inputPath,String outputPath){

try {

logger.info("---------視頻轉換開始-------------");

File source = new File(inputPath);  

File target = new File(outputPath); 

long startTime = System.currentTimeMillis();  //擷取開始時間

AudioAttributes audio = new AudioAttributes();  

audio.setCodec("libmp3lame");  //設定編碼器  

audio.setBitRate(new Integer(64000));  //設定位元速率  

audio.setChannels(new Integer(1));  //設定聲音頻道   

audio.setSamplingRate(new Integer(22050));  //設定節錄率 

VideoAttributes video = new VideoAttributes();  

video.setCodec("flv");  //設定編碼器  

video.setBitRate(new Integer(600000)); //設定位元速率   

video.setFrameRate(new Integer(15));  //設定幀率  

video.setSize(new VideoSize(640, 480));  //設定解析度

//video.setTag("libx264");

EncodingAttributes attrs = new EncodingAttributes();  

attrs.setFormat("flv");  

attrs.setAudioAttributes(audio);  

attrs.setVideoAttributes(video);  

Encoder encoder = new Encoder();  

encoder.encode(source, target, attrs); 

long endTime = System.currentTimeMillis();    //擷取結束時間

logger.info("-------- -視頻轉換成功,耗時:"+((endTime-startTime)/1000.00)+"s----------");

return true;

} catch (Exception e) {

logger.info("視頻轉換異常:"+e.getMessage());

return false;

}

}?

java視頻轉換那些事

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.