如何将7秒视频转换为Gif Android [英] How to convert 7sec Video to Gif Android
问题描述
我正在制作一个将拍摄视频剪辑转换为Gif的应用程序。我想知道是否有一个可以在Android上直接将视频转换为GIF的文件。
我已经尝试提取视频的所有帧并对其进行编码成为一个gif,但我只得到了视频的第一帧。我的代码如下:
public static final int GIF_DELAY_BETWEEN_FRAMES = 100;
public static final float SPEED_RATIO = 1.1f;
Uri videoFileUri = Uri.parse(mOutputFile.toString());
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(mOutputFile.getAbsolutePath());
rev = new ArrayList< Bitmap>();
MediaPlayer mp = MediaPlayer.create(RecorderActivity.this,videoFileUri);
int millis = mp.getDuration(); (int i = 1000000; i< millis * 1000; i + = 1000000){
Bitmap bitmap = retriever.getFrameAtTime(i,FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
rev.add(bitmap);
}
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(Environment.getExternalStorageDirectory()+/test.gif);
outStream.write(generateGIF());
outStream.close();
} catch(Exception e){
e.printStackTrace();
}
public byte [] generateGIF(){$ b $ b ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.setDelay((int)(GIF_DELAY_BETWEEN_FRAMES *(1 / SPEED_RATIO)));
encoder.start(bos);
for(Bitmap bitmap:rev){
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
请帮助我们...谢谢
查看JMF(Java Media Framework)。这将花费你一些时间
学习基础知识。那么你将能够做到这一点。我现在没有代码
样本,但据我记得你必须创建播放器,
然后从它适当的控件检索,它提供您访问
到帧。 / p>
这是一个开始的例子:
http://khemsoi.blogspot.com/2006/03/jmf-frame-grabber.html
Am making an app that convert shot video clips to Gif. i was wondering if there was a libary that directly converts videos to gifs on the Fly on Android.
I've tried to extract all the frames of the video and encode them into a gif but am only getting the the first frame of the video. my code is below:
public static final int GIF_DELAY_BETWEEN_FRAMES = 100;
public static final float SPEED_RATIO = 1.1f;
Uri videoFileUri=Uri.parse(mOutputFile.toString());
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(mOutputFile.getAbsolutePath());
rev = new ArrayList<Bitmap>();
MediaPlayer mp = MediaPlayer.create(RecorderActivity.this, videoFileUri);
int millis = mp.getDuration();
for(int i=1000000;i<millis*1000;i+=1000000){
Bitmap bitmap = retriever.getFrameAtTime(i,FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
rev.add(bitmap);
}
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/test.gif");
outStream.write(generateGIF());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
public byte[] generateGIF() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.setDelay((int)(GIF_DELAY_BETWEEN_FRAMES * (1 / SPEED_RATIO)));
encoder.start(bos);
for (Bitmap bitmap : rev) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
Please help me guys...thanks
Take look on JMF (Java Media Framework). It will take you some time to learn basics. Then you will able to do this. I do not have a code sample with me now but as far as I remember you have to create player, then retrieve from it appropriate "control" that provides you access to frames.
Here is an example for start: http://khemsoi.blogspot.com/2006/03/jmf-frame-grabber.html
FROM Lib/API for create small gif from video?
这篇关于如何将7秒视频转换为Gif Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!