从 Android 中的 MP4 文件中提取特定秒的缩略图 [英] Extract Thumbnail for specific second from MP4 file in Android

查看:32
本文介绍了从 Android 中的 MP4 文件中提取特定秒的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mp4 文件列表,我需要为每个文件提取缩略图.

I have a list of mp4 files that i need to extract a thumbnail for each one.

  1. 缩略图必须是 Base64 格式

  1. The thumbnail must be in Base64 format

缩略图具有特定的大小,将作为方法参数提供

The thumbnail has a specific size which will be provided as a method parameter

必须从文件中间的帧中提取(例如,如果视频时长为 10 秒,则缩略图必须从第 5 秒的帧中提取.

It must be extracted from the frame in the middle of the file (e.g. if the video duration is 10s then the thumbnail must be from the frame in 5th second.

1 和 2 目前已实现,但我不知道如何做 3.

1 and 2 are currently achieved but I'm not sure how to do 3.

这是我的代码:

public static String getVideoDrawable(String path, int height, int width) throws OutOfMemoryError{

    try {
        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, android.provider.MediaStore.Images.Thumbnails.MINI_KIND);
        bitmap = Bitmap.createScaledBitmap(bitmap, height, width, false);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();

        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

推荐答案

为此,您需要使用 MediaMetadataRetriever.

MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    try {
        metadataRetriever.setDataSource(path);
        String duration=metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long time=Long.valueOf(duration)/2;
        Bitmap bitmap = metadataRetriever.getFrameAtTime(time,MediaMetadataRetriever.OPTION_NEXT_SYNC);
    //now convert to base64
    } catch (Exception ex) {
    }

http:///developer.android.com/intl/es/reference/android/media/MediaMetadataRetriever.html#getFrameAtTime%28long,%20int%29

这篇关于从 Android 中的 MP4 文件中提取特定秒的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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