Create multiple audio files from one request to Amazon Polly

37 Views Asked by At

Let's say that I have a text file containing one word per line, and I need to create for each word an audio file using Amazon Polly. It's a really big file (containing ~ 1 million characters and around 98k words) and I would suggest that it might not be a good idea to send one request per word. Is it possible to send one request containing the list of words that I need to convert to speech and still be able to create multiple audios each containing one word (and not one audio with all the words) ? I've heard about batch processing but I'm not sure if I can use it for my case.

Thank you.

For now, let's say that this is my code:

import boto3
import os

polly_client = boto3.Session(
                aws_access_key_id='YOUR_ACCESS_KEY',
                aws_secret_access_key='YOUR_SECRET_KEY',
                region_name='us-east-1').client('polly')

with open('words.txt', 'r') as file:
    lines = file.readlines()

for i, line in enumerate(lines):
    text = line.strip()

    response = polly_client.synthesize_speech(VoiceId='Joanna',
                OutputFormat='pcm', 
                Text = text,
                Engine='neural')

    with open(f'word{i+1}.wav', 'wb') as file:
        file.write(response['AudioStream'].read())

0

There are 0 best solutions below