Skip to content

Instantly share code, notes, and snippets.

@rsato
Last active April 8, 2023 06:30
Show Gist options
  • Save rsato/1955c455c2841f33add7ee10e58f46e9 to your computer and use it in GitHub Desktop.
Save rsato/1955c455c2841f33add7ee10e58f46e9 to your computer and use it in GitHub Desktop.
Download YouTube video and video (yt-dlp wrapper)
#!/bin/sh
# Dependencies:
# - yt-dlp https://github.com/yt-dlp/yt-dlp
# - ffmpeg
# Prepare the "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 extract all the video IDs from a YouTube playlist, use "--get-id" option.
# $ yt-dlp --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
yt-dlp -e https://www.youtube.com/watch?v=$i
# Download video
# ID EXT RESOLUTION FPS CH │ FILESIZE TBR PROTO │ VCODEC VBR ACODEC ABR ASR MORE INFO
# 597 mp4 256x144 15 │ 1.26MiB 33k https │ avc1.4d400b 33k video only 144p, mp4_dash
# 136 mp4 1280x720 30 │ 85.48MiB 2207k https │ avc1.4d401f 2207k video only 720p, mp4_dash
# 398 mp4 1280x720 60 │ 56.24MiB 1452k https │ av01.0.08M.08 1452k video only 720p60, mp4_dash
# 298 mp4 1280x720 60 │ 131.96MiB 3407k https │ avc1.4d4020 3407k video only 720p60, mp4_dash
# 399 mp4 1920x1080 60 │ 102.53MiB 2648k https │ av01.0.09M.08 2648k video only 1080p60, mp4_dash
# 299 mp4 1920x1080 60 │ 222.07MiB 5734k https │ avc1.64002a 5734k video only 1080p60, mp4_dash
# 400 mp4 2560x1440 60 │ 222.03MiB 5733k https │ av01.0.12M.08 5733k video only 1440p60, mp4_dash
# 401 mp4 3840x2160 60 │ 458.57MiB 11841k https │ av01.0.13M.08 11841k video only 2160p60, mp4_dash
# 571 mp4 7680x4320 60 │ 978.30MiB 25262k https │ av01.0.17M.08 25262k video only 4320p60, mp4_dash
filename_mp4=$(yt-dlp -f 597 --get-filename https://www.youtube.com/watch?v=$i)
for fmt in 571 401 400 299 399 298 398 136; do
if [ ! -e "$filename_mp4" ]; then
yt-dlp -i -w -f $fmt https://www.youtube.com/watch?v=$i
fi
done
# Download audio
# format code extension resolution note
# 599 m4a audio only 2 │ 1.19MiB 31k https │ audio only mp4a.40.5 31k 22k ultralow, m4a_dash
# 139 m4a audio only 2 │ 1.89MiB 49k https │ audio only mp4a.40.5 49k 22k low, m4a_dash
# 140 m4a audio only 2 │ 5.02MiB 129k https │ audio only mp4a.40.2 129k 44k medium, m4a_dash
filename_m4a=$(yt-dlp -f 599 --get-filename https://www.youtube.com/watch?v=$i)
for fmt in 140 139 599; do
if [ ! -e "$filename_m4a" ]; then
yt-dlp -i -w -f $fmt https://www.youtube.com/watch?v=$i
fi
done
# Combine video(mp4) and audio(m4a) to "out" directory
mkdir -p out
if [ -e "$filename_mp4" -a -e "$filename_m4a" ]; then
ffmpeg -y -i "$filename_mp4" -i "$filename_m4a" -c copy "out/$filename_mp4"
fi
# clean up
rm "$filename_mp4"
rm "$filename_m4a"
done
@sunyta2
Copy link

sunyta2 commented Aug 27, 2019

impressive! wow!

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