gpt4 book ai didi

android - 将视频文件转换为 gif

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:25 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何在 android 中将视频文件转换为 gif。我在网上找到了一个类,AnimatedGifEncoder 类 ( https://github.com/nbadal/android-gif-encoder/blob/master/GifEncoder.java )。但它似乎并没有真正起作用。我在 AsnycTask 中包装了视频到 gif 的转换过程。代码如下所示:

@Override
protected String doInBackground(File[] files) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(files[0].getPath());
} catch (Exception e){
Log.i("VMPF", e.toString());
}
//int framesRate = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE));
int duration = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
Log.i("gifduration","" + duration);
//Log.i("giffps","" + framesRate);
int millisPerframe = 1000/24;
int timeAt = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.setDelay(0);
encoder.setRepeat(100);
encoder.start(bos);
while (timeAt < duration){
encoder.addFrame(mmr.getFrameAtTime(timeAt));
timeAt += millisPerframe;
Log.i("wenfhluwhru", timeAt+"");
}
encoder.finish();
Log.i("wenfhluwhru", "loop finished");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(path);
outStream.write(bos.toByteArray());
outStream.close();
Log.i("wenfhluwhru", "outstream closed");
return path;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}

我的代码有问题吗(我假设 AnimatedGifEncoder 类没有问题)?我还考虑过将文件转换为 byte[] 并通过 Glidebyte[] 提交到 GifImageView code> 但我愿意猜测视频的 byte[] 与 gif 文件的不同(如果我错了,请告诉我)?

最佳答案

您可以在不同的时间找到帧并从中创建可绘制的动画

public void getGIF(){
int counter = 0;
Bitmap b = mmRetriever.getFrameAtTime(counter);
while(counter < duration && b != null){
frame = new BitmapDrawable(getResources(), b);
animationDrawable.addFrame(frame, 50);
counter += incrementor;
b = mmRetriever.getFrameAtTime(counter);
}
}

关于android - 将视频文件转换为 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51067495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com