Python の pyttsx3 ライブラリを使用してテキストを音声に変換する


序文

皆さんこんにちは、Kongkong スターです. この記事では、Python の pyttsx3 ライブラリを使用してテキストを音声に変換する方法を紹介します。


1. pyttsx3 とは?

pyttsx3 は、テキストを自然な人間の音声に変換するオープン ソースの Python テキスト読み上げライブラリです。豊富で柔軟な構成オプションを提供し、音声、発話速度、イントネーションなどをカスタマイズでき、多言語変換をサポートします。さらに、メインプログラムをブロックすることなく、音声合成エンジンをバックグラウンドで自動的に呼び出すことができる非同期操作もサポートしています。Pyttsx3 は、自動音声プロンプト、インテリジェント音声アシスタント、音声検証など、さまざまな分野で広く使用できます。非常に優れた Python 音声処理ライブラリです。

2.pyttsx3 をインストールする

pip install pyttsx3

3. pyttsx3 のバージョンを確認する

 pip show pyttsx3

名前: pyttsx3
バージョン: 2.90
概要: Python 2 および 3 用の Text to Speech (TTS) ライブラリ。インターネット接続や遅延なしで動作します。Sapi5、nsss、espeak など、複数の TTS エンジンをサポートします。
ホームページ: https://github.com/nateshmbhat/pyttsx3

第四に、pyttsx3 の使用

1. ライブラリのインポート

import pyttsx3

2. 変換が必要なテキストを定義する

text = '大家好,我是空空star,本篇给大家分享一下文字转音频,这是通过pyttsx3转换的音频。'

3. pyttsx3 エンジンを初期化します

engine = pyttsx3.init()

4. サウンドを設定する

# 获取所有可用的声音列表
voices = engine.getProperty('voices')
# 选择一个指定语音(粤语语音sinji)
engine.setProperty('voice', voices[36].id)

ここでは、Mac システムで広東語の音声
com.apple.speech.synthesis.voice.sinjiを使用しています。

5. 読み上げ速度を設定する

engine.setProperty('rate', 150)

6.音量を設定する

engine.setProperty('volume', 0.8)

7.ボイスを再生

engine.say(text)

8.音声を保存する

local = '/Users/kkstar/Downloads/video/'
engine.save_to_file(text, local+"audio_pyttsx3.mp3")

9. 言語変換が完了するまで待ちます

engine.runAndWait()

engine.runAndWait() は、テキスト読み上げ中にプログラムの実行を続行する前に、テキスト変換が完了するのを待機するために使用されます。その目的は、テキストを音声に変換するときにプログラムが途中で終了して、テキストを完全に変換できない状況を回避することです。

5.ボイスエフェクト

mp3 ファイルはブログに挿入できないため、まず mp4 に変換し、変換効果を mp4 の音で聞くことができます。

pyttsx3-text-to-audio エフェクトのデモ


要約する


com.apple.speech.synthesis.voice.Alex
com.apple.speech.synthesis.voice.alice.premium
com.apple.speech.synthesis.voice.alva
com.apple.speech _ .synthesis.voice.amelie
com.apple.speech.synthesis.voice.anna
com.apple.speech.synthesis.voice.carmit
com.apple.speech.synthesis.voice.damayanti
com.apple.speech.synthesis.voice.daniel
com.apple.speech.synthesis.voice.diego
com.apple.speech.synthesis.voice.ellen
com.apple.speech.synthesis.voice.fiona
com.apple.speech.synthesis.voice.Fred
com.apple.speech. synthesis.voice.ioana
com.apple.speech.synthesis.voice.joana
com.apple.speech.synthesis.voice.jorge
com.apple.speech.synthesis.voice.juan
com.apple.speech.synthesis.voice.kanya
com.apple.speech.synthesis.voice.karen
com.apple.speech.synthesis.voice.kyoko
com.apple.speech. synthesis.voice.laura
com.apple.speech.synthesis.voice.lekha
com.apple.speech.synthesis.voice.luca
com.apple.speech.synthesis.voice.luciana
com.apple.speech.synthesis.voice.maged
com .apple.speech.synthesis.voice.mariska
com.apple.speech.synthesis.voice.meijia
com.apple.speech.synthesis.voice.melina
com.apple.speech.synthesis.voice.milena
com.apple.speech.synthesis .voice.moira
com.apple.speech.synthesis.voice.monica
com.apple.speech.synthesis.voice.nora
com.apple.speech.synthesis.voice.paulina
com.apple.speech.synthesis.voice.rishi
com.apple.speech.synthesis.voice.samantha
com.apple.speech.synthesis.voice.sara
com.apple.speech. synthesis.voice.satu
com.apple.speech.synthesis.voice.sinji
com.apple.speech.synthesis.voice.tessa
com.apple.speech.synthesis.voice.thomas
com.apple.speech.synthesis.voice.tingting.プレミアム
com.apple.speech.synthesis.voice.veena
com.apple.speech.synthesis.voice.Victoria
com.apple.speech.synthesis.voice.xander
com.apple.speech.synthesis.voice.yelda
com.apple.speech .synthesis.voice.yuna
com.apple.speech.synthesis.voice.yuri
com.apple.speech.synthesis.voice.zosia
com.apple.speech.synthesis.voice.zuzana

おすすめ

転載: blog.csdn.net/weixin_38093452/article/details/130148334