3

I have tried to download subtitles along with the video using the following Python 3.x code. It's just not working.

Here's my code:

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'outtmpl': '/PATH/%(title)s'+'.mp4',
'format':' (bestvideo[width>=?1080]/bestvideo)+bestaudio/best',
'subtitle': '--write-srt --sub-lang en',
}
url = input("Enter your URL: ")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])
print("Downloaded!")
Sakib Arifin
  • 255
  • 2
  • 13
Raj
  • 69
  • 1
  • 3
  • 11
  • 1
    The --write-srt option is for downloading subtitles uploaded by owner, if the video doesn't have any subtitles uploaded by owner it may not work. Try --write-auto-sub. – knoftrix Apr 05 '20 at 13:59
  • That makes sense. – Raj Oct 01 '20 at 19:13

1 Answers1

4

You need to set 'writesubtitles': True for youtube-dl to download subtitles. Also, you should specify [ext=mp4] or the program may download .webm files which are incompatible with mp4 format. The following code solves these problems:

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'outtmpl': '/Downloads/%(title)s_%(ext)s.mp4',
'format': '(bestvideo[width>=1080][ext=mp4]/bestvideo)+bestaudio/best',
'writesubtitles': True,
'subtitle': '--write-sub --sub-lang en',
}
url = input("Enter your URL: ")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])
print("Download Successful!")

Also, make sure to keep ffmpeg.exe in your youtube-dl folder for merging video and audio files. You can get it from here.

Sakib Arifin
  • 255
  • 2
  • 13