Skip to content

Instantly share code, notes, and snippets.

@ericxtang
Last active November 15, 2021 20:33
Show Gist options
  • Save ericxtang/0932c3be37628d511df8d37f4646848a to your computer and use it in GitHub Desktop.
Save ericxtang/0932c3be37628d511df8d37f4646848a to your computer and use it in GitHub Desktop.
Clipping a live stream with Livepeer

Clipping with Livepeer

Clipping in Livepeer is currently possible, but not yet a well-packaged feature. In order to create a clip, you need to:

1. Turn on recording

Make sure recording is turned on for the stream. Follow this API guide.

2. Ingest into a specific region

You can do this by changing the RTMP ingest URL to rtmp://{region}-rtmp.livepeer.com/live/{stream-key}. For example: rtmp://nyc-rtmp.livepeer.com/live/3821-jkku-06da-w3nd. Some available regions are: nyc, lax, mdw, lon, prg.

3. Get the session ID

Get current stession ID by calling GET https://livepeer.com/api/stream/{stream-id}/sessions?limit=1.

4. Get the recording playback URL

You can get it using this URL pattern: https://{region}-cdn.livepeer.com/recordings/{session-id}/source.m3u8

5. Create the clip

You can take the last N segments from the playlist based on the segment lengths and the designed clip length, concatenate the segments into a single mp4 file. For example:

wget https://nyc-cdn.livepeer.com/recordings/{session-id}/source/0.ts
wget https://nyc-cdn.livepeer.com/recordings/{session-id}/source/1.ts
wget https://nyc-cdn.livepeer.com/recordings/{session-id}/source/2.ts
wget https://nyc-cdn.livepeer.com/recordings/{session-id}/source/3.ts
cat 0.ts 1.ts 2.ts 3.ts > clip.ts
ffmpeg -i clip.ts -acodec copy -vcodec copy clip.mp4

Alternatively, you can create a new m3u8 file that contain ONLY the segments you want. For example:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:1
#EXTINF:1.000,
https://example.com/1.ts
#EXTINF:1.000,
https://example.com/2.ts
#EXTINF:1.000,
https://example.com/3.ts
#EXTINF:1.000,
https://example.com/4.ts
#EXT-X-ENDLIST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment