简体   繁体   中英

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. 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. 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. I could use cv.VideoCapture functionality to read frame by frame and save the ones I need. 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. 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. Reencoding the video before is not preferred as it wastes time and possibly decreases video quality.
  4. Write a new ffmpeg filter. 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. 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.

bash script:

#!/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 ☺

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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