How to Download Videos from YouTube and Embed Subtitles in Batch

All I want to do is download some courses that are very helpful for me on YouTube, embed Zh-Hans subtitles in the videos, and then submit them to Chinese websites that do not support external subtitles.

This can help Chinese students who are not fluent in English and cannot access YouTube.

Youtube-dl config

The file path is ~/.config/youtube-dl/config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--o '%(playlist_index)02ds - %(title)s.%(ext)s'
--no-mtime
--proxy socks5://127.0.0.1:1080

--ignore-errors

-f bestvideo[height<=?1080]+bestaudio/best

--merge-output-format 'mkv'

--write-auto-sub --sub-lang 'zh-Hans'

--convert-subs 'ass'

--embed-subs

Download in Batch

1
2
3
4
5
6
7
8
9
playlist='https://www.youtube.com/playlist?list=...'
num=70
i=1
while (($i <= $num))
do
end=$(( i+1 ))
youtube-dl --playlist-start $i --playlist-end $end $playlist > "log-$i-$end.txt" 2>&1 &
i=$(($end+1))
done

Batch Translate File Names

After downloading all the videos in the list, use

ls | grep mkv | sed "s/\.mkv//"

Then copy and paste it to Google to get the translated text, and save it to zh.txt

Embed Subtitles in Batch

1
2
3
4
5
6
7
8
9
10
11
tmp="tmp.mkv"
mkdir -p embedSub
for f in *.mkv; do
read -r zh <&3
echo "$f\n$zh"
# ass="$( echo $f | sed 's/\.mkv//').zh-Hans.ass"
cp $f $tmp
ffmpeg -nostats -loglevel 24 -n -i $tmp -vf subtitles=$tmp "embedSub/$zh.mkv";
done 3<zh.txt

rm $tmp

On an Intel Core i7, it usually took me a whole night to record 20 videos, and it took 20 minutes to embed subtitles in an hour-long video.

Translated by gpt-3.5-turbo