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

/**
* 将上传的视频转换为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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值