android捕获视频帧

Bud*_*ril 7 video android frame capture

我需要获取一个视频文件的框架(它可能在sdcard,cache dir或app dir上).我在我的应用程序中有android.media包,我有类MediaMetadataRetriever.要将第一帧放入位图,我使用代码:

public static Bitmap getVideoFrame(Context context, Uri videoUri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
        retriever.setDataSource(context, videoUri);
        return retriever.captureFrame();
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException();
    } catch (RuntimeException ex) {
        throw new RuntimeException();
    } finally {
        retriever.release();
    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.当我设置数据源时,它抛出异常(java.lang.RuntimeException:setDataSource failed:status = 0x80000000).你知道怎么让这段代码工作吗?或者你有没有使用ffmpeg或其他外部库的类似(简单)解决方案?videoUri是一个有效的uri(媒体播放器可以播放来自该URI的视频)

Rab*_*oss 13

以下适用于我:

public static Bitmap getVideoFrame(FileDescriptor FD) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(FD);
            return retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

如果您使用路径而不是filedescriptor也可以.


Pra*_*ant 6

试试这个,我已经习惯了它的工作

public static Bitmap getVideoFrame(Context context, Uri uri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(uri.toString(),new HashMap<String, String>());
        return retriever.getFrameAtTime();
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

代替uri,您可以直接传递您的网址.