使用jave1.0.2.jar進行音視訊轉碼
直接上程式碼:
package com.roots.cloudserver.util;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;
import java.io.File;
/**
* 使用jave1.0.2.jar進行轉碼截幀
* @author chengjs
*
*/
public class MediaInfo {
public static boolean flag = true;
/**
* 視訊轉碼
* 因轉碼出來不是H264的視訊編碼所以此方法棄用
* @param source
* @param targetPath
* @return
*/
public static boolean transcodingToMP4(File source,String targetPath){
//File source = new File("C:/Users/Administrator/Downloads/廚房裡的爆炸案.mpg");
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();// 音訊屬性
audio.setCodec("libmp3lame");// libmp3lame 音訊編碼
audio.setBitRate(new Integer(128000));// 音訊位元率
audio.setChannels(new Integer(1));// 聲道
audio.setSamplingRate(new Integer(44100));// 取樣率
VideoAttributes video = new VideoAttributes();// 視訊屬性
video.setCodec("libxvid");// 視訊編碼
video.setBitRate(new Integer(2048000));// 視訊位元率
video.setFrameRate(new Integer(18));// 幀率 1f/s幀頻,1是目前測試比較清楚的,越大越模糊
//video.setSize(new VideoSize(1920,1080));// 視訊寬高
EncodingAttributes attrs = new EncodingAttributes();// 轉碼屬性
attrs.setFormat("mp4");// 轉碼格式
attrs.setAudioAttributes(audio);// 音訊屬性
attrs.setVideoAttributes(video);// 視訊屬性
Encoder encoder = new Encoder();// 建立解碼器
long beginTime = System.currentTimeMillis();
try {
// 獲取時長
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("獲取時長花費時間是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("視訊轉碼花費時間是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
flag = true;
} catch (IllegalArgumentException e) {
flag = false;
e.printStackTrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 音訊轉碼 轉成MP3格式
* @param source
* @param targetPath
* @return
*/
public static boolean transcodingToMP3(File source,String targetPath){
//File source = new File("C:/Users/Administrator/Downloads/廚房裡的爆炸案.mpg");
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();// 音訊屬性
audio.setCodec("libmp3lame");// libmp3lame 音訊編碼
audio.setBitRate(new Integer(128000));// 音訊位元率
audio.setChannels(new Integer(1));// 聲道
audio.setSamplingRate(new Integer(44100));// 取樣率
EncodingAttributes attrs = new EncodingAttributes();// 視訊屬性
attrs.setFormat("mp3");// 轉碼格式
attrs.setAudioAttributes(audio);// 音訊屬性
Encoder encoder = new Encoder();// 建立解碼器
long beginTime = System.currentTimeMillis();
try {
// 獲取時長
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("獲取時長花費時間是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("音訊轉碼花費時間是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
flag = true;
} catch (IllegalArgumentException e) {
flag = false;
e.printStackTrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 擷取第一幀作為縮圖
* @param source
* @param targetPath
* @return
*/
public static boolean interceptionToJPG(File source,String targetPath){
//File source = new File("C:/Users/Administrator/Downloads/火箭少女101 - 卡路里.mp4");
File target = new File(targetPath);// 轉圖片
VideoAttributes video = new VideoAttributes();// 視訊屬性
video.setCodec("mjpeg");// 圖片編碼
video.setSize(new VideoSize(1200, 800));// 設定圖片寬高
EncodingAttributes attrs = new EncodingAttributes();// 轉碼屬性
attrs.setFormat("image2");// 轉碼格式
attrs.setOffset(3f);// 設定偏移位置,即開始轉碼位置(3秒)
attrs.setDuration(0.01f);// 設定轉碼持續時間(1秒)
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
long beginTime = System.currentTimeMillis();
try {
//獲取時長
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration());
System.out.println("獲取時長花費時間是:" + (System.currentTimeMillis() - beginTime));
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("圖片轉碼花費時間是:" + (System.currentTimeMillis() - beginTime));
flag = true;
} catch (IllegalArgumentException e) {
flag = false;
e.printStackTrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
public static Long getTime(File file) throws InputFormatException, EncoderException{
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(file);
long lengthOfTime = m.getDuration()/1000;
return lengthOfTime;
}
public static void main(String[] args) {
String fileOut = "E://30.mp4";
File file2 = new File(fileOut);
boolean flag1 = transcodingToMP3(file2,"C://別人家的小孩 .mp3");
boolean flag2 = interceptionToJPG(file2,"C://別人家的小孩 .jpg");
}
}