Create thumbnail from video using ffmpeg

51,611

Solution 1

I do not know a way to make a screenshot WHILE uploading, but I do know how to do it after.

The simplest code is:

ffmpeg -i input.mp4 -ss 00:00:01.000 -vframes 1 output.png

Run this script after you have uploaded the file. It should take only a short amount of time if the screenshot is taken in the beginning of the video. (first minute e.g.).

I do not think it is possible to take a screenshot while the file is still being uploaded.

Edit: removed -f image2 as it is guessed correct by ffmpeg

Solution 2

The other answers are fine... but for most "video" content, JPEG is a more space-efficient choice for a thumbnail image. This answer discusses JPEG quality settings.

And often you'll want to specify the thumbnail size - the below command (source) will scale the video down to fit in a 320x320 box (maintaining the aspect ratio by decreasing the smaller edge -- i.e. 320px on the long edge):

ffmpeg -ss 00:00:01.00 -i input.mp4 -vf 'scale=320:320:force_original_aspect_ratio=decrease' -vframes 1 output.jpg

Solution 3

According to this documentation https://trac.ffmpeg.org/wiki/Seeking and my personal tests you should change places of -ss and -i like this

ffmpeg -ss 00:00:01.000 -i input.mp4 -vframes 1 output.png

The operation will become much faster as no video decoding will be done. Video will be parsed using keyframes instead, which is very fast.

Share:
51,611

Related videos on Youtube

Hitesh
Author by

Hitesh

On the Journey to become better and better everyday Thank God, I am not where I used to be, i am growing and I am on my way and I am heading toward the ultimate goal :) Aiming to become the best software developer, To develop the best quality and standard products. Love Learning new stuff to improve my skills. "Learning to Soar Like an Eagle…Even When You Feel Like a Chicken" I have just touched the surface of web development, There is so much here and still need to learn swimming :D And As They All Say "You don’t have to be a genius to code!" ;)

Updated on August 10, 2021

Comments

  • Hitesh
    Hitesh almost 3 years

    I need to create a thumbnail from video while uploading it to CDN.

    I have been searching for this found this but I am not able to get the screen shot even after following steps.

    I am using jwplayer for playing video

    Can someone help me to create thumbnail while uploading video using ffmpeg

  • llogan
    llogan over 9 years
    The format is normally guessed from the output file extension, so -f image2 is not needed here.
  • Hitesh
    Hitesh over 9 years
    what is -f image2 ?? here
  • drumkruk
    drumkruk over 9 years
    -f is the format of the input/output (normally guessed by ffmpeg) and image2 is the demuxer. See ffmpeg documenation for more info: ffmpeg.org/ffmpeg-formats.html#Demuxers @LordNeckbeard removed it from the answer
  • llogan
    llogan over 9 years
    @hitesh -f image2 is generally used if the output is a variable.
  • Hitesh
    Hitesh over 9 years
    where to give output image file path
  • drumkruk
    drumkruk over 9 years
    absolute path: foo/bar/output.png
  • Suchipi
    Suchipi about 9 years
    If you put -ss before -i, then you won't have to wait for the first minute of frames to go through. However, most decoders can't go to an exact timestamp, so you may end up slightly before your timestamp (eg 00:00:58 instead of 00:01:00).
  • Shayan
    Shayan over 4 years
    What's the $ sign?
  • Shayan
    Shayan over 4 years
    @llogan Do you happen to know what the $ sign means? I googled linux dollar sign, and I found an example: lsattr "$(realpath /etc/resolv.conf)" but I still don't understand why drumkruk is not using parentheses here.
  • llogan
    llogan over 4 years
    @Shayan It is just a generic placeholder. To be more clear he could have simply used input.mp4. In some languages the $ indicates a variable.
  • drumkruk
    drumkruk over 4 years
    @llogan you are right, it was not needed to have the variable there.
  • Rizwan Ahmed
    Rizwan Ahmed about 2 years
    But if shorter than 1sec, what will be?