Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Dec 24, 2024
1 parent 5ac587f commit 9eeed51
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions examples/customizations/marytts_mp3_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,48 @@ def generate(self: "MaryTtsMp3Generator") -> bytes:
# Initialize with args
mary_tts = MaryTTS(locale=self.locale, voice=self.voice)

# Generate temporary filename for WAV file
filename = tempfile.NamedTemporaryFile(
prefix="_merytts_", suffix=".wav"
).name

# Write WAV file
with open(filename, "wb") as file:
file.write(mary_tts.speak(self.content))

# Convert WAV to MP3
ffmpeg.input(filename).output(filename + ".mp3").run()

with open(filename + ".mp3", "rb") as _fake_file:
return_value = _fake_file.read()
# Create temporary WAV file in memory
with tempfile.NamedTemporaryFile(
delete=False,
suffix=".wav",
) as temp_wav:
temp_wav.write(mary_tts.speak(self.content))
temp_wav_path = temp_wav.name

# Convert WAV to MP3 and store in memory
with tempfile.NamedTemporaryFile(
delete=False,
suffix=".mp3",
) as temp_mp3:
ffmpeg.input(temp_wav_path).output(temp_mp3.name).run()

# Read the MP3 file content into memory
with open(temp_mp3.name, "rb") as mp3_file:
mp3_content = mp3_file.read()

# Clean up temporary files
os.remove(filename)
os.remove(filename + ".mp3")

return return_value
os.remove(temp_wav.name)
os.remove(temp_mp3.name)

return mp3_content

# # Generate temporary filename for WAV file
# filename = tempfile.NamedTemporaryFile(
# prefix="_merytts_", suffix=".wav"
# ).name
#
# # Write WAV file
# with open(filename, "wb") as file:
# file.write(mary_tts.speak(self.content))
#
# # Convert WAV to MP3
# ffmpeg.input(filename).output(filename + ".mp3").run()
#
# with open(filename + ".mp3", "rb") as _fake_file:
# return_value = _fake_file.read()
#
# # Clean up temporary files
# os.remove(filename)
# os.remove(filename + ".mp3")
#
# return return_value

0 comments on commit 9eeed51

Please sign in to comment.