srt / app /audio_handler.py
badalsahani's picture
Update app/audio_handler.py
31743f4
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")