[Java] 纯文本查看 复制代码/**
* [url=home.php?mod=space&uid=952169]@Param[/url] gifPath gif图片的存储路径
* @param begin 开始时间
* @param end 结束时间
* @param fps
* @param speed
* @param gifWidth gif的导出宽度
* @param gifHeight gif的导出长度
* @param videoState 视频是横屏还是竖屏的
* @param needRoate 是否需要旋转
* @param rotateAngle 旋转角度
*/
public void encoder(final String gifPath, final long begin, final long end, final int fps,
final int speed, final int gifWidth, final int gifHeight, final int videoState, final boolean needRoate, final int rotateAngle) {
if (begin > duration) {
// throw new RuntimeException("开始时间不能大于视频时长");
callBack.erroCallBack("开始时间不能大于视频时长");
return;
}
if (end <= begin) {
// throw new RuntimeException("开始时间大于结束时间");
callBack.erroCallBack("开始时间大于结束时间");
return;
}
Thread thread = new Thread() {
@Override
public void run() {
super.run();
long endTime = duration;
if (end < duration) {
endTime = end;
}
long time1 = System.currentTimeMillis();
videoExtractor.seekTo(begin * 1000, trackIndex); // 精确定位到指定帧(定位到指定开始时间帧)
FastYUVtoRGB fastYUVtoRGB = new FastYUVtoRGB(context);
String mime = format.getString(MediaFormat.KEY_MIME);
MediaCodec videoDecoder = null;
try {
videoDecoder = MediaCodec.createDecoderByType(mime);
} catch (IOException e) {
e.printStackTrace();
}
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
int width = format.getInteger(MediaFormat.KEY_WIDTH);
int height = format.getInteger(MediaFormat.KEY_HEIGHT);
videoDecoder.configure(format, null, null, 0);
videoDecoder.start();
GIFEncoder encoder = null;
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
int f = fps;
if (f <= 0) {
f = 15;
}
int s = speed;
if (s <= 0) {
s = f;
}
long frameTime = 1000 / f;
long startTime = begin;
while (true) {
int run = extractorVideoInputBuffer(videoExtractor, videoDecoder);
if (run == 1) {
int outIndex = videoDecoder.dequeueOutputBuffer(info, 500000);
if (outIndex >= 0) {
long time = info.presentationTimeUs / 1000;
if (time >= begin && time <= endTime) {
if (time >= startTime) {
Image image = videoDecoder.getOutputImage(outIndex); // 输出
Bitmap bitmap = fastYUVtoRGB.convertYUVtoRGB(getDataFromImage(image), width, height);
int videoImageState = MImageUtil.getPicDirection(image.getHeight(), image.getWidth());
if (needRoate) { // 是否需要旋转,解决自动旋转错误的问题
bitmap = PhotoBitmapUtils.rotateBitmap(bitmap, rotateAngle);
} else {
// LogUtil.i("视频导出图片:" + videoImageState);
// LogUtil.i("原视频图片:" + videoState);
if (videoImageState != videoState) { // 横竖屏不一致,默认自动旋转90°
bitmap = PhotoBitmapUtils.rotateBitmap(bitmap, 90);
}
}
if (gifWidth != -1 && gifHeight != -1) { // 创建新的位图
bitmap = Bitmap.createScaledBitmap(bitmap, gifWidth, gifHeight, true);
} else {
bitmap = Bitmap.createScaledBitmap(bitmap, width / 4, height / 4, true);
}
if (encoder == null) {
encoder = new GIFEncoder();
encoder.setFrameRate(s);
encoder.init(bitmap);
encoder.start(gifPath);
} else {
encoder.addFrame(bitmap);
}
int p = (int) ((startTime - begin) * 100 / (endTime - begin));
LogUtil.d("p = " + p);//进度
if (callBack != null) {
callBack.currentProgress(p);
}
startTime += frameTime;
}
}
videoDecoder.releaseOutputBuffer(outIndex, true /* Surface init */);
if (time >= endTime) {
break;
}
}
} else if (run == -1) {
break;
}
}
if (encoder != null) {
encoder.finish();
}
LogUtil.d("encoder->time = " + (System.currentTimeMillis() - time1));
LogUtil.d("over");
LogUtil.d("gif_path:" + gifPath);
if (callBack != null) {
callBack.onSuccess(System.currentTimeMillis() - time1, gifPath);
}
videoDecoder.stop();
videoDecoder.release();
MFileUtil.clearAllCache(context); // 清除缓存
}
};
thread.start();
}