Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert video to gif in android programmatically [closed]

Tags:

android

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?

like image 640
Naskov Avatar asked May 25 '13 09:05

Naskov


1 Answers

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); 
like image 117
Sam R. Avatar answered Sep 23 '22 07:09

Sam R.