Reputation: 179
I'd like to have my software periodically (every 5 minutes or so) take a screenshot of a live stream that's currently happening. I tagged this question for both Youtube and Twitch since the stream is happening on both, so an answer that works for either of those is perfect.
I've looked into some older libraries like youtube-dl and livestreamer but they are CLI that download a file that then needs to be read with a video player.
Upvotes: 5
Views: 3942
Reputation: 5642
Using yt-dlp and ffmpeg you can get the last frame of a livestream happening on YouTube by executing:
ffmpeg -i "$(yt-dlp -g VIDEO_ID | head -n 1)" -vframes 1 last.jpg
yt-dlp -g VIDEO_ID | head -n 1
will retrieve the actual video URL from Google servers.
As far as I tested with ydYDqZQpim8
, the retrieved frame is less than 2 seconds later than the time displayed on the livestream.
Note that the above method only downloads 2K resolution screenshot for 4K livestreams, if someone knows how to have 4K resolution screenshot please comment or add an answer.
Upvotes: 5
Reputation: 1058
For Twitch you can use the Get Streams API to get the current thumbnail of the stream.
Twitch already did the work for you to provide a screenshot, so you can just consume the thumbnail instead.
So example response to Get Streams
{
"data": [
{
"id": "41375541868",
"user_id": "459331509",
"user_login": "auronplay",
"user_name": "auronplay",
"game_id": "494131",
"game_name": "Little Nightmares",
"type": "live",
"title": "hablamos y le damos a Little Nightmares 1",
"viewer_count": 78365,
"started_at": "2021-03-10T15:04:21Z",
"language": "es",
"thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_auronplay-{width}x{height}.jpg",
"tag_ids": [
"d4bb9c58-2141-4881-bcdc-3fe0505457d1"
],
"is_mature": false
},
Just grab the thumbnail_url
and throw a cache buster on the end and you have an image. No FFMPEG or yt-dl schnanigans needed
Upvotes: 0