3

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.

Kaiser
  • 606
  • 8
  • 22
Regalia
  • 129
  • 2
  • 10
  • It's actually quite simple to grab the current frame of a ` –  Oct 31 '19 at 14:08
  • I'm looking to do this in my nodejs server. But I think the real problem is managing to get the video element from the live stream – Regalia Oct 31 '19 at 14:16
  • In that case you'll probably want to use a headless browser library like puppeteer to simulate a browser session. Once puppeteer has navigated to the URL, it should be as simple as `const vid = document.querySelector("video")` or whatever the puppeteer equivalent is. –  Oct 31 '19 at 14:18
  • I tried it with puppeteer myself, and apparently neither twitch nor youtube wants you to be scraping their pages. I guess your best bet is just to do it the way it's meant to be. Both [YT](https://developers.google.com/youtube/v3/quickstart/nodejs) and [Twitch](https://dev.twitch.tv/docs/api/) have APIs. – code Nov 02 '22 at 22:00

2 Answers2

3

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
0

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

Barry Carlyon
  • 1,039
  • 9
  • 13