Python Forum
How to generate more MP3 files at the same time in Amazon Polly using Python code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to generate more MP3 files at the same time in Amazon Polly using Python code?
#1
Hi!

The programming language of Amazon Polly (TTS) is Python. However, I'm not very familiar with it yet.

I have a list with words and want Amazon Polly to pronounce them and save every word as a new mp3 file. (name could be the word's name)

My list is like:

"
apple
banana
peach
orange
...
"

On the following website there is a code provided by Amazon which result is more mp3 file so seems promising: (I inserted it at the end of my post)
https://aws.amazon.com/blogs/machine-lea...on-script/

My question is, how can I modify that code, so I can use it to generate the pronouncation for each word as a new mp3 file?

Thank you for your help! It means to me a lot!

Have a very nice day!

____

# coding: utf-8
import subprocess
import codecs

f = codecs.open("story.txt", encoding='utf-8')

cnt = 0
file_names = ''

for line in f:
    rendered = ''
    line = line.replace('"', '\\"')
    command = 'aws polly synthesize-speech --text-type ssml --output-format "mp3" --voice-id "Salli" --text "{0}" {1}'

    if '\r\n' == line:
        #A pause after a paragraph
        rendered = '<speak><break time= "2s"/></speak>'
    else:
        #A pause after a sentence
        rendered = '<speak><amazon:effect name=\\"drc\\">' + line.strip() + '<break time=\\"1s\\"/></amazon:effect></speak>'
    
    file_name = ' polly_out{0}.mp3'.format(u''.join(str(cnt)).encode('utf-8'))
    cnt += 1
    command = command.format(rendered.encode('utf-8'), file_name)
    file_names += file_name
    print command
    subprocess.call(command, shell=True)

print file_names
execute_command = 'cat ' + file_names + '>result.mp3'
subprocess.call(execute_command, shell=True)

execute_command = 'rm ' + file_names
print 'Removing temporary files: ' + execute_command
subprocess.call(execute_command, shell=True)
Reply


Messages In This Thread
How to generate more MP3 files at the same time in Amazon Polly using Python code? - by makiwara - Jun-30-2018, 08:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 6 159 Yesterday, 05:39 PM
Last Post: tester_V
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 783 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,076 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,635 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Generate Python variables dynamically Pedroski55 5 2,916 Mar-04-2022, 10:13 PM
Last Post: deanhystad
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,430 Feb-13-2022, 07:13 PM
Last Post: menator01
  Delete files from amazon s3 bucket python_student 0 3,947 Jan-14-2022, 04:51 PM
Last Post: python_student
  Generate Multiple sql Files With csv inputs vkomarag 13 4,266 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,851 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,298 Feb-24-2021, 10:43 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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