-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_enhancer.py
164 lines (117 loc) · 5.02 KB
/
sound_enhancer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import shutil
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException
from webdriver_manager.chrome import ChromeDriverManager
from moviepy.editor import VideoFileClip, AudioFileClip
from pydub import AudioSegment
def prepare_dirs() -> None:
shutil.rmtree("./.tmp", ignore_errors=True)
os.mkdir("./.tmp")
try:
os.mkdir("./results")
except FileExistsError:
pass
def get_driver_options() -> Options:
options = Options()
options.add_argument("--start-maximized")
prefs = {"download.default_directory": "/home/metju/Projects/Sound-enhancer/.tmp"}
options.add_experimental_option("prefs", prefs)
return options
def wait_for_download() -> None:
downloading = True
while downloading:
time.sleep(1)
downloading = False
files = os.listdir("./.tmp")
for file in files:
if file.endswith(".crdownload"):
downloading = True
def download_lecture(link: str) -> None:
driver_options = get_driver_options()
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
options=driver_options)
driver.get(link)
dialog_box = driver.switch_to.alert
dialog_box.accept()
print("Lecture downloading started")
wait_for_download()
file_name = os.listdir("./.tmp")[0]
assert file_name.endswith(".mp4")
os.rename("./.tmp/" + file_name, "./.tmp/lecture.mp4")
print("Lecture downloading finished")
driver.quit()
def extract_audio() -> None:
print("Audio extraction started")
video = VideoFileClip("./.tmp/lecture.mp4")
video.audio.write_audiofile("./.tmp/lecture.mp3", verbose=False, logger=None)
print("Audio extraction finished")
print("Audio cutting started")
audio = AudioSegment.from_mp3("./.tmp/lecture.mp3")
assert audio.duration_seconds <= 59 * 2 * 60
cut = audio[:59 * 60 * 1000]
cut.export("./.tmp/cut01.mp3", format="mp3")
cut = audio[59 * 60 * 1000:]
cut.export("./.tmp/cut02.mp3", format="mp3")
print("Audio cutting finished")
def enhance_audio(login: str, password: str, filepath: str) -> None:
driver_options = get_driver_options()
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
options=driver_options)
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue=' +
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1' +
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(15)
driver.find_element("xpath", '//*[@id ="identifierId"]').send_keys(login)
driver.find_elements("xpath", '//*[@id ="identifierNext"]')[0].click()
driver.find_element("xpath", '//*[@id ="password"]/div[1]/div / div[1]/input').send_keys(password)
driver.find_elements("xpath", '//*[@id ="passwordNext"]')[0].click()
# state: Logged in Google account
# action: Go to Adobe page
driver.get("https://podcast.adobe.com/enhance")
driver.find_element("xpath", "//button[contains(text(),'Sign in')]").click()
driver.find_element("xpath", "//span[contains(text(),'Continue with Google')]").click()
driver.find_element("xpath", "//input[@type='file']").send_keys(os.getcwd() + filepath)
print("Audio enhancing started")
while True:
time.sleep(1)
try:
driver.find_element("xpath", "//span[contains(text(),'Download')]").click()
break
except NoSuchElementException:
continue
print("Audio enhancing finished")
wait_for_download()
def join_audio() -> None:
print("Audio joining started")
first = AudioSegment.from_wav("./.tmp/cut01 (enhanced).wav")
second = AudioSegment.from_wav("./.tmp/cut02 (enhanced).wav")
joined = first + second
joined.export("./.tmp/joined.mp3")
print("Audio joining finished")
def join_audio_and_video(filename: str) -> None:
print("Audio and video joining started")
video = VideoFileClip("./.tmp/lecture.mp4")
audio = AudioFileClip("./.tmp/joined.mp3")
joined = video.set_audio(audio)
joined.write_videofile(f"./results/{filename}.mp4", verbose=False, logger=None)
print("Audio and video joining finished")
def remove_tmp_dir() -> None:
shutil.rmtree("./.tmp", ignore_errors=True)
if __name__ == '__main__':
GOOGLE_LOGIN = "FILL IN"
GOOGLE_PASSWORD = "FILL IN"
download_link = input("Enter lecture download link:")
output_file_name = input("Enter output file name (without '.mp4'):")
prepare_dirs()
download_lecture(download_link)
extract_audio()
enhance_audio(GOOGLE_LOGIN, GOOGLE_PASSWORD, "/.tmp/cut01.mp3")
enhance_audio(GOOGLE_LOGIN, GOOGLE_PASSWORD, "/.tmp/cut02.mp3")
join_audio()
join_audio_and_video(output_file_name)
remove_tmp_dir()
print(f"You can find the result in ./results/{output_file_name}.mp4")