====== Youtube HD 1080 (DASH) ======
required: ''youtube-dl'' and ''ffmpeg''
This script creates a folder in ''/tmp/'' (which I have mounted as ramdisk), downloads the separate video and audio streams (see [[https://en.wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP|about DASH]]), copies them into a Matroska container, sets the modtime and moves the resulting file in the current directory.
I recommend putting an alias in ''.bashrc'' for easier use (I set it as ''ytd''). example: ytd https://www.youtube.com/watch?v=yVpbFMhOAwE
#!/bin/bash
hashfold=$(echo "$1" | md5sum)
outdir=$(pwd)
mkdir -p "/tmp/youtube-dash/$hashfold"
cd "/tmp/youtube-dash/$hashfold"
# downloading the video stream (MP4 1080p, else fallback 720p)
youtube-dl -f 137/136 -o "/tmp/youtube-dash/$hashfold/%(title)s.zvideo" "$1"
# downloading the audio stream (MP4A-AAC)
youtube-dl -f 140 -o "/tmp/youtube-dash/$hashfold/%(title)s.zaudio" "$1"
for file in *.zvideo; do
filemtime=$(stat -c %Y "$file")
ffmpeg -i "$file" -i "${file%.??????}.zaudio" -c:v copy -c:a copy "${file%.??????}.mkv"
touch -d "@$filemtime" "${file%.??????}.mkv"
mv "${file%.??????}.mkv" "$outdir/"
rm -r "/tmp/youtube-dash/$hashfold"
done