ffmpeg: Select frames at % position of video

2020-03-10 06:15发布

问题:

I'm trying to create a 2x2 tile thumbnail of a video, that contains frames from 20%, 40%, 60%, and 80% through a video. I see that I need to use a video filter, with select, but can't work out what options will do this. I want to do something like this:

ffmpeg -i video.avi -frames 1 -vf "select=not(mod(pos\,0.2)),tile=2x2" tile.png

Where position would be the position in the video from 0.0 to 1.0. How do I do this, or what are my options?

回答1:

  • Method 1: Frame intervals

    This may take some time depending on the input or return N/A for certain types of inputs (see the duration based method in this case).

    Get the total number of video frames:

    ffprobe <input> -select_streams v -show_entries stream=nb_frames -of default=nk=1:nw=1 -v quiet
    

    The command will output an integer value, for example:

    18034
    

    For the example above the frame interval is nb_frames / 5 = 18034 / 5 = 3607

    Finally the ffmpeg command is:

    ffmpeg -i <input> -filter:v "select=(gte(n\,3607))*not(mod(n\,3607)),tile=2x2" -frames:v 1 -vsync vfr -y tile.png
    
  • Method 2: Duration intervals

    Same idea as above but use a duration in seconds. This can also take a while and the reported duration may be invalid (eg: if the file is truncated).

    ffprobe <input> -select_streams v -show_entries stream=duration -of default=nk=1:nw=1 -v quiet
    

    It returns a real value like:

    601.133333
    

    Your interval is 601 / 5 ~= 120 seconds:

    ffmpeg -i <input> -filter:v "select=(gte(t\,120))*(isnan(prev_selected_t)+gte(t-prev_selected_t\,120)),tile=2x2" -frames:v 1 -y tile.png
    
  • Method 3: Seek & extract

    Seek to a specific time with -ss -i, extract a single frame and use imagemagick's montage to create the tile.

    Example output for a 10 minute countdown timer:



标签: ffmpeg