-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice_generator.py
56 lines (46 loc) · 2 KB
/
voice_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from boto3 import Session
from contextlib import closing
from botocore.exceptions import BotoCoreError, ClientError
import logging, os
default_profile_name = "content-management"
default_region_name = "us-east-1"
# default_local_file_storage = '/storage'
default_local_file_storage = os.getenv('VIDEO_GENERATION_STORAGE_PATH', '/root/moviepyvideo/storage')
class VoiceGenerator:
def __init__(self,
profile_name=default_profile_name,
region_name=default_region_name,
local_storage=default_local_file_storage,
):
session = Session(profile_name=profile_name, region_name=region_name)
self.polly = session.client('polly')
self.s3_client = session.client('s3')
self.local_storage = local_storage
self.voice_file_storage = '{}/audio/voice'.format(self.local_storage)
# initialize the voice file storage if not exists
os.makedirs(self.voice_file_storage, exist_ok=True)
def generate_text_audio(self, text, speech, output_file_name):
processing_text = '''<speak><prosody rate="80%">{}</prosody></speak>'''.format(text)
try:
response = self.polly.synthesize_speech(
Text=processing_text,
OutputFormat="mp3",
VoiceId=speech['voice'],
TextType="ssml",
Engine="neural",
LanguageCode=speech['language'],
)
except (BotoCoreError, ClientError) as error:
logging.error(error)
raise error
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = output_file_name
try:
# Open a file for writing the output as a binary stream
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
logging.error(error)
raise error
return