Search code examples
python-3.xyoutubesubtitle

Why can't download subtitle with pure python?


Download the subtitle with youtube-dl command:

url="https://www.youtube.com/watch?v=Ix8xPfKDxNg"
youtube-dl --write-auto-sub --skip-download --sub-lang en  $url
[info] Writing video subtitles to: Speak English Confidently & Clearly with Ellen-Ix8xPfKDxNg.en.vtt

Now i want to do the same task with pure python code.

from __future__ import unicode_literals
import youtube_dl
url="https://www.youtube.com/watch?v=Ix8xPfKDxNg"
options = {
        'writeautomaticsub': True,
        'subtitleslangs': ['en'],
        'skip_download': True,
        'subtitleslangs': 'en'
}
with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download([url])

The error info:

WARNING: e subtitles not available for Ix8xPfKDxNg
WARNING: n subtitles not available for Ix8xPfKDxNg

Why can't replace the bash command

youtube-dl --write-auto-sub --skip-download --sub-lang en  $url

with pure python code?


Solution

  • Removing the following should fix it for you, 'subtitleslangs': 'en'. As you have already included it in as 'subtitleslangs': ['en']

    options = {
            'writeautomaticsub': True,
            'subtitleslangs': ['en'],
            'skip_download': True
    }
    

    subtitleslangs needs to be included within [].

    I also found adding the option to list all the available options 'listsubtitles': True then adding the subtitleslangs after helps too.