

We can use ffmpeg and basically copy the video to /dev/nullwithout reconverting it and count its frames in the process. ffprobe reads information from video streams and, in my experience, videos downloaded from YouTube usually have nb_frames equal to N/A, which is useless.īut there’s a clever workaround.

Unfortunately, this method works only with some videos.

head -n1 We only care about the video stream, which comes first.Each video usually has two streams (video and audio). -show-streams Shows all streams found in the video.$ ffprobe - show_streams "video.mp4" 2 > /dev/ null | grep nb_frames | head - n1 | sed 's/.*=//'.The easiest way is to use ffprobe which is a utility that comes with ffmpeg and is designed to extract various bits of information from videos in human and machine-readable formats. In order to do this we need to know the number of frames in our video and that requires some scripting. Since ffmpeg doesn’t have any built-in option for making video previews we have to tell it to “make a screenshot every Nth frame”. Getting the total number of frames in a video We can download our video with 360p and save it as video.mp4 (cca 8.28MB).

It’s always better to choose those with relative width (that’s 144p, 240p, 360p, 480p, etc.) because fixed size formats might have black stripes at the top and bottom, and as I said earlier we want a fixed height of 120px with various widths. 140 m4a audio only DASH audio, Īt the bottom there’s the default resolution of 1280x720 (best) (with format itag 22), but 360pwith itag 134 is sufficient for our needs.171 webm audio only DASH audio, 48k ( worst ).v1uyQZNg2vE : Extracting video information.v1uyQZNg2vE : Downloading video info webpage.youtube - dl - F https : //There’s quiet a lot of them ( see all supported video formats on YouTube):.Let’s list all formats that YouTube generated for this video (parameter -F works only with YouTube): The sweet thing is that you don’t have to download full size video in order to make a preview that’s only 120px tall. It’s up to date and it supports many sites). I strongly recommend you use youtube-dl ( see installation notes. In order to generate a video preview we have to download the video first. Download a video from YouTube, Vimeo, etc. Please note, that I’m using ffmpeg 2.2.2, although all functionality used in this tutorial should be already implemented in older versions.
