4 ms·
#!/bin/sh ttml2srt() { x=$(echo x|tr x '\34'); tr -d '\34' \ |sed -n "/<p begin/{ s/<p begin=\"//; s/\" end=\"/ --> /;
by 1vuio0pswjnm7 4y ago
#!/bin/sh
ttml2srt()
{
x=$(echo x|tr x '\34');
tr -d '\34' \
|sed -n "/<p begin/{
s/<p begin=\"//;
s/\" end=\"/ --> /;
s/\" style=\"s2\">/$x/;
s#</p>##p;}" \
|sed = \
|sed "/$x/!s/^/$x/" \
|tr '\34' '\12' \
|sed '/[ ]-->[ ]/s/\./,/g'
}
read x;
case $x in
https://www.youtube.com/watch?v=??????????\
|https://www.youtube.com/watch?v=???????????\
|https://youtu.be/???????????\
|https://youtu.be/??????????)
f=${x##*=};f=${f##*/};case ${#f} in 10|11)
curl -4o $f.mp4 $x
video=$(tr \{ '\12' < $f.mp4|sed -n "/itag=22/{s/u0026/\&/g;s/%3D/=/g;s/%2C/,/g;s/%26/\&/g;s/.*url\":\"//;s/\".*//p;}"|tr -d '\134')
test $video||exit
ttml=$(tr \{ '\12' < $f.mp4 |sed -n '/timedtext/{s/u0026/\&/g;s/.*:\"//;s/\".*//;s/$/\&fmt=ttml/p;q;}'|tr -d '\134')
test $ttml||exit
curl -s4 $ttml|ttml2srt > $f.srt
exec ffmpeg -v quiet -y -i $video -vf subtitles=$f.srt $f.mp4
esac
esac
exit
The script above, "1.sh", can be used as follows.
echo https://www.youtube.com/watch?v=aeWyp2vXxqA | 1.sh
It will download the captions as .srt and then "hardsub" them into the video as it dowloads the .mp4. NB. This is slow YouTube downloading without using yt-dl/yt-dlp. Obviously, it will not work with commercial videos.
The .srt file is saved as [YouTube ID].srt and the video as [YouTube ID].mp4, where [YouTube ID] is a 10 or 11 ASCII character string.
The video format is itag=22, i.e., mp4/720p. Not all videos will have 22 of course. I usually try itag=18, mp4/360p, if 22 is not available. Change the format to whatever is preferred.
Looking around for a .ttml/.vtt/.srv[1-3] to srt converter I found solutions that required installing Python or some other large scripting language. On GitHub I found a project called "astisub" that will convert from ttml or vtt to srt. It is a 3.8M Go binary. I wrote a shell function instead.