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
  problem in output of a snippet code akbarza 2 302 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  [split] Why is there an output of None akbarza 1 416 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 579 Sep-07-2023, 05:09 PM
Last Post: deanhystad
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 1,292 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  I cannot able to see output of this code ted 1 716 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 711 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,084 Jun-23-2022, 09:18 PM
Last Post: William369
  Controlling text-to-speech pauses with pyttsx3 pmac1300 4 4,325 Mar-14-2022, 06:47 AM
Last Post: Coricoco_fr
  How can I organize my code according to output that I want ilknurg 1 1,141 Mar-11-2022, 09:24 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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