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
#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


Messages In This Thread
RE: How to Split Output Audio on Text to Speech Code - by Base12 - Aug-29-2020, 03:23 AM

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