Amazon Polly 文本轉語音 (python實現)
網頁版的長這個樣子,需要自己手動輸入,比較麻煩,我們希望通過python和一些文件自動合成語音
首先需要去官網註冊賬戶,這裏有一年的免費體驗,註冊賬戶需要信用卡綁定!
註冊完賬號後,需要配置AWS CLI , 它是一個命令行的接口,具體參考AWS CLI
使用 Pip 安裝 AWS CLI
使用 pip
安裝 AWS CLI。
$ pip install awscli --upgrade --user
驗證 AWS CLI 是否已正確安裝。
$ aws --version
aws-cli/1.11.84 Python/3.6.2 Linux/4.4.0-59-generic botocore/1.5.47
如果出現錯誤,請參閱糾正 AWS CLI 錯誤。
要升級到最新版本,請重新運行安裝命令:
$ pip install awscli --upgrade --user
配置 AWS CLI
本節介紹如何配置 AWS Command Line Interface在與 AWS 交互時使用的設置,如您的安全證書和默認區域。
注意
AWS CLI 代表您對請求進行簽名,並在簽名中包含日期。請確保您的計算機的日期和時間設置正確;否則,簽名中的日期可能與請求的日期不匹配,進而導致 AWS 拒絕請求。
快速配置
對於一般用途,aws configure
命令是設置 AWS CLI 安裝的最快方法。
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
AWS CLI 將提示四種信息。AWS 訪問密鑰 ID 和 AWS 私有訪問密鑰是您的賬戶證書。
在官網中有其他配置,這裏我這是快速配置一下!
上面配置,需要Access Key 和 Secret Access Key ,官網上說創建個IAM ,可以不用創建
官網提示:
建議您使用 AWS 賬戶根用戶 訪問祕鑰而不是使用 IAM 賬戶根用戶訪問祕鑰。IAM 讓您可以安全地控制對您的 AWS 賬戶中 AWS 服務和資源的訪問。
僅當創建訪問密鑰時,您才能查看或下載祕密訪問密鑰。以後您無法恢復它們。不過,您隨時可以創建新的訪問密鑰。您還必須擁有執行所需 IAM 操作的權限。有關更多信息,請參閱 IAM 用戶指南 中的訪問 IAM 資源所需的權限。
申請密鑰步驟:
1. 選擇安全證書選項
2. 這裏可能會提示,continue 繼續就行
3. 選擇Access Key 選項 點擊 Create New Access Key
4.要查看新訪問祕鑰,請選擇顯示。您的憑證與下面類似
1.訪問密鑰 ID:AKIAIOSFODNN7EXAMPLE
2.私有訪問密鑰:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
5. 要下載密鑰對文件,請選擇下載 .csv 文件。將密文件保存到本地。
密鑰申請完成,下面去服務器進行配置!
打開終端 輸入: $ aws configure
前兩行不多說,剛申請了填上就行,第三行表示選擇區域填us-east-1, 輸出格式填json,填完後配置就大功告成了!
下面python代碼:
#!/usr/bin/env python
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import argparse
import os
import sys
import subprocess
from tempfile import gettempdir
outfile = "cc.mp3"
# Create a client using the credentials and region defined in the [adminuser]
# section of the AWS credentials file (~/.aws/credentials).
session = Session() #profile_name="adminuser")
polly = session.client("polly",region_name="us-east-1")
voice = "Geraint"
#voice = "Joanna"
#infile = args.infile
#index = 1
#pieces = []
#with open(infile, "rb") as f:
# pieces = [l for l in (line.strip() for line in f) if l]
piece = "<speak>Community service is an important component of education here at our university</speak>"
def MainFunction():
with open(outfile, "wb") as out:
try:
# Request speech synthesis
response = polly.synthesize_speech(Text=piece, TextType="ssml", OutputFormat="mp3",
VoiceId=voice)
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 as 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:
try:
# Open a file for writing the output as a binary stream
out.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)
return True
if __name__ == "__main__":
MainFunction()
可以通過腳本自動將語音寫到本地!
詳細教程: