MediaMetadataRetriever.getFrameAtTime()只返回第一帧 [英] MediaMetadataRetriever.getFrameAtTime() returns only first frame

查看:5447
本文介绍了MediaMetadataRetriever.getFrameAtTime()只返回第一帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提取使用MetadataRetriever一个视频帧,并保存所有图像在的ArrayList<位图> 。我想存储所有的人都在SD卡(只用于测试目的)。

I have extracted frames from a video using MetadataRetriever, and have stored all images in an ArrayList<Bitmap>. I want to store all of them on an SD card (just for testing purposes).

但是,当我拿出一个文件夹从仿真器,并期待在保存的图像,所有的图像视频的第一帧而已。

But when I pull out a folder from the emulator and look at the saved images, all images were of the video's first frame only.

这是我从视频中提取帧:

This is how I extract the frames from the video:

File videoFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videos","sample_mpeg4.mp4");

Uri videoFileUri=Uri.parse(videoFile.toString());

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(videoFile.getAbsolutePath());
ArrayList<Bitmap> rev=new ArrayList<Bitmap>();

//Create a new Media Player
MediaPlayer mp = MediaPlayer.create(getBaseContext(), videoFileUri);

int millis = mp.getDuration();
for(int i=0;i<millis;i+=100){
   Bitmap bitmap=retriever.getFrameAtTime(i,OPTION_CLOSEST_SYNC);
   rev.add(bitmap);
}

这是我如何保存它们(我调用这个方法并传递位图的ArrayList):

And this is how I'm saving them (I'm calling this method and passing the bitmap ArrayList):

public void saveFrames(ArrayList<Bitmap> saveBitmapList) throws IOException{
    Random r = new Random();
    int folder_id = r.nextInt(1000) + 1;

    String folder = Environment.getExternalStorageDirectory()+"/videos/frames/"+folder_id+"/";
    File saveFolder=new File(folder);
    if(!saveFolder.exists()){
       saveFolder.mkdirs();
    }

    int i=1;
    for (Bitmap b : saveBitmapList){
       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

        File f = new File(saveFolder,("frame"+i+".jpg"));

        f.createNewFile();

        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

           fo.flush();
           fo.close();

        i++;
    }
    Toast.makeText(getApplicationContext(),"Folder id : "+folder_id, Toast.LENGTH_LONG).show();

}

当我拉出来一个文件夹以查看所有的帧,所有图像都是视频的第一帧。可有人请向我解释发生了什么事?

When I pull out a folder to see all frames, all of the images were of the first frame of video. Can someone please explain to me what is happening?

更新:

我试图用另一段视频。我发现,我没有收到空白图像,但它永远只能返回第一帧。

I've tried with another video. I've found that I'm not getting blank images, but it's always returning the first frame only.

MediaMetadataRetriever.getFrameAtTime(长timeUS)是只返回第一帧,但我想要检索的所有帧。我应该什么样的变化?

MediaMetadataRetriever.getFrameAtTime(long timeUS) is returning only the first frame, but I want to retrieve all frames. What changes should I make?

我要如何解决呢?

推荐答案

类MediaMetadataRetriever getFrameAt 方法接受微秒代替毫秒(第二1 /第百万),所以在你的情况下,它总是舍入下到第一帧

MediaMetadataRetriever's getFrameAt method takes in microseconds (1/1000000th of a second) instead of milliseconds, so in your case it is always rounding down to the 1st frame.

这篇关于MediaMetadataRetriever.getFrameAtTime()只返回第一帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆