mirror of
https://github.com/FAUSheppy/ths-speech
synced 2025-12-09 07:08:33 +01:00
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import base64
|
|
import os.path
|
|
from pydub import AudioSegment
|
|
|
|
def save_audio(filename, base64_string):
|
|
decoded = None
|
|
orig_filename = filename[:-4]+"_orig"
|
|
try:
|
|
decoded = base64.b64decode(base64_string)
|
|
except TypeError:
|
|
return b"ERROR_INVALID_ENCODING_64"
|
|
#if os.path.isfile(filename):
|
|
# return b"ERROR_FILE_EXISTS"
|
|
with open(orig_filename,"wb") as f:
|
|
f.write(decoded)
|
|
AudioSegment.from_file(orig_filename).export(filename,format="wav")
|
|
return b"SUCCESS"
|
|
|
|
def save_audio_chain(filenames, base64_strings):
|
|
compleAudio = None
|
|
for fname in filenames:
|
|
decoded = None
|
|
orig_filename = fname[:-4]+"_orig"
|
|
try:
|
|
decoded = base64.b64decode(base64_string)
|
|
except TypeError:
|
|
return b"ERROR_INVALID_ENCODING_64"
|
|
with open(orig_filename,"wb") as f:
|
|
f.write(decoded)
|
|
if compleAudio == None:
|
|
completeAudio = AudioSegment.from_file(orig_filename)
|
|
else:
|
|
completeAudio += [AudioSegment.from_file(orig_filename)]
|
|
completeAudio.export(filenames[0],format="wav")
|
|
|
|
def save_transcript(filename, transcript):
|
|
if os.path.isfile(filename):
|
|
pass
|
|
with open(filename + "_transcript","w") as f:
|
|
f.write(transcript)
|