【发布时间】:2013-05-25 09:12:50
【问题描述】:
是否可以在 Android 中以编程方式将录制的 video 转换为 GIF? 你能帮我一些狙击代码或告诉我怎么做吗?
【问题讨论】:
标签: android
是否可以在 Android 中以编程方式将录制的 video 转换为 GIF? 你能帮我一些狙击代码或告诉我怎么做吗?
【问题讨论】:
标签: android
我的建议是使用MediaMetadataRetriever 并调用getFrameAtTime() 方法来检索您要提取的帧:
// Get your video file
File myVideo = new File
(Environment.getExternalStorageDirectory().getAbsolutePath(),
"YOUR_FILE.mp4");
// URI to your video file
Uri myVideoUri = Uri.parse(myVideo.toString());
// MediaMetadataRetriever instance
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
mmRetriever.setDataSource(myVideo.getAbsolutePath());
// Array list to hold your frames
ArrayList<Bitmap> frames = new ArrayList<Bitmap>();
//Create a new Media Player
MediaPlayer mp = MediaPlayer.create(getBaseContext(), myVideoUri);
// Some kind of iteration to retrieve the frames and add it to Array list
Bitmap bitmap = mmRetriever.getFrameAtTime(TIME IN MICROSECONDS);
frames.add(bitmap);
然后以编程方式创建Drawable Animation:
AnimationDrawable animatedGIF = new AnimationDrawable();
animationDrawable.addFrame("FIRST FRAME", 50);
animationDrawable.addFrame("SECOND FRAME", 50);
...
animationDrawable.addFrame("LAST FRAME ", 50);
【讨论】:
在您的项目中导入ffmpeg,抓取部分或整个视频并将其转换为 gif。
或
使用MediaRetriever 提取帧范围,只需在范围内循环,然后转换为可绘制或位图,以便您保存。
或
在视频播放时从SurfaceViewHolder 或SurfaceView 画布中抓取帧。
【讨论】: