Is it possible to convert recorded video into GIF in Android programmatically? Can you help me with some sniped code or tell me how to do it?
Asked
Active
Viewed 1.2k times
2 Answers
12
My advice would be to use MediaMetadataRetriever and call getFrameAtTime()
method to retrieve the frames you want to extract:
// 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);
And then create a Drawable Animation programmatically:
AnimationDrawable animatedGIF = new AnimationDrawable();
animationDrawable.addFrame("FIRST FRAME", 50);
animationDrawable.addFrame("SECOND FRAME", 50);
...
animationDrawable.addFrame("LAST FRAME ", 50);

Sam R.
- 16,027
- 12
- 69
- 122
-
Can you tell me how to get all frames. what will be the loop for it.http://stackoverflow.com/questions/22684347/extract-all-video-frames-in-android can you please answer it. – Akanksha Rathore Mar 28 '14 at 14:10
-
mmRetriever.setDataSource(myVideo.getAbsolutePath()); is always crashing – Binod Singh Aug 13 '14 at 10:44
-
1Why do you create new MediaPlayer? Is it necessary? – Sergei Vasilenko Jan 27 '17 at 16:09
-
1@Delagro, it seems to be unused. Not sure why I did it. Hard to tell what I was thinking 4 years ago. – Sam R. Jan 27 '17 at 18:39
-
Works but is slow. After getting this to work I've decided to create gifs in cloud. – wakeupoj Aug 01 '20 at 14:56
8
Import
ffmpeg
in your project, grab the portion or entire video and convert it to gif.or
Use
MediaRetriever
to extract range of frames with simple looping through the range and then converting to Drawable or Bitmap so you can save it.or
Grab the frames while the video is playing from the
SurfaceViewHolder
orSurfaceView
Canvas.

Nikola Despotoski
- 49,966
- 15
- 119
- 148