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.
This script downloads the audio and video as separate streams, then uses FFMPEG to re-combine them into a single video file.
youtube-dl (pip install youtube-dl)
- I recommend installing this through
pip
instead of throughapt-get
because the one in the official repositories seems to be outdated.pip
will ensure you have the most up to date version ofyoutube-dl
- I recommend installing this through
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 ownffmpeg
(as described here for Ubuntu) which only took about 10 minutes.
- WILL NOT WORK if you use the
# 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"