0
import os
import subprocess
from gtts import gTTS


class Record:
    def __init__(self, file, path):
        self.file_name = f'{file.split(".")[0]}.mp3'
        self.file_path = os.path.join(path, file)
        self.out_path = os.path.join(path, self.file_name)

    def create_recordings(self):
        subprocess.call(['gtts-cli', '-f' + self.file_path, '-o' + self.out_path])


path = r'C:\Users\xxxx\Desktop\create_recordings'

for file in os.listdir(path):
    if file.endswith('.txt'):
        Record(file, path).create_recordings()

我想做什么:

更改 subprocess.call 命令中的语言 我尝试在 self.file_path 之后添加 'lang=' + 'en-uk',但它返回错误:

Usage: gtts-cli [OPTIONS] <text>

Error: Invalid value for "<text>": <text> and -f/--file <file> can't be used together 

所以我不太确定如何更改它以添加不同的语言

id 真正喜欢的是如果我可以将语言传递给 Record 类并在 subprocess.call 命令中使用它

有很多语言可以改成,但这里是所有在命令行中使用 gtts-cli --all 显示的英文:

  en-au: English (Australia)
  en-ca: English (Canada)
  en-gb: English (UK)
  en-gh: English (Ghana)
  en-ie: English (Ireland)
  en-in: English (India)
  en-ng: English (Nigeria)
  en-nz: English (New Zealand)
  en-ph: English (Philippines)
  en-tz: English (Tanzania)
  en-uk: English (UK)
  en-us: English (US)
  en-za: English (South Africa)
4

0 回答 0