youtube-dl
has the ability to give you a direct download link for a video instead of downloading the whole video by itself. This is really useful if you only intend to clip a specific part of a stream, and you can even do that without reencoding for maximum speed if you can accept a tolerance of 2 seconds (the usual keyframe interval used for streams). This is a technique I am using to make my Games Done Quick clips as well, so this is not limited to YouTube.
Here's how you can do it:
- youtube-dl, ideally newest version (https://youtube-dl.org)
- ffmpeg, you can get this from https://ffmpeg.org/download.html.
- ready Windows binaries: https://www.gyan.dev/ffmpeg/builds/#release-section
- For protected streams you may need to pass cookies to youtube-dl and a tool to generate the cookie file for it, see below for details.
Public streams don't need further preparation to be directly downloaded, but protected streams require valid credentials to access them. Since most websites use cookies for this, you will usually need to pass such valid cookies to youtube-dl
via the --cookies
parameter.
An easy way to extract such valid cookies from an already logged-in browser is using an add-on on the target website such as cookies.txt for Firefox, similar plugins should exist for Chrome as well. If you use Firefox's "multi account containers," make sure you extract the cookies from the correct container.
With the file created, follow the instructions below but add --cookies path\to\your\cookies.txt
to your youtube-dl
calls. Now you should be able to access the protected streams you want to access.
-
Extract the direct stream URL with
youtube-dl
. The direct stream usually is an HLS stream which uses.m3u8
playlist URLs.youtube-dl [--cookies cookies.txt] -f best -g "https://video.url/here"
The
-g
parameter tells youtube-dl to only return the direct download link, by default it would download the whole stream by itself. I also add-f best
to get the best quality, I don't know if youtube-dl defaults to it anyways.NOTE: If you already found the direct download URL via your browser's network tab, you can skip this. You sometimes need to do this for platforms that aren't supported by youtube-dl, such as SPWN.
-
Copy this direct stream URL.
-
Now decide which part of the stream you want to clip. For our purposes, we will choose to start the clip from 2 minutes into the video (
00:02:00
) and extract the next45
seconds from it. -
Put together the
ffmpeg
command to download the part you want as an MP4 like this:ffmpeg -ss 00:02:00 -t 45 -i "<direct stream URL>" -c copy "some_filename_for_where_you_want_to_save_your_video.mp4"`
If you don't intend to limit the length or set a start time you can just leave out those respective parameters -- removing
-ss 00:02:00
results inffmpeg
starting at zero seconds and removing-t 45
will just clip up to the end of the stream.The parameter
-c copy
tells ffmpeg to just copy the already encoded video and audio from the original stream, removing this will cause ffmpeg to reencode your clip! -
After running this command and waiting for it to finish you should have a finished MP4 file!
Congratulations, you now have a clip of the stream, done without any reencoding!
If you're an expert-level lazyball, you can combine these steps into a single command that will do the exact things described above. On Windows, this can be done with PowerShell, while on Linux you can do it with bash
like this:
ffmpeg -ss "$start_time" -t "$length" -i (youtube-dl --cookies "$cookies" -f best -g "$video_url") -c copy "$target"
ffmpeg -ss "$start_time" -t "$length" -i "$(youtube-dl --cookies "$cookies" -f best -g "$video_url")" -c copy "$target"