79

Anyone knows the trick?

And how to install ffmpeg ? yum install mpeg only returns this:

======================================================================================== Matched: mpeg ========================================================================================
libiec61883.i386 : Streaming library for IEEE1394
libiec61883.x86_64 : Streaming library for IEEE1394
qffmpeg-devel.i386 : Development package for qffmpeg
qffmpeg-devel.x86_64 : Development package for qffmpeg
qffmpeg-libs.i386 : Libraries for qffmpeg
qffmpeg-libs.x86_64 : Libraries for qffmpeg
0

4 Answers 4

104

I've cobbled up this command line from various answers that works great for me to get the absolutely first frame out from a video. I use this to save a thumbnail screenshot for the video.

ffmpeg -i inputfile.mkv -vf "select=eq(n\,0)" -q:v 3 output_image.jpg

Explanation:

The select filter -vf "select=eq(n\,0)" is to select only frame #0.

-q:v allows you to set the quality of the output jpeg between 1 and 31. Lower the number, higher the quality. 2 - 5 works good, I use 3.

Note: This will get you an image with the same size as the video. To get a thumbnail, you can use the scale filter to get a thumbnail to fit whatever width you need, like so:

ffmpeg -i inputfile.mkv -vf "select=eq(n\,0)" -vf scale=320:-2 -q:v 3 output_image.jpg

The above command will give you a thumbnail jpeg that will be scaled to match width of 320, and height will be calculated to match the aspect ratio.

10
  • Interestingly enough, though, it always returns an error saying it could not get the image out and the exit code reflects that. However, the image is right there... Have you had that problem too? Jun 25, 2017 at 1:30
  • Well no, actually. In reality I'm using this command running out through Node and / or PHP and in both cases the command is completing successfully, as far as I can gather, because (at least in Node) the exit code is used to signal successful run or not. Jun 25, 2017 at 5:29
  • For me it's not keeping the aspect ratio of the video and outputs a square image
    – Guig
    Jul 20, 2017 at 16:33
  • 1
    @DhirajGupta I got en error with the scale command "Could not get frame filename number 2 from pattern 'output_image2.jpg' (either set updatefirst or use a pattern like %03d within the filename pattern) av_interleaved_write_frame(): Invalid argument"
    – Murilo
    Sep 19, 2017 at 23:57
  • 1
    This approach is unoptimal since it decodes the full video even after dumping the first frame. The answer by @jcomeau_ictx is the optimal one.
    – Vishal
    Jan 11, 2019 at 0:47
78

It's on the manpage:

* You can extract images from a video, or create a video from many
       images:

       For extracting images from a video:

               ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg

       This will extract one video frame per second from the video and will
       output them in files named foo-001.jpeg, foo-002.jpeg, etc. Images
       will be rescaled to fit the new WxH values.

       If you want to extract just a limited number of frames, you can use
       the above command in combination with the -vframes or -t option, or in
       combination with -ss to start extracting from a certain point in time.

But of course you have to install it first. I'm on Debian and don't use yum.

[update for the other question]


i=1
for avi in *.avi; do
 ffmpeg -i $avi -vframes 1 -f image2 /tmp/$i.jpg; i=$((i+1))
done

Tested and works.

[update for yet another question...]


for flv in *.flv; do
 ffmpeg -i $flv -vframes 1 -f image2 ${flv%%.flv}.jpg
done
4
  • 6
    How to extract only the 1st frame of a list of .flv files?
    – lex
    Dec 13, 2010 at 2:55
  • Sorry,but my list of .flv files is specified by something like a.flv,b.flv...,and I want to save the image with the same name as the flv files..
    – lex
    Dec 13, 2010 at 3:07
  • Furthering on from this. Is it possible to convert a video to images at 1fps starting from the very first frame? Setting fps = 1 starts from 1s
    – Chris
    Mar 25, 2020 at 1:14
  • 2
    probably, but you'll have to ask it as a question, not as a comment. Mar 27, 2020 at 14:36
48

An easy to grok solution that works for me is

ffmpeg -i <input> -vframes 1 <output>.jpeg

Note that I do get an error "[swscaler @ 0x111652000] deprecated pixel format used, make sure you did set range correctly" but according to a little reading (see for example https://stackoverflow.com/a/43038480/1241736) that can safely be ignored.

2
  • 1
    The accepted answer did not work for me, got Error initializing filter 'select' with args 'eq(n', but maybe a formatting problem on my end. Your answer worked great! Dec 23, 2019 at 1:32
  • 1
    Worked a lot faster than other solution proposed here
    – hannojg
    May 22, 2021 at 10:52
5

It's works for me

ffmpeg -i sample-mp4-file.mp4 -ss 1 -vframes 1 output.jpg
1
  • 4
    the -ss 1 is wrong here , because it will seek to 1st second not 1st frame.
    – Maria
    May 11, 2021 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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