Spaces:
Sleeping
Sleeping
File size: 2,458 Bytes
2f2406a |
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 |
from datetime import datetime
from uuid import uuid4
from tempfile import TemporaryDirectory
from .s3_handler import upload_file
from app.scripts import synthesise_audio
from .helper import download_audio, download_video, m4a_to_wav, audio_to_srt, merge_video_audio
from app.constants import MALE_LANGUAGES, FEMALE_LANGUAGES
def handler_video_url(url, from_lang, to_lang, gender):
with TemporaryDirectory(dir=".") as tempdir:
srt_file = f"{tempdir}/audio.srt"
video_file = f"{tempdir}/video.mp4"
audio_file = f"{tempdir}/audio.m4a"
audio_wav_file = f"{tempdir}/audio.wav"
translated_video = f"{tempdir}/translated_video.mp4"
download_audio(url, audio_file)
download_video(url, video_file)
m4a_to_wav(audio_file, audio_wav_file)
language_code = MALE_LANGUAGES[from_lang][0]
audio_to_srt(language_code, audio_wav_file, srt_file)
if gender.lower() == "male":
language_code = MALE_LANGUAGES[to_lang][0]
voice_name = MALE_LANGUAGES[to_lang][1]
else:
language_code = FEMALE_LANGUAGES[to_lang][0]
voice_name = FEMALE_LANGUAGES[to_lang][1]
result = synthesise_audio(
srt_file=srt_file,
video_file=video_file,
output_folder=tempdir,
language_code=language_code,
voice_name=voice_name,
from_lang=from_lang,
to_lang=to_lang,
gender=gender,
)
translated_srt = result["translated_subtitle"]
translated_audio = result["translated_audio"]
merge_video_audio(video_file, translated_audio, translated_video)
now = datetime.now()
today = now.strftime("%Y-%m-%d")
id = f"{today}/{str(uuid4()).replace('-', '')[:15]}"
srt_url = upload_file(srt_file, "expressapi", id, "subtitle.srt")
translated_srt_url = upload_file(
translated_srt, "expressapi", id, "translated_subtitle.srt"
)
translated_audio_url = upload_file(
translated_audio, "expressapi", id, "translated_audio.mp3"
)
translated_video_url = upload_file(translated_video, "expressapi", id, "translated_video.mp4")
return {
"srt_url": srt_url,
"video_url": translated_video_url,
"translated_srt_url": translated_srt_url,
"translated_audio_url": translated_audio_url,
}
|