‘The half minute which we daily devote to the winding-up of our watches is an exertion of labour almost insensible; yet, by the aid of a few wheels, its effect is spread over the whole twenty-four hours.’

youtube-dl bash shell function: prefer mp4, and timestamp with download time

Benjamin Slade

In case it’s useful, a quick Bash shell function which takes a single argument (a web address that is processable by youtube-dl) and returns the best quality mp4 version (in case you need to deal with a device that doesn’t like modern video encodings/containers), with the download time as the file’s modification time timestamp (useful if you have a directory of downloaded videos and want to quickly see the last N files you downloaded, rather than the files being sorted by upload time).

Add to your ~/.bashrc or other shell configuration file as appropriate:

youtubemp4() {
    youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' "$1"
    touch "$(youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' --get-filename $1)"

Then call in the command-line, e.g. youtubemp4 https://www.youtube.com/watch?v=Gnnb6sjgk3A.