Extracting "one of every 10 frames" in a video using VLC or FFmpeg

12

8

I am trying to extract "exactly 1 frame of every 10" frames of a video (i.e. extract 1 , leave 9 then repeat) for scientific purposes. The video is 105 frames, 3.5 seconds, 29.97fps (h.264, .mov, produced by Nikon D3100).

I have uploaded it here.

VLC

Below command should produce 10 frames, but it only produces 6 images. I tried different scene ratios and neither of them produce correct number of frames (not even near to correct).

vlc 1.mov --video-filter=scene --vout=dummy --scene-ratio=10 --scene-prefix=img- --scene-path=. vlc://quit

Would someone please tell me what is the problem?

FFmpeg

FFmpeg does not seem to have a command exactly for my purpose. Below command extracts 3 frames out of every second, but since the FPS is not exactly 30 (rather 2.97), that will not produce correct results for me.

In addition even FFmpeg does not give out correct number of frames with even this command. For 3.5 seconds of video I expect at most 10 frames, but what I get is 12 frames!

ffmpeg -i 1.mov -y -an -sameq  -r 3 -f image2 -vcodec mjpeg %03d.jpg 

How can I achieve what I want?

wmac

Posted 2012-02-18T04:18:32.360

Reputation: 1 047

1BTW -sameq doesn't do what you probably think it does, and has been removed from the current version (the real ffmpeg that is, I'm not sure if it's in the Ubuntu fake ffmpeg). Use qscale instead. – stib – 2014-04-22T02:38:53.173

Answers

21

Select 1 frame out of every 10 frames

You can use the select video filter in ffmpeg to do this:

ffmpeg -i input.mov -vf "select=not(mod(n\,10))" -vsync vfr -q:v 2 img_%03d.jpg
  • For JPG output you can vary quality with -q:v. Effective range is 2 (best quality) to 31 (worst quality). You don't need this option if you want to output to PNG instead.

  • This will output img_001.jpg, img_002.jpg, img_003.jpg, etc.

llogan

Posted 2012-02-18T04:18:32.360

Reputation: 31 929

i was going from a 10 fps video and i wanted every 10th frame. however select=not(mod(n\,10)) did not have the desired effect. i expected it to produce a file 1/10th the duration but it did not. from https://superuser.com/a/1156862/316154 you also need to use setpts=N/10/TB (10 is the fps) making the whole thing -vf select=not(mod(n\,10)),setpts=N/10/TB

– northern-bradley – 2019-07-03T07:23:06.760

@northern-bradley Looks like you want to output video. Users who want images, such as the question the answer addressed, won't need setpts. – llogan – 2019-07-08T17:58:27.193

Looks very interesting, but seems to require some particular version or something extra compiled-in. I get "unrecognized option '-filter:v'" with "FFmpeg version SVN-r0.5.1-4:0.5.1-1ubuntu1.3" (--enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static) – mivk – 2012-02-19T21:54:51.323

I also cannot run the command and receive the following error on ffmpeg windows N-35709-g7d531e8: [select @ 017EBB00] [Eval @ 0022DC08] Missing ')' or too many args in 'mod(n,10))' [select @ 017EBB00] Error while parsing expression 'not(mod(n,10))' Error initializing filter 'select' with args 'not(mod(n,10))' Error opening filters! – wmac – 2012-02-20T05:22:48.727

@mivk Your FFmpeg version from the Ubuntu repo does not have filtering capabilities. You will have to compile FFmpeg.

– llogan – 2012-02-20T19:33:01.710

1@wmac I guess Windows doesn't like the single quotes. Change them to double quotes: ". – llogan – 2012-02-20T19:52:25.040

1@LordNeckbeard, thanks, it runs correct and solved the whole problem. I really really appreciate your help. – wmac – 2012-02-21T01:52:01.173

4

The most important aspect in your question is the fact that the video uses 29.97 frames per second, not 30. Pesky NTSC.

Anyway, I think it would be easiest to just extract every frame, and then remove the ones you don't need:

ffmpeg -i 1.mov -y -f image2 -c:v mjpeg %03d.jpg

Then, remove the ones you don't need. Since every tenth frame will end with a 1.jpg, we can just take all the others …

find . -maxdepth 1 -not -iname "*1.jpg"

… and once you're sure these are the ones you want to remove:

find . -maxdepth 1 -not -iname "*1.jpg" -exec rm '{}' \;

If you can use mencoder, you could try the framestep option, as explained in the documentation, like framestep=10 in your case. I personally couldn't install/try it though.

slhck

Posted 2012-02-18T04:18:32.360

Reputation: 182 472

1Thank you very much for your help and your edit.

I tried mencoder and even that one has problem! I guess it has something to do with either codec or container.

ffmpeg extracts exactly 105 frames with below command:

ffmpeg -i 1.mov -y -an -sameq -f image2 -vcodec mjpeg %03d.jpg

but mencoder only extracts 90 frames!!! with below command:

mplayer -vo png 1.mov

Using framestep=10 also produces wrong number of frames. I am shocked on how all three (ffmpeg, vlc and mencoder) are unable to do such a simple task correctly. I submited a bug report to VLC website.

Thanks again. – wmac – 2012-02-18T17:56:05.143

0

If you were to first convert the video to a series of raw rgb24 or rgb32 images possibly then you might be able to get the right number of frames since in original form there seems to include unusual frame types that may not be images at all ???

In the original Laser Discs the entire video was composed of a series of images with individual frame numbers 1 to 100,000 or more, and like that is really the correct way to establish a baseline for future conversions or manipulations.

The industry has changed to this strange compression idea simply to reduce monies outlaid and to corrupt true scientific forms of handling numbers.

You must first extract any audio as a wave file so as not to lose audio entirely. It seems FFMPEG puts identifying info in each extracted image, because if you try to string images together from ones just extracted, and you mix other images from other sources with same extension, ffmpeg will ignore the images you tried to piece into the middle of everything.

With the laser disc format, the frame rate is simply dictated by whatever rate you present the sequential images and not controlled in any way by the images themselves.

FFMPEG might take a lesson from science instead of art for proper handling and display of images of any kind. Or possibly the entire AV industry itself. The industry really needs to improve capabilities of the hardware and use raw data which requires lots of memory/storage. Nothing beats raw data for precision and accuracy.

Aluetta

Posted 2012-02-18T04:18:32.360

Reputation: 11