public class SnippetUtil {
public static final String MP4FORM = "mp4";
public static final String MP4VIDEOCODEC = "mpeg4";
public static final String AUDIOCODEC = "libmp3lame";
/**
* 压缩视频工具类,目前默认压缩后格式为mp4,暂支持avi/mp4/flv/mov 格式直接压缩
*
* @param inAbsolutePath 视频输入路径
* @param outAbsolutePath 视频输出路径
* @return Boolean
*/
public static Boolean snippetVideo(String inAbsolutePath, String outAbsolutePath, Boolean isSnippet) {
File source = new File(inAbsolutePath);
File target = new File(outAbsolutePath);
try {
//进行压缩
if (isSnippet != null && isSnippet) {
AudioAttributes audio = new AudioAttributes();
audio.setCodec(AUDIOCODEC);
audio.setBitRate(new Integer(128000));
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(44100));
VideoAttributes video = new VideoAttributes();
video.setCodec(MP4VIDEOCODEC);
video.setBitRate(new Integer(600000));
video.setFrameRate(new Integer(22));
video.setSize(new VideoSize(320,240));// 视频宽高
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(MP4FORM);
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
return true;
} else {
//直接转换格式未mp4
Encoder encoder = new Encoder();
MultimediaInfo info = null;
info = encoder.getInfo(source);
// 获取源文件的视频信息
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(AUDIOCODEC);
audio.setBitRate(new Integer(audioBitRate));
audio.setSamplingRate(new Integer(audioSimplingRate));
audio.setChannels(new Integer(audioChannel));
VideoAttributes video = new VideoAttributes();
video.setCodec(MP4VIDEOCODEC);
video.setBitRate(new Integer(vodeoBitRate));
video.setFrameRate(new Integer(videoFrameRate));
video.setSize(videoSize);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(MP4FORM);
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
encoder.encode(source, target, attrs);
return true;
}
} catch (EncoderException e) {
e.printStackTrace();
return false;
}
}
}
关于音视频压缩的工具(JAVE)
最新推荐文章于 2022-06-14 13:44:43 发布