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
#2
Please, tell us what is happening? Output and errors of your script.

Your mp3 file name is resulting in polly_out'X'.mp3, this is not good.
Change it to something like:
file_name = ' polly_out_{0}.mp3'.format(cnt)

I think that what you want is exactly those polly_out_X.mp3 files, right?
But at the end of your script you're removing them from your system and staying only with the result.mp3.
Reply
#3
Hi Gontajones, thank you for your answer and sorry for the late reply!

I was reading about the topic, hoping that I can ask better questions.

As far as I know, the last part of the code unites the generated mp3 files into 1 big mp3 file. (the amazon article which I have linked says so at least) So that part could be deleted (?), since I need a new mp3 file for each line in my txt file.

So my txt file is something like:

orange and fruit
vegetables
citrone
peach
...

And the output's name could be the number of the row, containing the following content:
So 1.mp3 = "orange and fruit"
2.mp3 ="vegetables"
3.mp3 = "citrone"
4.mp3 = "peach"

And so on.

I really appreciate your help, since this whole Python language is new to me.

It means to me a lot!

Have a nice day!





(Jun-30-2018, 12:30 PM)gontajones Wrote: Please, tell us what is happening? Output and errors of your script.

Your mp3 file name is resulting in polly_out'X'.mp3, this is not good.
Change it to something like:
file_name = ' polly_out_{0}.mp3'.format(cnt)

I think that what you want is exactly those polly_out_X.mp3 files, right?
But at the end of your script you're removing them from your system and staying only with the result.mp3.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 708 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 960 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,527 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Generate Python variables dynamically Pedroski55 5 2,802 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,353 Feb-13-2022, 07:13 PM
Last Post: menator01
  Delete files from amazon s3 bucket python_student 0 3,869 Jan-14-2022, 04:51 PM
Last Post: python_student
  Generate Multiple sql Files With csv inputs vkomarag 13 4,110 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,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,165 Feb-24-2021, 10:43 PM
Last Post: nilamo
  python tool to collect the time measurement of entire code maiya 3 2,237 Feb-12-2021, 05:39 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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