How to convert video to gif in android programmatically

Can I convert recorded videos to GIF in Android programmatically? Can you help me with some kind of sniper or tell me how to do this?

+6
source share
2 answers
  • Import ffmpeg into your project, capture part or the whole video and convert it to gif.

    or

  • Use MediaRetriever to extract a range of frames with a simple loop over the range, and then convert to Drawable or Bitmap so you can save it.

    or

  • Take frames while playing videos with SurfaceViewHolder or SurfaceView Canvas.

+6
source

My advice: use MediaMetadataRetriever and call the 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 programmatically create an Animated Animation :

 AnimationDrawable animatedGIF = new AnimationDrawable(); animationDrawable.addFrame("FIRST FRAME", 50); animationDrawable.addFrame("SECOND FRAME", 50); ... animationDrawable.addFrame("LAST FRAME ", 50); 
+9
source

Source: https://habr.com/ru/post/945831/


All Articles