Drop every even or odd frames using FFmpeg?

30,275

Solution 1

To work accurately, first convert the video to RAW YUV bitstream (if it is not already) by:

ffmpeg -i input.mp4 -an -vcodec rawvideo -pix_fmt yuv420p rawbitstream.yuv

Next step: The select filter takes an expression, where n is the frame number.

ffmpeg -r 2 -s WxH -i rawbitstream.yuv -filter:v select="mod(n-1\,2)" \
-c:v rawvideo -r 1 -format rawvideo -pix_fmt yuv420p -an odd.yuv

ffmpeg -r 2 -s WxH -i rawbitstream.yuv -filter:v select="not(mod(n-1\,2))" \
-c:v rawvideo -r 1 -format rawvideo -pix_fmt yuv420p -an even.yuv

To have ffmpeg not duplicate frames, you have to force half of the framerate of your input - so you set "2" as the input and "1" to the output. Don't forget to replace the WxH with the actual dimensions of your clip because the raw bitstream doesn't have a header that carries this information.

Instead of the above, another possibility would be to add the setpts filter to set new timestamps for the output. But be careful since it drops frames not accurately. Here, 25 is the actual output frame rate you want:

ffmpeg -i input.mp4 -filter:v select="mod(n-1\,2)",setpts="N/(25*TB)" \
-c:v rawvideo -r 12.5 -format rawvideo -pix_fmt yuv420p -an odd.yuv

ffmpeg -i input.mp4 -filter:v select="not(mod(n-1\,2))",setpts="N/(25*TB)" \
-c:v rawvideo -r 12.5 -format rawvideo -pix_fmt yuv420p -an even.yuv

You can of course choose another pixel format (any of ffmpeg -pix_fmts). Make sure that when reading the file you know the pixel size and pixel format:

ffmpeg -f rawvideo -s:v 1280x720 -pix_fmt yuv420p input.yuv …

Solution 2

If your ffmpeg was built with the AviSynth flag, then I believe you can pass an .avs file.

You can check by running ffmpeg and looking for --enable-avisynth in the configuration data. --enable-avisynth

If it's there you can use it like so: ffmpeg -i blahEven.avs blahEven.yuv.

Where blahEven.avs is simply:

ffvideosource("blah.yuv").SelectEven()

For odd frames, use SelectOdd().

For more advanded usage, see the SelectEvery documentation.

Share:
30,275

Related videos on Youtube

Mark
Author by

Mark

Updated on September 18, 2022

Comments

  • Mark
    Mark almost 2 years

    Is there an accurate way to take raw video and extract from it a new video that contains only odd frames or even frames (by choice)?

    For example:

    I have "blah.yuv" with 400 frames (0-399). I want to create "blahOdd.yuv" that contains frames 1-399 (1,3,5,7...399) and "blahEven" that contains frames 0-398 (0,2,4,6...398).

    Any ideas how to do it using only FFmpeg?

  • slhck
    slhck about 11 years
    -vf is an alias of -filter:v. I can't reliably test it now, but will look into this later when I'm back on my machine. Maybe the tinterlace filter can do the same?
  • Mark
    Mark about 11 years
    I've tried '-r 2 -i blah.yuv -r 1' but it shows me an error Option framerate not found.
  • slhck
    slhck about 11 years
    Ah sorry.. Scratch that, this doesn't work (anymore?) and only for images.
  • slhck
    slhck about 11 years
    I think I figured out why. If you use the select filter to drop frames, ffmpeg will still try to create a video with the original input framerate, thus doubling the frames. Try specifying half the frame rate, or using the setpts filter.
  • Mark
    Mark about 11 years
    The ‘setpts="N/(25*TB)”’ gives me weird frame count. For instance (even): 2, 4, 6, 10, 12, 16, 20, 22 ... (no 8, 14, 18). However I’ve found another way based on the ‘mod(n-1\,2)’. First, in case of compressed file, decode it to raw YUV: ‘-i input.mp4 -an -vcodec rawvideo -pix_fmt yuv420p output.yuv’. With YUVs, input frame rate required, so it also works in this case :) So the second iteration will be ‘-r 2 -s WxH -i output.yuv -vf "select=mod(n-1\,2)" -r 1 -an -vcodec rawvideo -pix_fmt yuv420p outputEven.yuv’. If the file is already YUV, only the second is needed. Works like a charm :)
  • slhck
    slhck about 11 years
    @Mark I'd be happy for you to suggest an edit to my answer and add the steps that worked for you, thereby correcting any mistakes I made. You can also post your own answer and mark it as accepted :)
  • Mark
    Mark about 11 years
    This is an excellent alternative, but as I've mentioned - I'm limited to use only ffmpeg. The reason for that is it is a part of an automated system that will create only .bat files, no possibility for addition AVS.
  • Mark
    Mark about 11 years
    Great, thanks! Very interesting the way it works, I'm new to this system of Q&A :)
  • Louis Waweru
    Louis Waweru about 11 years
    @Mark I was thinking that if --enable-avisynth was there, that it meant AviSynth was built into ffmpeg, but I'm not sure about that.
  • Anmol Singh Jaggi
    Anmol Singh Jaggi about 8 years
    Can it not be done using the options -framerate/-r ?
  • slhck
    slhck about 8 years
    @AnmolSinghJaggi No, this just drops frames, but I wouldn't be sure that it's so deterministic (i.e., drops every odd frame). It'll be based on time codes, which may not be accurate.