Skip to content

Instantly share code, notes, and snippets.

@rsato rsato/yt-dl.sh
Last active Mar 20, 2019

Embed
What would you like to do?
Download YouTube video (youtube-dl wrapper)
#!/bin/sh
# Dependencies:
# - youtube-dl
# - ffmpeg
# Preparing a "list.txt" that is a list of YouTube video IDs.
# (Video ID is a value of "v" key in YouTube URL: youtube.com/watch?v=video_id )
# Tips:
# - To expand IDs in the YouTube playlist, use "youtube-dl --get-id" command.
# $ youtube-dl --get-id https://www.youtube.com/playlist?list=list_id > list.txt
for i in $(cat list.txt); do
# show ID and title
echo $i
youtube-dl -e https://www.youtube.com/watch?v=$i
# download video
# format code extension resolution note
# 160 mp4 256x144 144p 117k , avc1.4d400c, 30fps, video only, 19.93MiB
# 136 mp4 1280x720 720p 2638k , avc1.4d401f, 30fps, video only, 437.53MiB
# 298 mp4 1280x720 720p60 4068k , avc1.4d4020, 60fps, video only, 654.95MiB
# 137 mp4 1920x1080 1080p 4949k , avc1.640028, 30fps, video only, 797.54MiB
# 299 mp4 1920x1080 1080p60 6766k , avc1.64002a, 60fps, video only, 1.08GiB
filename_mp4=$(youtube-dl -f 160 --get-filename https://www.youtube.com/watch?v=$i)
for fmt in 299 137 298 136 ; do
if [ ! -e "$filename_mp4" ]; then
youtube-dl -i -w -f $fmt https://www.youtube.com/watch?v=$i
fi
done
# download audio
# format code extension resolution note
# 140 m4a audio only DASH audio 129k , m4a_dash container, mp4a.40.2@128k, 25.73MiB
filename_m4a=$(youtube-dl -f 140 --get-filename https://www.youtube.com/watch?v=$i)
youtube-dl -i -w -f 140 https://www.youtube.com/watch?v=$i
# combine mp4 + m4a to out directory
mkdir -p out
if [ -e "$filename_mp4" -a -e "$filename_m4a" ]; then
ffmpeg -i "$filename_mp4" -i "$filename_m4a" -c copy "out/$filename_mp4"
fi
# clean up
rm "$filename_mp4"
rm "$filename_m4a"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.