Spaces:
Runtime error
Runtime error
File size: 2,679 Bytes
8b5af58 b3d5047 8b5af58 b3d5047 8b5af58 b3d5047 8b5af58 b3d5047 8b5af58 2af14b0 8b5af58 31743f4 8b5af58 |
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 |
import shutil
import subprocess
from uuid import uuid4
from tempfile import TemporaryDirectory
from .srt_to_ssml import sub_to_ssml
from .s3_handler import upload_file
from .text_to_speech import synthesize_ssml_to_speech
def audio_handler(audio, from_lang="en", to_lang="hi"):
with TemporaryDirectory(dir=".") as tempdir:
id = str(uuid4()).replace("-", "")
audio_file = f'{tempdir}/audio.wav'
srt_file = f'{tempdir}/audio.srt'
ssml_file = f'{tempdir}/ssml.txt'
translated_audio = f'{tempdir}/translated_audio.wav'
with open(audio_file, "wb") as buffer:
shutil.copyfileobj(audio, buffer)
audio_to_srt(audio_file)
sub_to_ssml(srt_file, ssml_file, "hi-IN", "hi-IN-SwaraNeural", "Female", from_lang, to_lang)
srt_url = upload_file(srt_file, "expressapi", id, "subtitle.srt")
ssml_url = upload_file(ssml_file, "expressapi", id, "ssml.txt")
return {"id":id, "srt_url":srt_url, "ssml_url": ssml_url}
def audio_url_handler(url: str, from_lang="en", to_lang="hi"):
with TemporaryDirectory(dir=".") as tempdir:
id = str(uuid4()).replace("-", "")
input_audio = f'{tempdir}/audio.m4a'
audio_file = f'{tempdir}/audio.wav'
srt_file = f'{tempdir}/audio.srt'
ssml_file = f'{tempdir}/ssml.txt'
translated_audio = f'{tempdir}/translated_audio.wav'
download_audio(url, input_audio)
m4a_to_wav(input_audio, audio_file)
audio_to_srt(audio_file)
ssml = sub_to_ssml(srt_file, ssml_file, "hi-IN", "hi-IN-SwaraNeural", "Female", from_lang, to_lang)
synthesize_ssml_to_speech(ssml, translated_audio)
srt_url = upload_file(srt_file, "expressapi", id, "subtitle.srt")
ssml_url = upload_file(ssml_file, "expressapi", id, "ssml.txt")
translated_audio_url = upload_file(translated_audio, "expressapi", id, "translated_audio.wav")
return {"id":id, "srt_url":srt_url, "ssml_url": ssml_url, "translated_audio_url":translated_audio_url}
def download_audio(link, output):
command = ["yt-dlp", "-f", "ba*[ext=m4a]", "-o", output, link]
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("Audio file downloaded: ", link)
def m4a_to_wav(input, output):
command = ["ffmpeg", "-i", input, output]
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(f"m4a to wav converted, Input: {input}, Output: {output}")
def audio_to_srt(audio_file):
command = ["las", "gen", "-s", audio_file]
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("audio to srt converted") |