gif android. 耗资源,Android MP4转GIF

[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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值