summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--aws-polly.py55
-rw-r--r--speech.mp3bin0 -> 13054 bytes
2 files changed, 55 insertions, 0 deletions
diff --git a/aws-polly.py b/aws-polly.py
new file mode 100644
index 0000000..151f2f4
--- /dev/null
+++ b/aws-polly.py
@@ -0,0 +1,55 @@
+# https://docs.aws.amazon.com/polly/latest/dg/get-started-what-next.html
+
+"""Getting Started Example for Python 2.7+/3.3+"""
+from boto3 import Session
+from botocore.exceptions import BotoCoreError, ClientError
+from contextlib import closing
+import os
+import sys
+import subprocess
+from tempfile import gettempdir
+
+# Create a client using the credentials and region defined in the [polly-windows]
+# section of the AWS credentials file (~/.aws/credentials).
+session = Session(profile_name="polly-windows")
+polly = session.client("polly")
+
+try:
+ # Request speech synthesis
+ response = polly.synthesize_speech(Text="hello sexy, you're my master!", OutputFormat="mp3",
+ VoiceId="Justin")
+except (BotoCoreError, ClientError) as error:
+ # The service returned an error, exit gracefully
+ print(error)
+ sys.exit(-1)
+
+# Access the audio stream from the response
+if "AudioStream" in response:
+ # Note: Closing the stream is important because the service throttles on the
+ # number of parallel connections. Here we are using contextlib.closing to
+ # ensure the close method of the stream object will be called automatically
+ # at the end of the with statement's scope.
+ with closing(response["AudioStream"]) as stream:
+ output = os.path.join(gettempdir(), "speech.mp3")
+
+ 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:
+ # Could not write to file, exit gracefully
+ print(error)
+ sys.exit(-1)
+
+else:
+ # The response didn't contain audio data, exit gracefully
+ print("Could not stream audio")
+ sys.exit(-1)
+
+# Play the audio using the platform's default player
+if sys.platform == "win32":
+ os.startfile(output)
+else:
+ # The following works on macOS and Linux. (Darwin = mac, xdg-open = linux).
+ opener = "open" if sys.platform == "darwin" else "xdg-open"
+ subprocess.call([opener, output]) \ No newline at end of file
diff --git a/speech.mp3 b/speech.mp3
new file mode 100644
index 0000000..8de30e1
--- /dev/null
+++ b/speech.mp3
Binary files differ