Skip to content

Serve stream excerpts

In this tutorial, you’ll start a playback server for a YouTube live stream. The server gives you MPEG-DASH access to excerpts of the stream. You’ll rewind to a past moment with a single request, then send the result to a media player or a downloader.

We’ll use the Northern Royal Albatross nesting cam as our example.

  Want some background first? Read Core concept in Overview.

Prerequisites

Before you begin, make sure you have:

  1. ypb installed
  2. ffmpeg (for ffplay) and yt-dlp, if you want to follow the playback and download steps

Start the playback server

Run ypb serve with the YouTube video ID:

$ ypb serve Mm_zVDDUeNA
(<<) Stream 'Live & Just Hatched! Royal Albatross Cam - NZ Dept. of Conservation | Cornell Lab' is alive!
(<<) Playback started and listening on http://localhost:9000...

The server keeps running in this terminal. Open a second terminal for the next steps.

Rewind to a past moment

The playback server does one job: rewind to the moment you ask for. It finds that moment, builds an MPEG-DASH manifest for it, and streams the segments the manifest points to. You ask for a moment through the /mpd/{interval} endpoint, where {interval} is the moment you want. Let’s ask for the last 30 minutes:

curl http://localhost:9000/mpd/30m--now

You should see a wall of XML. That’s the manifest describing the excerpt.

The {interval} part uses the same format as the -i/--interval option of the download command. Two things change in a URL: use -- instead of /, and avoid whitespace (or percent-encode it).

See Specifying the rewind time for all the interval formats.

Static and dynamic manifests

The kind of manifest you get depends on the interval. A closed interval, one with an end, gives you a static manifest: it describes a finished excerpt and never changes. An open interval, one with no end, gives you a dynamic manifest: it keeps growing as the stream goes on.

# Static: excerpts with a fixed end
curl localhost:9000/mpd/30m--now
curl localhost:9000/mpd/12:00--30m

# Dynamic: excerpts with no end
curl localhost:9000/mpd/now
curl localhost:9000/mpd/12:00

Play the excerpt

Now let’s play the excerpt with a player, without downloading it first. This tutorial uses mpv. Point it at the same URL:

mpv --demuxer-lavf-o=protocol_whitelist=file,http,https,tcp,tls \
  localhost:9000/mpd/30m--now

The --demuxer-lavf-o option passes the protocol whitelist to mpv’s FFmpeg demuxer, letting it open the manifest and fetch its media segments.

Using a different player

Any MPEG-DASH compatible player works here, not just mpv. For example, ffplay and VLC can both open the same manifest URL directly.

Download the excerpt

You can also point the manifest to a downloader, instead of a player.

For example, let’s use yt-dlp’s general extractor directly: this is kind of similar to what the download command does under the hood:

yt-dlp -o output.mp4 http://localhost:9000/mpd/30m--now

Using a different downloader

Other downloaders work too: FFmpeg, GPAC’s MP4Box, or dash-mpd-cli.

See also