将音频文件保存到所需路径

问题描述 投票:0回答:4

我有很多需要保存的文本转语音音频文件,但文件经常丢失。 目前我正在使用

    import gtts
    from playsound import playsound
   
    def say(speech):
        tts = gtts.gTTS(speech)
        tts.save("audio.mp3")
        playsound("audio.mp3")

有什么方法可以将 mp3 保存到我想要的任何地方吗?

python file audio path mp3
4个回答
1
投票

您可以将新音频文件的内容粘贴到另一个文件中,如下所示:

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

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

0
投票

将 tts 保存为 mp3 文件时,只需将需要的目录放在括号中即可:

tts.save("directory/that/you/want/audio.mp3")


0
投票

只需自己或使用您的应用程序创建文件夹并写入:

from gtts import gTTS

tts.save("c:/folder/audioname.mp3")# use the "/" while setting the path

这只是一个例子。你可以换磁盘,但必须用小写字母提及(d:, f:, c:)


-1
投票

我确实喜欢这样:s.save(“你的文件夹名称/audio.mp3”)

© www.soinside.com 2019 - 2024. All rights reserved.