从视频中获取多个缩略图

2024-02-15

我在用着MediaMetadataRetriever检索视频中特定时间的缩略图。这就是我实现这一目标的方法:

MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    try {
        metadataRetriever.setDataSource(MainActivity.this, Uri.parse("android.resource://packageName/raw/"+"test"));
        String duration=metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long time = Long.valueOf(duration)/3;
        Bitmap bitmap1 = metadataRetriever.getFrameAtTime(time,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        imgone.setImageBitmap(bitmap1);

    }catch (Exception ex) {
        Toast.makeText(MainActivity.this, String.valueOf(ex), Toast.LENGTH_SHORT).show();
        }

这将按预期返回位图/缩略图,问题是如果我想在视频中的不同时间获取多个缩略图,如下所示:

MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    try {
        metadataRetriever.setDataSource(MainActivity.this, Uri.parse("android.resource://packageName/raw/"+"test"));
        String duration=metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long time = Long.valueOf(duration)/3;
        long time2 = time+time;
        long time3 = time+time+time;
        Bitmap bitmap1 = metadataRetriever.getFrameAtTime(time,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        Bitmap bitmap2 = metadataRetriever.getFrameAtTime(time2,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        Bitmap bitmap3 = metadataRetriever.getFrameAtTime(time3,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
        imgone.setImageBitmap(bitmap1);
        imgtwo.setImageBitmap(bitmap2);
        imgthree.setImageBitmap(bitmap3);

    }catch (Exception ex) {
        Toast.makeText(MainActivity.this, String.valueOf(ex), Toast.LENGTH_SHORT).show();
        }

然后它仍然只返回相同的缩略图,我不确定是否是因为视频只有一个缩略图或什么,但我尝试了不同的视频文件,但结果相同。

我尝试过改变MediaMetadataRetriever.OPTION_CLOSEST_SYNC所有可用选项,但结果仍然相同。

我不确定是否FFMPEG对此会是更好的选择吗?


整整一年后,我发现我从未提供过答案。

在最初的问题中,我想检索 3 个缩略图,最终检索到了 5 个。我还提到,我不确定 FFmpeg 是否是一个合适的选择,这正是我所使用的。

So, in OnCreate,我确保支持 FFmpeg,然后执行以下操作:

if (FFmpeg.getInstance(getApplication()).isSupported()) {
      @SuppressLint("SimpleDateFormat")
      //ffmpeg expects the time format to be "00:00:00"
      Format formatter = new SimpleDateFormat("00:" + "mm:ss.SS");
      //Get the duration of the video
      long duration = player.getDuration();
      //Since I want 5 thumbnails, I divide the duration by 6 to get the first thumbnail position 
      long img1 = duration / 6;
      //I format the first thumbnail time since ffmpeg expects "00:00:00" format
      String firstTumbTime = formatter.format(img1);

      //Scale the size of the thumbnail output (this can be improved/changed to your liking)
      String scaledSize = displayMetrics.widthPixels / 7 + ":" + displayMetrics.heightPixels / 7;
      //Set ffmpeg command (notice that I set vframes to one, since I only want 1 thumbnail/image)
      String[] a = {"-ss", firstTumbTime, "-i", mStringFilePath, "-vframes", "1", "-s", scaledSize, imageThumbsDirectory + "/" + "thumb1.bmp"};
      //start ffmpeg asynctask for the first thumbnail
      ExecuteThumbFFMPEG(a);

}  else {
     Toast.makeText(TestNewPlayer.this, "Your device doesn't support FFMPEG...", Toast.LENGTH_SHORT).show();
}

上面代码中的注释解释了一切,现在这是我的ExecuteThumbFFMPEG method.

public void ExecuteThumbFFMPEG(String[] command) {

    ffmpegImages = FFmpeg.getInstance(this).execute(command, new ExecuteBinaryResponseHandler() {

        @Override
        public void onStart() {
            //ffmpeg started
        }

        @Override
        public void onProgress(String message) {
            //get ffmpeg progress
        }

        @Override
        public void onFailure(String message) {
            //ffmpeg failed
        }

        @Override
        public void onSuccess(String message) {
            //first thumbnail saved successfully, now to get the other 4

            //Scale the thumbnail output (Same as above)
            String scaledSize = displayMetrics.widthPixels / 7 + ":" + displayMetrics.heightPixels / 7;

            try {
                //I first set the path/name for each thumbnail, this will also be used to check if the thumbnail is available or if we should get it
                String imgPath1 = imageThumbsDirectory + "/" + "thumb1.bmp";
                String imgPath2 = imageThumbsDirectory + "/" + "thumb2.bmp";
                String imgPath3 = imageThumbsDirectory + "/" + "thumb3.bmp";
                String imgPath4 = imageThumbsDirectory + "/" + "thumb4.bmp";
                String imgPath5 = imageThumbsDirectory + "/" + "thumb5.bmp";

                //Set the format again (same as above)
                @SuppressLint("SimpleDateFormat")
                Format formatter = new SimpleDateFormat("00:" + "mm:ss.SS");

                //Get the length of the video
                long duration = Player.getDuration();

                //Divide the length of the video by 6 (same as above)
                long time = duration / 6;

                //Since I want 5 thumbnails evenly distributed throughout the video
                //I use the video length divided by 6 to accomplish that
                long img2 = time + time;
                long img3 = time + time + time;
                long img4 = time + time + time + time;
                long img5 = time + time + time + time + time;

                //Format the time (calculated above) for each thumbnail I want to retrieve
                String Img2Timeformat = formatter.format(img2);
                String Img3Timeformat = formatter.format(img3);
                String Img4Timeformat = formatter.format(img4);
                String Img5Timeformat = formatter.format(img5);

                //Get reference to the thumbnails (to see if they have been created before)
                File fileimgPath1 = new File(imgPath1);
                File fileimgPath2 = new File(imgPath2);
                File fileimgPath3 = new File(imgPath3);
                File fileimgPath4 = new File(imgPath4);
                File fileimgPath5 = new File(imgPath5);

                //If thumbnail 1 exist and thumbnail 2 doesn't then we need to get thumbnail 2
                if (fileimgPath1.exists() && !fileimgPath2.exists()) {
                    //Get/decode bitmap from the first thumbnail path to be able to set it to our ImageView that should hold the first thumbnail
                    Bitmap bmp1 = BitmapFactory.decodeFile(imgPath1);
                    //Set the first thumbnail to our first ImageView
                    imgone.setImageBitmap(bmp1);
                    //Set the ffmpeg command to retrieve the second thumbnail
                    String[] ffmpegCommandForThumb2 = {"-ss", Img2Timeformat, "-i", mStringFilePath, "-vframes", "1", "-s", scaledSize, imageThumbsDirectory + "/" + "thumb2.bmp"};
                    //Start ffmpeg again, this time we will be getting thumbnail 2
                    ExecuteThumbFFMPEG(ffmpegCommandForThumb2);
                }

                //If thumbnail 2 exist and thumbnail 3 doesn't then we need to get thumbnail 3
                if (fileimgPath2.exists() && !fileimgPath3.exists()) {
                    //Get/decode bitmap from the second thumbnail path to be able to set it to our ImageView that should hold the second thumbnail
                    Bitmap bmp2 = BitmapFactory.decodeFile(imgPath2);
                    //Set the second thumbnail to our second ImageView
                    imgTwo.setImageBitmap(bmp2);
                    //Set the ffmpeg command to retrieve the third thumbnail
                    String[] ffmpegCommandForThumb3 = {"-ss", Img3Timeformat, "-i", mStringFilePath, "-vframes", "1", "-s", scaledSize, imageThumbsDirectory + "/" + "thumb3.bmp"};
                    //Start ffmpeg again, this time we will be getting thumbnail 3
                    ExecuteThumbFFMPEG(ffmpegCommandForThumb3);
                }

                ////If thumbnail 3 exist and thumbnail 4 doesn't then we need to get thumbnail 4
                if (fileimgPath3.exists() && !fileimgPath4.exists()) {
                    //Get/decode bitmap from the third thumbnail path to be able to set it to our ImageView that should hold the third thumbnail
                    Bitmap bmp3 = BitmapFactory.decodeFile(imgPath3);
                    //Set the third thumbnail to our third ImageView
                    imgThree.setImageBitmap(bmp3);
                    //Set the ffmpeg command to retrieve the fourth thumbnail
                    String[] ffmpegCommandForThumb4 = {"-ss", Img4Timeformat, "-i", mStringFilePath, "-vframes", "1", "-s", scaledSize, imageThumbsDirectory + "/" + "thumb4.bmp"};
                    //Start ffmpeg again, this time we will be getting thumbnail 4
                    ExecuteThumbFFMPEG(ffmpegCommandForThumb4);
                }

                ////If thumbnail 4 exist and thumbnail 5 doesn't then we need to get thumbnail 5
                if (fileimgPath4.exists() && !fileimgPath5.exists()) {
                    //Get/decode bitmap from the first fourth path to be able to set it to our ImageView that should hold the fourth thumbnail
                    Bitmap bmp4 = BitmapFactory.decodeFile(imgPath4);
                    //Set the fourth thumbnail to our fourth ImageView
                    imgFour.setImageBitmap(bmp4);
                    //Set the ffmpeg command to retrieve the last thumbnail
                    String[] ffmpegCommandForThumb5 = {"-ss", Img5Timeformat, "-i", mStringFilePath, "-vframes", "1", "-s", scaledSize, imageThumbsDirectory + "/" + "thumb5.bmp"};
                    //Start ffmpeg again, this time we will be getting thumbnail 5
                    ExecuteThumbFFMPEG(ffmpegCommandForThumb5);
                }

                //If thumbnail 5 exist, then we are done and we need to set it to our ImageView
                if (fileimgPath5.exists()) {
                    Bitmap bmp5 = BitmapFactory.decodeFile(imgPath5);
                    imgFive.setImageBitmap(bmp5);
                }


            } catch (Exception ex) {
                Toast.makeText(Player.this, String.valueOf(ex), Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFinish() {
            //ffmpeg is done
        }


    });

}

当用户退出时Activity or OnDestroy被调用,所有缩略图都应该被删除,我通过调用以下方法来做到这一点:

DeleteThumbs.deleteAllThumbnails(getBaseContext());

这里是DeleteThumbs用于删除所有缩略图/图像的类

class DeleteThumbs {

    @SuppressWarnings("unused")
    static void deleteAllThumbnails(Context baseContext){
        //Directory where all the thumbnails are stored
        File imageThumbsDirectory = baseContext.getExternalFilesDir("ThumbTemp");
        //Path to each thumbnail
        File f1 = new File(imageThumbsDirectory + "/" + "thumb1.bmp");
        File f2 = new File(imageThumbsDirectory + "/" + "thumb2.bmp");
        File f3 = new File(imageThumbsDirectory + "/" + "thumb3.bmp");
        File f4 = new File(imageThumbsDirectory + "/" + "thumb4.bmp");
        File f5 = new File(imageThumbsDirectory + "/" + "thumb5.bmp");

        boolean d1 = f1.delete();
        boolean d2 = f2.delete();
        boolean d3 = f3.delete();
        boolean d4 = f4.delete();
        boolean d5 = f5.delete();
    }

}

由于我们知道每个缩略图的名称,因此很容易一次将它们全部删除。

这为我提供了 5 个缩略图,这些缩略图经过缩放以减少加载时间ImageView的。因为我将视频的持续时间除以 6,所以我得到了 5 张在整个视频中均匀“分布”的图像。

NOTE:

这可以通过将图像缓存到内存中或使用 picasso 或 glide 等库来为我们处理图像加载来改进。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从视频中获取多个缩略图 的相关文章

随机推荐

  • Python-生成特定自相关的数组

    我有兴趣生成一个长度为 N 的数组 或 numpy 系列 它将在滞后 1 处表现出特定的自相关性 理想情况下 我还想指定均值和方差 并从 多 正态分布中提取数据 但最重要的是 我想指定自相关 如何使用 numpy 或 scikit lear
  • 为什么我会收到“构造函数未定义”错误?

    为什么下面的代码会产生编译错误 构造函数AA 未定义 它不应该调用默认构造函数吗 public class A public A public class AA extends A public AA int aa public class
  • IPC::Run - 检测早产儿退出和关闭管道

    我想使用 IPC Run 通过孩子的 STDIN STDOUT 和 STDERR 开始 泵送 完成 与孩子进行通信 似乎有效 我想知道如何检测 儿童过早退出 例如由错误引起 水管被孩子关闭 The pump抛出一个die出现错误时 或将其消
  • 如何在 Python 中从 URL 读取图像数据?

    当我们处理本地文件时 我想做的事情相当简单 但当我尝试使用远程 URL 来执行此操作时 问题就出现了 基本上 我正在尝试从从 URL 提取的文件创建 PIL 图像对象 当然 我总是可以只获取 URL 并将其存储在临时文件中 然后将其打开到图
  • 如何从生产中删除 console.log?

    如何从生产中删除所有 console log 此代码无法运行laravel mix 4 x webpack mix js mix js resources js app js public js if mix inProduction mi
  • android 无法启动模拟器:无法运行程序

    我已经在Ubuntu上成功安装了android SDK和android eclipse插件 一切都工作正常 直到我删除 Ubuntu 并安装 Linux Mint 我再次安装了 SDK 并使用了我在 Ubuntu 上使用的相同 Eclips
  • “未提供任何值的属性”错误 - UrlFetchApp

    我有以下错误 未提供任何值的属性 错误来自行 var content UrlFetchApp fetch url getContentText 这是我的代码 function getArray var newData new Array v
  • 在golang中一起执行bash echo和nc [重复]

    这个问题在这里已经有答案了 这可能是一个简单的问题 在 Linux 机器上工作 我正在尝试从go程序 我有一个服务器正在监听请求 但这行代码给我带来了问题 cmd exec Command echo n hello nc localhost
  • Linux:在 docker 容器中找不到现有共享库

    我尝试在 docker 容器内设置 FastRTPS 我编写了一个 Dockerfile 它从源代码构建 FastRTPS 及其依赖项 并安装库并提供示例 但这些例子不起作用 opt usr local examples C HelloWo
  • 如何设置android wifi热点的高级设置

    我通过以下代码打开便携式 wifi 热点 private void createWifiAccessPoint WifiManager wifiManager WifiManager getBaseContext getSystemServ
  • KQL 正则表达式行提取

    我有一个名为 AdditionalExtensions 的专栏 其中包含以下内容 start Aug 13 2022 20 42 17 logver700060366 ad 我想添加正则表达式搜索以仅提取并显示日期和时间 如下所示 2022
  • Jar 文件名形成 java 代码

    我想从我的 java 代码中确定 jar 文件名 我在谷歌中找到了很多解决方案 但没有任何效果 只是为了看看我在这里尝试了什么 这是一个 stackoverflow 论坛 其中发布了一堆解决方案 堆栈溢出 https stackoverfl
  • UINavigationBar:拦截后退按钮和后退滑动手势

    我有一个 UINavigationBar 可以拦截后退按钮点击 如果有未保存的更改 则会提醒用户 这是基于中提出的解决方案UINavigationController 和 UINavigationBarDelegate ShouldPopI
  • 条目 '' 已添加错误

    我在 ASP net 中有一个 web config 给我配置错误
  • 如何使用 Promise.all 并以对象作为输入

    我一直在开发一个供自己使用的小型 2D 游戏库 但遇到了一些问题 库中有一个名为 loadGame 的特定函数 它将依赖项信息作为输入 资源文件和要执行的脚本列表 这是一个例子 loadGame root source folder for
  • 使用SyncAdapters时登录两次

    我正在使用创建一个新的 Android 应用程序SyncAdapter处理数据库同步 我已准备就绪 应用程序工作正常 但我注意到我登录了两次 首次登录发生在AuthenticatorActivity类 它扩展了AccountAuthenti
  • Android 无法在设备上安装 HelloWorld.apk (null) 错误

    我是安卓新手 当我在 Eclipse 中运行 Android 应用程序时 我在控制台中收到以下消息 2011 03 08 12 57 35 HelloWorld 2011 03 08 12 57 35 HelloWorld Android
  • WIX - 如何使用删除文件

    我们有多个配置文件 根据所选的环境 将复制正确的文件 并删除其余文件 我在用RemoveFile但这不会删除任何内容 或者可能是我没有正确使用它 有人可以举个删除的例子吗 config from INSTALLDIR 在安装过程中 复制所有
  • 避免 p:treeTable 的节点在更新后崩溃

    我有一个p treeTable以一种形式和一种p dialog以另一种形式从哪里p dialog我将数据添加到p treeTable 提交时h commandButton我添加更新的对话框p treeTable为了看到添加的节点 问题是用户
  • 从视频中获取多个缩略图

    我在用着MediaMetadataRetriever检索视频中特定时间的缩略图 这就是我实现这一目标的方法 MediaMetadataRetriever metadataRetriever new MediaMetadataRetrieve