Python Forum
azure TTS from text files to mp3s
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
azure TTS from text files to mp3s
#1
Experiencing a problem with the sound files generated by the program, and that they are coming out as corrupted or not working properly.

I am having an issue with a program I created using the python programming language. The program converts text files located in a specific folder into speech audio files using the Azure Cognitive Services Text to Speech API and saves the generated audio files in a different folder. However, the audio files are coming out as corrupted and not working properly. I have not been able to find a solution and would greatly appreciate any suggestions or help.

Thank you in advance.

import os
import requests
from array import array

# Global constants
API_KEY = ""
ENDPOINT_URL = ""
TEXT_FOLDER = "C:/Users/user/Desktop/text"
AUDIO_FOLDER = "C:/Users/user/Desktop/audio"
VOICE_OPTION = "en-US-JessaNeural"

def file_list_to_array(folder, extension):
    """
    Returns an array of file names that match the specified extension in the given folder.
    """
    files = []
    for file in os.listdir(folder):
        if file.endswith(extension):
            files.append(file)
    return files

def text_to_speech(text_file, audio_file):
    """
    Converts the text in the given text file to speech and saves the generated audio as the given audio file.
    """
    headers = {
        "Ocp-Apim-Subscription-Key": API_KEY,
        "Content-Type": "application/ssml+xml",
        "X-Microsoft-OutputFormat": "audio-24khz-48kbitrate-mono-WAV",
        "User-Agent": "Edge"
    }
    with open(text_file, "r") as f:
        text = f.read()
    body = f"<speak version='1.0' xmlns='https://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='{VOICE_OPTION}'>{text}</voice></speak>"
    response = requests.post(ENDPOINT_URL, headers=headers, data=body)
    if response.status_code != 200:
        print(f"Error: {response.content}")
        return
    with open(audio_file, "wb") as f:
        f.write(response.content)
    print(f"Text in {text_file} converted to speech and saved as {audio_file}")

def main():
    text_files = file_list_to_array(TEXT_FOLDER, ".txt")
    for text_file in text_files:
        audio_file = text_file.replace(".txt", ".WAV")
        audio_file = os.path.join(AUDIO_FOLDER, audio_file)
        text_file = os.path.join(TEXT_FOLDER, text_file)
        text_to_speech(text_file, audio_file)

if __name__ == "__main__":
    main()

Hello everyone,
I posted a thread on this forum seeking assistance with a Python program script that I am trying to fix. Unfortunately, I have not received any responses yet. I understand that everyone is busy and may not have the time to answer right away, but if anyone could take a look at my post and provide some help or guidance, it would be greatly appreciated.
Thank you
Reply
#2
Have you looked at response content print(response) to see if it's really is a binary format you get back?
Here a example found online you can try.
import requests
import json

# Replace with your Text to Speech subscription key
subscription_key = "YOUR_SUBSCRIPTION_KEY"

# Replace with the voice you want to use
voice = "en-US-Jessa24kRUS"

# Replace with the text you want to convert to speech
text = "Hello, this is an example of the Text to Speech API."

# Specify the language and format of the audio file
headers = {
    "Ocp-Apim-Subscription-Key": subscription_key,
    "Content-Type": "application/ssml+xml",
    "X-Microsoft-OutputFormat": "riff-24khz-16bit-mono-pcm",
    "User-Agent": "YOUR_RESOURCE_NAME"
}

# Create the SSML request
body = "<speak version='1.0' xml:lang='en-us'><voice xml:lang='en-us' xml:gender='Female' name='" + \
    voice + "'>" + text + "</voice></speak>"

# Make the request to the Text to Speech API
response = requests.post("https://YOUR_REGION.tts.speech.microsoft.com/cognitiveservices/v1", headers=headers, data=body)

# Save the generated audio file
with open("output.wav", "wb") as audio_file:
    audio_file.write(response.content)
print("The file has been saved to output.wav.")
Reply
#3
it creates the file (.wav) but the file is corrupted
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upload Files to Azure Storage Container phillyfa 6 699 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  Writing into 2 text files from the same function paul18fr 4 1,679 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,568 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,174 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,924 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  Help Needed playing MP3s bill_z 2 1,026 Apr-13-2022, 01:44 PM
Last Post: bill_z
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,517 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Separate text files and convert into csv marfer 6 2,880 Dec-10-2021, 12:09 PM
Last Post: marfer
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,892 Aug-20-2021, 05:42 PM
Last Post: snippsat
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,142 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020