Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Last active October 21, 2023 16:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lelandbatey/11100493 to your computer and use it in GitHub Desktop.
Save lelandbatey/11100493 to your computer and use it in GitHub Desktop.
YouTube 1080p Download Script

YouTube 1080p Downloader

The Problem

It used to be you could directly download any YouTube video in any quality you wanted, as a single .mp4 file. However, around a year ago, YouTube switched from the "single file stream", to "DASH" streaming, which streams the video and the audio to you as two separate streams, which are played in sync with each other in the YouTube player.

It's still possible to download YouTube videos as a single file, but YouTube only offers that for qualities up to 720p. So you can't download "single file stream" videos in 1080p or higher.

The Solution

This script downloads the audio and video as separate streams, then uses FFMPEG to re-combine them into a single video file.

Requirements

  • youtube-dl (pip install youtube-dl)
    • I recommend installing this through pip instead of through apt-get because the one in the official repositories seems to be outdated. pip will ensure you have the most up to date version of youtube-dl
  • ffmpeg
    • WILL NOT WORK if you use the ffmpeg found in the Ubuntu repository. That one is old and weird, and will not work with this script. I ended up compiling my own ffmpeg (as described here for Ubuntu) which only took about 10 minutes.

Script Contents

# YouTube 1080p Video Downloader

# Sets up a variable to track where we were at the start of all this
ORIGINAL_DIR=$(echo $PWD)
ORIGINAL_DIR+="/"

# Sets up temporary directory for the conversion process.
TEMPDIR=$(mktemp -d)

# Go to the temporary directory. It will be our "scratchspace" so we don't accidentally mess with other files
cd $TEMPDIR

echo -e "INVOKED WITH:\n\t$0 $1 $2 $3 $4 $5 $6"
echo "Tempdir: $TEMPDIR"
echo "Original dir: $ORIGINAL_DIR"

# Downloads the video and audio for the video in separate streams
youtube-dl -f 140 "$1" # Downloads audio
youtube-dl -f 137 "$1" # Downloads video


# String flogging for simple rename
x=$(basename *.mp4) # Only way I know of to get x to equal "*.mp4"
NEW_FILENAME="${x%-*}" # Extracts everything prior to the '-' character (the name of the video, without the YT ID or file extension)
NEW_FILENAME+="-1080p.mp4"

ffmpeg -i *.mp4 -i *.m4a -map 0 -map 1 -acodec copy -vcodec copy -shortest "$NEW_FILENAME" # Combines files into 1080 video with sound

# Move the newly created video back to our original directory, then move there
mv "$NEW_FILENAME" $ORIGINAL_DIR
cd $ORIGINAL_DIR 

# Delete our temporary directory with it's working files
rm -rf "$TEMPDIR"
@AdrianSimionov
Copy link

You know you can use -f 137+140, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment