86

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
lex
  • 1,139
  • 2
  • 10
  • 13

4 Answers4

109

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.

arielCo
  • 413
  • 5
  • 16
Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54
  • 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? – Alexis Wilke Jun 25 '17 at 01: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. – Dhiraj Gupta Jun 25 '17 at 05:29
  • For me it's not keeping the aspect ratio of the video and outputs a square image – Guig Jul 20 '17 at 16:33
  • @Guig did you try the first command (without the scale filter). That shouldn't change the output image size at all, it should be same as source video. – Dhiraj Gupta Jul 22 '17 at 08:45
  • I figured that the width and height where the same, but the aspect ratio was part of the metadata and not handled properly by the above command. – Guig Jul 24 '17 at 07:19
  • 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 '17 at 23:57
  • @Murilo Check your quote marks around the parameters, and ordering - from the error you're seeing, it looks like as if ffmpeg is treating output_image2.jpg as the input file? – Dhiraj Gupta Sep 20 '17 at 07:09
  • @DhirajGupta In the output filename, use something like `output_image%03d.jpg`. The `%03d` pattern specifies to use a decimal number composed of three digits padded with zeroes to express the sequence number. – JustinStolle Mar 01 '18 at 07:19
  • 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 '19 at 00:47
  • It works and the image is good, but the command produces whole screen of errors: `Cannot write more than one file with the same name. Are you missing the -update option or a sequence pattern?` – kelin Jan 26 '23 at 12:46
80

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
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • 7
    How to extract only the 1st frame of a list of .flv files? – lex Dec 13 '10 at 02: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 '10 at 03: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 '20 at 01:14
  • 2
    probably, but you'll have to ask it *as a question*, not as a comment. – jcomeau_ictx Mar 27 '20 at 14:36
52

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.

henry
  • 4,244
  • 2
  • 26
  • 37
  • 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! – Evan Wagstaff Dec 23 '19 at 01:32
  • 1
    Worked a lot faster than other solution proposed here – hannojg May 22 '21 at 10:52
7

It's works for me

ffmpeg -i sample-mp4-file.mp4 -ss 1 -vframes 1 output.jpg
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Mohamed Abbas
  • 79
  • 1
  • 2