码迷,mamicode.com
首页 > 编程语言 > 详细

Java利用Jave转换视频格式为3gp

时间:2015-06-12 17:27:32      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:java   jave   

/**
* 将上传的视频转换为3gp标清格式

* @param resourceId
* @return
*/
public static Map<String, Object> to3GP(String resourceId) {
String sourcePath = "E:";
String sourceFilePath = sourcePath + "/" + resourceId;
logger.info("--------------------sourceFilePath:" + sourceFilePath);
String targetSourceId = (resourceId.substring(0,resourceId.lastIndexOf(".")))+ ".3gp";
String targetFilePath = sourcePath + "/" + targetSourceId;
logger.info("--------------------targetFilePath:" + targetFilePath);


Map<String, Object> returnMap = new HashMap<String, Object>();
File source = new File(sourceFilePath);
File target = new File(targetFilePath);
Encoder encoder = new Encoder();
MultimediaInfo info=null;
if(!source.exists()){
logger.info("source not exists");
}
try {
info = encoder.getInfo(source);
} catch (Exception e) {
logger.error(e);
}
// 获取源文件的视频信息
VideoInfo videoInfo = info.getVideo();
// int vodeoBitRate=videoInfo.getBitRate(); //总比特率
// int videoFrameRate=(int)videoInfo.getFrameRate();//帧速率
// String videoDecoder=videoInfo.getDecoder();
VideoSize videoSize = videoInfo.getSize();



// 获取源文件的音频信息
AudioInfo audioInfo = info.getAudio();
// int audioBitRate=audioInfo.getBitRate();//比特率
// String audioDecoder=audioInfo.getDecoder();
int audioChannel = audioInfo.getChannels();// 频道
int audioSimplingRate = audioInfo.getSamplingRate();// 音频采样频率


AudioAttributes audio = new AudioAttributes();
audio.setCodec("libfaac");
audio.setBitRate(new Integer(8));
audio.setSamplingRate(new Integer(audioSimplingRate / 2));
audio.setChannels(new Integer(audioChannel));


VideoAttributes video = new VideoAttributes();
video.setCodec("mpeg4");
video.setBitRate(new Integer(100000));
video.setFrameRate(new Integer(16));
video.setSize(new VideoSize((int) (videoSize.getWidth() / 4),(int) (videoSize.getHeight() / 4)));


EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("3gp");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);


try {
encoder.encode(source, target, attrs);
} catch (Exception e) {
logger.error(e);
targetSourceId="";
}

logger.info("--------------------sourceSize:" + source.length());
logger.info("--------------------targetSize:" + target.length());
returnMap.put("resourceID", targetSourceId);
Integer resourceSize=(int) target.length();
returnMap.put("resourceSize", resourceSize);
return returnMap;
}

Java利用Jave转换视频格式为3gp

标签:java   jave   

原文地址:http://blog.csdn.net/yj_vs_gy/article/details/46471567

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!