How can get the subtitle with yt-dlt's python script?

Install yt-dip to work:

pip install  yt-dlp  

Get the subtitle in youtube with bash command:

url="https://www.youtube.com/watch?v=ZrQJaLemaoE"
yt-dlp  --write-auto-sub  --sub-lang en --skip-download  $url

How can get the substitle with pure python script?

import yt_dlp
url="https://www.youtube.com/watch?v=ZrQJaLemaoE"
ydl_opts= {}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    #some lines here to get the subtitle?

You fill out the ydl_opts with corresponding values, eg:

 ydl_opts = dict(
     write_auto_sub=True,
     sub_lang='en',
     skip_download=True,
 )

Cheers,
Cameron Simpson cs@cskk.id.au

With bash command:

url=“https://www.youtube.com/watch?v=ZrQJaLemaoE
yt-dlp --write-auto-sub --sub-lang en --skip-download $url --output /tmp/sub.txt

The substitute will be downloaded in /tmp/sub.txt.

import yt_dlp
url="https://www.youtube.com/watch?v=ZrQJaLemaoE"
ydl_opts = {
     "write_auto_sub":True,
     "sub_lang":'en',
     "skip_download":True,
     "output":'/tmp/sub.txt',
    }
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    ydl.download(url)

Why ydl.download(url) can’t download substitute ?