[Python] Text to Speech a conversation using gtts in python

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make a dialog between two people in different dialects using gtts. My logic is very simple, the dialog is in a list, if it starts with person 1, then it speaks that and puts it in mp3_files as an mp3. Then I would combine it all to make a single mp3. I keep getting an error and I have no idea how to move on.

    import os
    from gtts import gTTS
    from moviepy.editor import *
    import moviepy.editor as mp
    from pydub import AudioSegment

    # Dialog between two people
    dialog = "Person 1: Hi, how are you? \nPerson 2: I'm good, thanks for asking. How about you? \nPerson 1: I'm great, thanks!"

    # Split the dialog into separate lines
    lines = dialog.split("\n")

    # Create an empty list to store the mp3 files
    mp3_files = []

    # Convert each line of dialog to speech and save as mp3
    for line in lines:
    if "Person 1" in line:
    # Use a male voice for Person 1
    tts = gTTS(text=line, lang='en', slow=False)
    tts.save("temp1.mp3")
    mp3_files.append("temp1.mp3")
    elif "Person 2" in line:
    # Use a french voice for Person 2
    tts = gTTS(text=line, lang='fr', slow=False)
    tts.save("temp2.mp3")
    mp3_files.append("temp2.mp3")

    # Create an empty audio segment
    combined = AudioSegment.empty()

    # Iterate through the list of mp3 files and concatenate them
    for file in mp3_files:
    combined += AudioSegment.from_file(file, format="mp3")

    # Export the combined audio segment to a new mp3 file
    combined.export("dialog.mp3", format="mp3")

    # remove the temp files
    for file in mp3_files:
    os.remove(file)


    Error:

    PS C:\Users\BattleShip\Desktop\work> & C:/Users/BattleShip/AppData/Local/Microsoft/WindowsApps/python3.9.exe c:/Users/BattleShip/Desktop/work/Testing1.py
    Traceback (most recent call last):
    File "c:\Users\BattleShip\Desktop\work\Testing1.py", line 33, in <module>
    combined += AudioSegment.from_file(file, format="mp3")
    File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py", line 728, in from_file
    info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
    File "C:\Users\BattleShip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\utils.py", line 274, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
    FileNotFoundError: [WinError 2] The system cannot find the file specified

    Continue reading...

Compartilhe esta Página