Python Forum
How to Split Output Audio on Text to Speech Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Split Output Audio on Text to Speech Code
#1
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!
Reply
#2
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!
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Change Text Size in Console? CodingKid 1 378 Jul-20-2021, 02:47 PM
Last Post: deanhystad
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 2,193 Apr-13-2021, 01:26 PM
Last Post: amirian
  How do I split the output? AnunnakiKungFu 2 593 Feb-25-2021, 11:13 PM
Last Post: AnunnakiKungFu
  Getting a GET request output text into a variable to work with it. LeoT 2 614 Feb-24-2021, 02:05 PM
Last Post: LeoT
  Increment text files output and limit contains Kaminsky 1 903 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  [split] How to convert the CSV text file into a txt file Pinto94 5 1,056 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Split gps files based on time (text splitting) dervast 0 552 Nov-09-2020, 09:19 AM
Last Post: dervast
  Handling multi-input/output audio in python bor1904 4 1,179 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Why this code not getting desired output ? MDRI 2 1,209 Sep-18-2020, 02:11 AM
Last Post: MDRI
  [split] Python code help sairam17519 3 949 Sep-10-2020, 01:44 PM
Last Post: sairam17519

Forum Jump:

User Panel Messages

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