37

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

Community
  • 1
  • 1
Hitesh
  • 4,098
  • 11
  • 44
  • 82

5 Answers5

91

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

drumkruk
  • 1,046
  • 9
  • 12
  • 2
    The format is normally guessed from the output file extension, so `-f image2` is not needed here. – llogan Nov 26 '14 at 18:33
  • what is `-f image2` ?? here – Hitesh Nov 27 '14 at 05:52
  • -f is the format of the input/output (normally guessed by ffmpeg) and image2 is the demuxer. See ffmpeg documenation for more info: https://www.ffmpeg.org/ffmpeg-formats.html#Demuxers @LordNeckbeard removed it from the answer – drumkruk Nov 27 '14 at 10:39
  • @hitesh `-f image2` is generally used if the output is a variable. – llogan Nov 27 '14 at 18:52
  • where to give output image file path – Hitesh Nov 28 '14 at 09:37
  • absolute path: foo/bar/output.png – drumkruk Nov 28 '14 at 09:39
  • 3
    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). – Suchipi Apr 25 '15 at 09:13
  • What's the `$` sign? – Shayan Dec 13 '19 at 16:32
  • @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. – Shayan Feb 11 '20 at 14:45
  • 2
    @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. – llogan Feb 11 '20 at 19:08
  • @llogan you are right, it was not needed to have the variable there. – drumkruk Feb 12 '20 at 15:22
  • But if shorter than 1sec, what will be? – Rizwan Ahmed Jun 14 '22 at 11:05
17

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
Jeff Ward
  • 16,563
  • 6
  • 48
  • 57
7

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.

0
$ffmpegPath = exec('which ffmpeg');
$ffprobePath = exec('which ffprobe');

$command = "$ffprobePath -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $input_video";
$video_duration = shell_exec($command);

$thumbnails_output = 'output%02d.png';
$command = "$ffmpegPath -i $input_video -vf fps=3/$video_duration $thumbnails_output";
shell_exec($command)
Raza Rafaideen
  • 2,119
  • 1
  • 19
  • 30
  • Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. Answers with an explanation are always more helpful. The solution may be obvious to you. To others it may not. Please explain what it does, and how it's different from existing answers. Otherwise this answer may be flagged as [Low Quality](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). – Jan Aug 13 '22 at 22:56
0

This is my bash script to create n thumbnails of a video (in the example there are 11 but you can change it) It does it at regular intervals over the entire length of the video.

#!/bin/bash

for input_video in "$@"
do
  duracion=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input_video");
  #echo $duracion;

  ffmpeg -i "$input_video" -vf fps=11/$duracion "$input_video%02d.png";
  #echo "  ffmpeg -i \"$input_video\" -vf fps=3/$duracion  \"$input_video%02d.png\"";

done