-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosubtitle.py
59 lines (52 loc) · 2.01 KB
/
autosubtitle.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from requests import get
import os
import yt_dlp as youtube_dl
from yt_dlp import YoutubeDL
from youtube_search import YoutubeSearch
YDL_OPTIONS_VIDEO = {
"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"noplaylist": "True",
}
YDL_OPTIONS_AUDIO = {
"format": "bestaudio/best",
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "wav",
}
],
"noplaylist": "True",
}
def download_video_and_audio(song, time_length):
results = YoutubeSearch(song, max_results=15).to_dict()
for data in results:
if len(data["duration"].split(":")) < 3:
time = (int(data["duration"].split(":")[0]) * 60) + int(
data["duration"].split(":")[1]
)
if abs(int(time) - int(time_length)) < 3:
title = data["title"]
with YoutubeDL(YDL_OPTIONS_VIDEO) as ydl_video, YoutubeDL(
YDL_OPTIONS_AUDIO
) as ydl_audio:
try:
get(title)
except:
video_info = ydl_video.extract_info(
f"ytsearch:{title}", download=True
)["entries"][0]
audio_info = ydl_audio.extract_info(
f"ytsearch:{title}", download=True
)["entries"][0]
else:
video_info = ydl_video.extract_info(title, download=True)
audio_info = ydl_audio.extract_info(title, download=True)
filename_video = os.path.splitext(
ydl_video.prepare_filename(video_info)
)[:-1]
filename_audio = os.path.splitext(
ydl_audio.prepare_filename(audio_info)
)[:-1]
break
return "".join(filename_video) + ".mp4", "".join(filename_audio) + ".wav"
video_file, audio_file = download_video_and_audio("The Wait", 60)