直接上代码:
/**
* 使用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");
}
}