Last active
October 10, 2021 11:06
-
-
Save ns-mkusper/4a575f98d1b159ca19cd2ef239f2806f to your computer and use it in GitHub Desktop.
txt 2 mp3 human voice amazon polly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert txt to polly-audio ogg file | |
# coding: utf-8 | |
import subprocess | |
import codecs | |
import sys | |
f = codecs.open(sys.argv[1], 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 "Matthew" --text "{0}" {1}' | |
if '\r\n' == line: | |
# Don't do anything on blank lines | |
rendered = '<speak><break time= ".4s"/></speak>' | |
elif len(line) > 3: | |
# A pause after a sentence | |
rendered = '<speak><amazon:effect name=\\"drc\\">' + line.strip( | |
) + '<break time=\\".4s\\"/></amazon:effect></speak>' | |
else: | |
continue | |
file_name = ' polly_out{0}.mp3'.format( | |
u''.join("_" + str("%06d.txt" % 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(f'Removing temporary files: {execute_command}') | |
subprocess.call(execute_command, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment