简体   繁体   English

文件中列出的 FFmpeg 导出帧(包括重复项)

[英]FFmpeg export frames listed in file (including duplicates)

Problem问题

I have a list of frames that I want to extract from some video file.我有一个要从某个视频文件中提取的帧列表。 The list might look like this:该列表可能如下所示:

1,6,6,10,15,20,20,25,25,50

In reality there are thousands of frames.实际上有成千上万的帧。

How do I extract these frames into images or video in efficient way?如何以有效的方式将这些帧提取到图像或视频中?

What I have tried我试过的

  1. Using FFmpeg select filter.使用 FFmpeg 选择过滤器。 I could create huge string from file like select=eq(\\n,1)+eq(\\n,6)+... but the problem is that I cannot export duplicates this way.我可以从文件中创建巨大的字符串,例如select=eq(\\n,1)+eq(\\n,6)+...但问题是我不能以这种方式导出重复项。 Also I am not sure about the performance.我也不确定性能。
  2. Exporting all frames to folder, then copying relevant frames to another folder.将所有帧导出到文件夹,然后将相关帧复制到另一个文件夹。 This is the current solution, which is painfully slow and takes a lot of storage.这是当前的解决方案,它非常缓慢并且需要大量存储。
  3. Using python and opencv.使用 python 和 opencv。 I could use cv.VideoCapture functionality to read frame by frame and save the ones I need.我可以使用cv.VideoCapture功能逐帧读取并保存我需要的那些。 The problem is that before the extraction, I need to change framerate of the video to some constant framerate (very important).问题是在提取之前,我需要将视频的帧率更改为某个恒定的帧率(非常重要)。 That's why ffmpeg is preferred, because I could add fps filter to filterchain.这就是为什么 ffmpeg 是首选的原因,因为我可以将 fps 过滤器添加到过滤器链。 I know that it's possible to read frames with opencv by using ffmpeg backend, but I couldn't find how to change the framerate beforehand.我知道可以通过使用 ffmpeg 后端使用 opencv 读取帧,但是我找不到如何事先更改帧率。 Reencoding the video before is not preferred as it wastes time and possibly decreases video quality.之前重新编码视频不是首选,因为它浪费时间并可能降低视频质量。
  4. Write a new ffmpeg filter.编写一个新的 ffmpeg 过滤器。 While I am not in close relationship with C or ffmpeg API, I tried editing fps filter to drop frames that are not in the list.虽然我与 C 或 ffmpeg API 没有密切关系,但我尝试编辑 fps 过滤器以丢弃不在列表中的帧。 This kind of worked but I couldn't solve the problem with duplicating frames.这种工作,但我无法解决重复帧的问题。 I wonder if that's possible to do and if I should continue trying with this approach.我想知道这是否可行,我是否应该继续尝试这种方法。 Having ffmpeg filter would be probably the best option.拥有 ffmpeg 过滤器可能是最好的选择。

bash script: bash脚本:

#!/bin/bash
LST=(1 6 6 10)
TOT=${#LST[*]}
for (( i=0; i<=$(( $TOT -1 )); i++ )); do
  ffmpeg -i "input 1.mkv" -filter:v "select='eq(n,${LST[$i]})'" -vsync vfr "/tmp/img_${i}_${LST[$i]}.jpg"
done

it is not fast, but works ☺它不快,但有效 ☺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM