-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
80 lines (63 loc) · 2.47 KB
/
manage.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
import asyncio
import os
import sys
import app.utils as utils
from app.butcher.youtube import YoutubeButcher
from app.config import config
from app.rabbitmq import RabbitMQContext
from app.synthesizer.ssml import SSMLConverter
from app.transcriber.video_processor import VideoProcessorContext
logger = utils.get_logger(__name__)
def manage_event(body):
source = body.decode("utf-8")
YoutubeButcher.download_source(
source=source,
output_path=config.downloads_directory,
)
with utils.DirectoryManager(config.downloads_directory) as files:
for file in files:
with VideoProcessorContext(file) as processor:
ssml_filename = utils.get_file_name_without_extension(file)
audio_file_path = processor.extract_audio(config.audio_directory)
language, segments = processor.transcribe(
audio=audio_file_path,
model_name="large-v3",
language=config.localization,
)
subtitle_file_path = processor.generate_subtitle_file(
language,
segments,
config.subtitles_directory,
)
SSMLConverter(
srt_file=subtitle_file_path,
output_file=config.ssml_directory + f"/{ssml_filename}.txt",
voice_name="en-US-DavisNeural",
).convert()
processor.add_subtitle_to_video(
embedded_subtitles=True,
subtitle_file_path=subtitle_file_path,
subtitle_language=language,
output_directory=config.results_directory,
)
utils.delete_file(subtitle_file_path)
utils.delete_file(audio_file_path)
utils.delete_file(file)
async def main():
with RabbitMQContext() as rabbit:
while True:
rabbit.consume_message(manage_event)
if __name__ == "__main__":
try:
utils.create_directory(config.downloads_directory)
utils.create_directory(config.results_directory)
utils.create_directory(config.subtitles_directory)
utils.create_directory(config.audio_directory)
utils.create_directory(config.ssml_directory)
asyncio.run(main())
except KeyboardInterrupt:
print("Interrupted")
try:
sys.exit(0)
except SystemExit:
os._exit(0)