Ausgabe
Ich habe viele Text-zu-Sprache-Audiodateien, die ich speichern muss, aber oft gehen die Dateien verloren. Derzeit benutze ich
import gtts
from playsound import playsound
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
Gibt es eine Möglichkeit, die mp3 an einem beliebigen Ort zu speichern?
Lösung
Sie können den Inhalt einer neuen Audiodatei einfach in eine andere Datei wie unten einfügen:
import gtts
from playsound import playsound
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
# PLACE CONTENT INTO NEW FILE => S T A R T
main_file = open("audio.mp3", "rb").read()
dest_file = open('path/to_your/file_name.mp3', 'wb+')
dest_file.write(main_file)
dest_file.close()
# PLACE CONTENT INTO NEW FILE => E N D
oder
import gtts
from playsound import playsound
import shutil
def say(speech):
tts = gtts.gTTS(speech)
tts.save("audio.mp3")
playsound("audio.mp3")
# PLACE CONTENT INTO NEW FILE => S T A R T
shutil.move("audio.mp3", "path/to_your/file_name.mp3")
# PLACE CONTENT INTO NEW FILE => E N D
Beantwortet von – ilyas Jumadurdyew
Antwort geprüft von – Gilberto Lyons (FixError Admin)