Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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 …
share|improve this answer
    
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 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

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|improve this answer
    
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 3 '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 Apr 3 '13 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.