/**
* 将上传的视频转换为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;
}