Take snapshots of Youtube videos to create a preview GIF programatically

Question:

I use the Youtube API to integrate content into my site. But, it doesn’t provide any good snapshots of the videos. It provides a few very low quality snapshots that are virtually unusable.

https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg

For example: https://img.youtube.com/vi/XdwaASKJGxo/1.jpg creates the following:

enter image description here

This snapshot is such low quality it’s unusable. So I need to make my own!

My goal is to have a unique thumbnail/preview for each video. I would like to programmatically take 3 snapshots of the video within the middle of the video to avoid intros/outros. Then I would like to take these snapshots and generate a GIF.

I know how to generate a GIF using PHP, but I cannot find a way to take snapshots of a video.

Is it possible to do this using PHP or Python? Or any other code for that matter.

Example:

enter image description here

Asked By: Maciek Semik

||

Answers:

Since, you haven’t added any code or approach that you have used, I am going to give you an overview (using python).

As the quality of the snapshots provided by the YouTube API is not good enough, you may want to consider using a different method to extract frames from the video. You can use something like FFmpeg to extract multiple frames from the video at regular intervals, adjusting the timing and frame rate of the frames as needed. This will give you more control over the timing and quality of the frames too. Once you have the frames, you can use a graphics library like ImageMagick or Pillow to combine them into a single GIF image.

Let me know how you go!

Answered By: Aman Hirani

I finally found a solution. A good approach to this is to use the PageSpeed Insights API.

The method goes as follows:

  1. Enable the PageSpeed Insights API and create an API key
  2. Direct it to an embedded youtube video such as https://www.youtube.com/embed/videoid?start=100 where videoid is the video ID and start is the timestamp in seconds
  3. Extract final-screenshot from result given to you by the GET call from PageSpeed Insights

Here’s a good tutorial.

Answered By: Maciek Semik

As you haven’t mentioned anything much apart from creating preview gifs from YouTube Videos snapshots, I’ll tell you how can we do that with YouTube Data API and the FFmpeg library.

Note: You need to have an API key for YouTube Data API and install FFmpeg on your server.

<?php

// Set your API key for the YouTube Data API
$apiKey = "YOUR_API_KEY_HERE";

// Set the YouTube video ID
$videoId = "VIDEO_ID_HERE";

// Set the start and end time of the video clip (in seconds)
$startTime = 0;
$endTime = 5;

// Set the desired width and height of the snapshot
$width = 320;
$height = 240;

// Set the output path and filename for the preview GIF
$outputPath = "PREVIEW_GIF_FILENAME.gif";

// Get the video information using the YouTube Data API
$videoInfo = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apiKey&part=snippet"), true);

// Get the video title from the API response
$videoTitle = $videoInfo['items'][0]['snippet']['title'];

// Use FFmpeg to create a snapshot of the video
exec("ffmpeg -ss $startTime -i "https://www.youtube.com/watch?v=$videoId" -frames:v 1 -s {$width}x{$height} snapshot.jpg");

// Use FFmpeg to create a preview GIF from the snapshot
exec("ffmpeg -i snapshot.jpg -vf scale=$width:$height -t $endTime -pix_fmt rgb24 -loop 0 $outputPath");

// Display the preview GIF with the video title as alt text
echo "<img src="$outputPath" alt="$videoTitle">";
?>

I hope this helps 🙂

Answered By: Apoorv
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.