Skip to content

Instantly share code, notes, and snippets.

@icedream
Last active March 25, 2022 13:31
Show Gist options
  • Save icedream/a95aa8d85dbe219223cbf94f02fbd1be to your computer and use it in GitHub Desktop.
Save icedream/a95aa8d85dbe219223cbf94f02fbd1be to your computer and use it in GitHub Desktop.
Clipping without reencoding

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:

Requirements

Accessing protected streams

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.

Downloading a specific part of a stream

  1. 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.

  2. Copy this direct stream URL.

  3. 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 next 45 seconds from it.

  4. 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 in ffmpeg 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!

  5. 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!

I am extra lazy, give me a one-liner

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:

Windows

ffmpeg -ss "$start_time" -t "$length" -i (youtube-dl --cookies "$cookies" -f best -g "$video_url") -c copy "$target"

Linux

ffmpeg -ss "$start_time" -t "$length" -i "$(youtube-dl --cookies "$cookies" -f best -g "$video_url")" -c copy "$target"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment