Twitch and FFmpeg and Youtube-dl: Fetch from live stream to local file
(Using Windows PowerShell, adapt for UNIX bash shouldn’t be a big issue)
Record a live stream#
So something nice with youtube-dl is like you can ask not to download the media but to fetch for the media link :
>> youtube-dl -g https://www.youtube.com/watch?v=RJt01u4yrLQ
https://r2---sn-h0jeened.googlevideo.com/videoplayback?expire=1[...]
If you use that for a twitch channel that is streaming live, it returns you the HLS stream.
>> youtube-dl -g https://www.twitch.tv/farore_de_firone
https://video-weaver.ber01.hls.ttvnw.net/v1/playlist/CpkEQusnrcdffNI3[..]MA2c4.m3u8
It by default binds to the best quality video, but you can check and select all format available using -F
>> youtube-dl -F https://www.twitch.tv/farore_de_firone
[twitch:stream] farore_de_firone: Downloading stream GraphQL
[twitch:stream] farore_de_firone: Downloading access token JSON
[twitch:stream] 39653517372: Downloading m3u8 information
[info] Available formats for 39653517372:
format code extension resolution note
audio_only mp4 audio only 2k , mp4a.40.2
160p mp4 284x160 230k , avc1.4D401F, 30.0fps, mp4a.40.2
360p mp4 640x360 630k , avc1.4D401F, 30.0fps, mp4a.40.2
480p mp4 852x480 1262k , avc1.4D401F, 30.0fps, mp4a.40.2
720p60 mp4 1280x720 3257k , avc1.4D401F, 60.0fps, mp4a.40.2
1080p60__source_ mp4 1920x1080 6713k , avc1.64002A, 60.0fps, mp4a.40.2 (best)
You could even only have the audio-stream
And to select it
youtube-dl -f 160p -g https://www.twitch.tv/farore_de_firone
So with this link, you can use ffmpeg to record localy the stream to your computer (and have your own reaplay / VOD without the “disagreement” of Twitch VOD :) )
ffmpeg -i "$(youtube-dl -f 720p60 -g https://www.twitch.tv/farore_de_firone)" -c copy stream.20201012.mp4
And here it’s quite simple “dump” of the running script. Nothing prevent you to add some filters, reencoding that adapt to your needs.
Mixing multiple stream#
Let’s have some fun, there is some streamers that plays together on the same game. Usually, you can watch their POV at the sametime with the Twitch Squad mechanism or Multitwitch application. But would it be possible to record a file in such way ?
Actually yes, ffmpeg can take multiple video input and transform it on the fly via the filter complex to render all the video on the same stream.
There is a nice topic on Stackoverflow that explain how to simply stack multiple video : https://stackoverflow.com/questions/11552565/vertically-or-horizontally-stack-mosaic-several-videos-using-ffmpeg
Example merging 2 videos :
ffmpeg -i "$(youtube-dl -g https://www.twitch.tv/antoinedaniellive)" \
-i "$(youtube-dl -g https://www.twitch.tv/soon)" \
-filter_complex vstack=inputs=2 \
-map 0:a \
output.mp4
Example merging 4 videos :
ffmpeg -i "$(youtube-dl -f 160p -g https://www.twitch.tv/antoinedaniellive)" \
-i "$(youtube-dl -f 160p -g https://www.twitch.tv/soon)" \
-i "$(youtube-dl -f 160p -g https://www.twitch.tv/angledroit )" \
-i "$(youtube-dl -f 160p -g https://www.twitch.tv/etoiles)" \
-filter_complex "[0:v][1:v][2:v][3:v]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" -map "[v]" \
-map 0:a \
-y output.mp4
Note : it’s better to stack video vertically. Horizontal stack does works but then some services (like twitter) won’t accept the video because the image ratio will be too extreme.
The -map 0:a here is necessary to select which audio you want to have.
The format mkv also allow to record multiple video stream within one file that you can selected after that :
ffmpeg -i "$(youtube-dl -g https://www.twitch.tv/alphacast)" \
-i "$(youtube-dl -g https://www.twitch.tv/colas_bim)" \
-i "$(youtube-dl -g https://www.twitch.tv/eventisfr)" \
-i "$(youtube-dl -g https://www.twitch.tv/fusiow)" \
-map 0:1 -map 1:1 -map 2:1 -map 3:1 -map 0:0 \
-c copy \
out.mkv