【问题标题】:Android Create Video Thumbnail at specific timeAndroid 在特定时间创建视频缩略图
【发布时间】:2014-09-05 16:50:07
【问题描述】:

我已经使用它从我的视频中创建缩略图。 代码如下所示:

videoGalleryThumbnails.add(ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(
                    videoFile.getAbsolutePath(), MediaStore.Images.Thumbnails.MINI_KIND), 500, 200));

但是创建的缩略图的时间非常糟糕。正是视频是黑色的时候。现在我没有使用完全黑色的缩略图。

如何在特定时间拍摄我的视频的缩略图?例如。 00:31 还是 01:44?

还是不可能?

我也尝试使用 MediaMetadataRetriever,但我只得到一张白色图像。代码如下所示

File tempVideoList[] = (Environment.getExternalStoragePublicDirectory(PATH_VIDEO_GALLERY))
            .listFiles();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap myBitmap=null;
for (File videoFile : tempVideoList) {
    if (videoFile.isFile()) {
        //from here
        try {
            retriever.setDataSource(videoFile.getAbsolutePath());
            myBitmap = retriever.getFrameAtTime(11); //at 11th second
        } catch (Exception ex) {
            Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
        }

    videoGalleryThumbnails.add(myBitmap);
    //to here
}

如果我用最上面的代码替换标记为“从这里”到“到这里”的代码,它就可以工作。 我还尝试了 MediaMetadataRetriever.OPTION_CLOSEST 和 OPTION_CLOSEST_SYNC 和 OPTION_NEXT_SYNC。

【问题讨论】:

  • 可以查看并分享videoFile声明吗?
  • 我编辑了我的代码,所以你看到了声明。目前看来还不错。

标签: android video time


【解决方案1】:

好的,我明白了。 MediaMetadataRetriever 绝对是正确的方法。问题是,它以微秒而不是秒为单位计算时间。解决方案如下所示:

try {
        retriever.setDataSource(videoFile.getAbsolutePath());
        int timeInSeconds = 30;
        myBitmap = retriever.getFrameAtTime(timeInSeconds * 1000000,
                    MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
    } catch (Exception ex) {
        Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
    }

videoGalleryThumbnails.add(myBitmap);

我不知道是否真的需要 OPTION_CLOSEST_SYNC,但看起来这是更好的编程方式。

感谢 William Riley 为我指明了正确的方向。

【讨论】:

  • 小改进建议:使用TimeUnit.SECONDS.toMicros(timeInSeconds) 比混淆零的数量更安全
【解决方案2】:

此代码只需稍作改动:

try {

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

//必须控制setDataSource的版本

   if (Build.VERSION.SDK_INT >= 14)
       retriever.setDataSource(video_path, new HashMap<String, String>());
   else
       retriever.setDataSource(video_path);
   int timeInSeconds = 5;
   Bitmap thumb = retriever.getFrameAtTime(timeInSeconds * 1000000,
                                MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
   imageViewThumb.setImageBitmap(thumb);
   } catch (Exception ex) {
        ex.printStackTrace();
   }

如果我们不控制“setDataSource”的版本,那么我们将得到异常。对我来说,直到版本控制才起作用。

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2023-03-17
    • 1970-01-01
    • 2014-03-14
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2014-04-14
    相关资源
    最近更新 更多