20

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
  • 223,558
  • 70
  • 607
  • 592
Mark
  • 425
  • 2
  • 4
  • 9

2 Answers2

18

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 …
Vladimir Panteleev
  • 1,389
  • 1
  • 13
  • 20
slhck
  • 223,558
  • 70
  • 607
  • 592
  • Thanks, For new versions of FFMPEG it should be -vf instead of -filter:v. In addition it should be mod(n-1\,2) because the n count seems to start from from 1 while frame count from 0 (otherwise the first frame is duplicated 3 time). But there is still a problem, it duplicates the frames while I want to get rid of them - e.g. the final clip will contain only half of the frames. – Mark Mar 28 '13 at 15:28
  • `-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](http://ffmpeg.org/ffmpeg-filters.html#tinterlace) can do the same? – slhck Mar 28 '13 at 15:35
  • I've tried '-r 2 -i blah.yuv -r 1' but it shows me an error _Option framerate not found._ – Mark Mar 28 '13 at 15:36
  • Ah sorry.. Scratch that, this doesn't work (anymore?) and only for images. – slhck Mar 28 '13 at 15:38
  • 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. – slhck Mar 29 '13 at 16:50
  • 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 :) – Mark Apr 03 '13 at 15:16
  • @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 :) – slhck Apr 03 '13 at 15:51
  • Great, thanks! Very interesting the way it works, I'm new to this system of Q&A :) – Mark Apr 03 '13 at 17:04
  • Can it not be done using the options `-framerate`/`-r` ? – Anmol Singh Jaggi Jun 16 '16 at 03:23
  • 1
    @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. – slhck Jun 16 '16 at 08:04
3

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.

Lucas Walter
  • 215
  • 2
  • 8
Louis Waweru
  • 23,945
  • 39
  • 132
  • 198
  • 1
    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 Apr 03 '13 at 17:02
  • @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. – Louis Waweru Apr 03 '13 at 17:09