Python Forum
How to Split Output Audio on Text to Speech Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to Split Output Audio on Text to Speech Code (/thread-29289.html)



How to Split Output Audio on Text to Speech Code - Base12 - Aug-26-2020

Hello, this is my first post here and I have extremely little Python experience.

My goal is to use IBM Watson Text to Speech to read a .TXT file and output it to multiple .MP3 files.

I have been able to successfully do some of this using Jupyter Notebook and some code found in this video.

@3:15 you can see the code similar to what I'm using...



My question is…

Is it possible to split the audio into separate .MP3 files every time the .TXT file has an [enter] in it?

Example:

This is sentence number one. This is sentence number two.
[enter]
This is sentence number three. This is sentence number four.
[enter]

In the above scenario, sentence one and two would output as 001.mp3. Sentence three and four would output as 002.mp3. And so on.

Note that the separator doesn't have to be an [enter]. It can be a series of asterisks or whatever.

It looks like Jupyter Notebook uses "\n\n" as [enter]. Maybe that's where the split can happen.

I’m hoping someone could expand on the code I have…

Here is the code that reads the .TXT file...

with open("test.txt") as text_file:
    text = text_file.read()
    text_file.close()
Here is the code that outputs to audio MP3...

with open('./speech.mp3', 'wb') as audio_file:
    res = tts.synthesize(text, accept='audio/mp3', voice='en-US_MichaelV3Voice').get_result()
    audio_file.write(res.content)
I can provide more code if necessary.

Thank you!


RE: How to Split Output Audio on Text to Speech Code - Base12 - Aug-28-2020

I broke down the problem into something simpler.

I used a free 'Text Splitter' program called GSPLIT to break the .TXT file into separate files...

001.TXT contains: This is sentence number one. This is sentence number two.

002.TXT contains: This is sentence number three. This is sentence number four.

All I need now is some code that 'loops' through each .TXT file and outputs the audio.

Any advice would be appreciated!


RE: How to Split Output Audio on Text to Speech Code - Base12 - Aug-29-2020

OK, I managed to cobble together some code after watching a few more tutorials...

try:
    for FileNumber in range (1,1000):
        with open((str(FileNumber).zfill(3)) + ".txt") as text_file:
            text = text_file.read()
            FileName = ((str(FileNumber).zfill(3)) + ".txt")
            print (FileName)
            print (text)
            with open('./' + FileName + '.mp3', 'wb') as audio_file:
                res = tts.synthesize(text, accept='audio/mp3', voice='en-US_MichaelV3Voice').get_result()
                audio_file.write(res.content)
except FileNotFoundError:
    print("No more files to convert!")
The code will now...
  • Begin with a maximum range of 999 files to look for
  • Loop through each xxx.TXT file it finds (i.e. 001.TXT, 002.TXT, 003.TXT, etc.)
  • Print the File Name
  • Print the text inside the file
  • Convert the text to speech and record an .MP3 file
  • Stop when there are no more files left to convert (FileNotFoundError)
  • Print "No more files to convert!" after it reaches the last file and causes an error
I used 'zfill(x)' to add leading zeros to my file names.

Hope I did a good job... lol. Dance