-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeDownload.py
More file actions
44 lines (34 loc) · 1.44 KB
/
Copy pathYoutubeDownload.py
File metadata and controls
44 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#This script can be used to download the videos direct from youtube along with the subtitles
import youtube_dl
url = "https://youtu.be/OQopEV3H32M"
def download_video_srt(subs):
""" Downloads specified Youtube video's subtitles as a vtt/srt file.
Args:
subs(str): Full url of Youtube video
Returns:
True
The video will be downloaded as 1.mp4 and its subtitles as 1.(lang).srt
Both, the video and its subtitles, will be downloaded to the same location
as that of this script (sum.py)
"""
ydl_opts = {
'format': 'best',
'outtmpl': '1.%(ext)s',
'subtitlesformat': 'srt',
'writeautomaticsub': True,
# 'allsubtitles': True # Get all subtitles
}
movie_filename = ""
subtitle_filename = ""
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
# ydl.download([subs])
result = ydl.extract_info("{}".format("https://youtu.be/OQopEV3H32M"), download=True)
movie_filename = ydl.prepare_filename(result)
subtitle_info = result.get("requested_subtitles")
subtitle_language = subtitle_info.keys()[0]
subtitle_ext = subtitle_info.get(subtitle_language).get("ext")
subtitle_filename = movie_filename.replace(".mp4", ".%s.%s" %
(subtitle_language,
subtitle_ext))
return movie_filename, subtitle_filename
download_video_srt(url)