Python + edge-tts: one line of code to easily turn your text into speech

Hello everyone, I am Mr. Tree! Today I will introduce a Python library edge-tts, which can easily convert text to speech locally. It is very convenient and completely free!

Let's listen to the effect first: self-introduction

How about it? Is it familiar? The beginning is often used in film and television commentary: This woman is called Xiaomei. . .

edge-tts introduction

edge-tts is a Python library that inherits the text-to-speech (TTS) function of Microsoft Azure and is free to use. This library provides a simple API to convert text to speech, and supports multiple languages ​​and voices.

insert image description here

Convert text to speech with just one line of code!

edge-tts --text "Hello, world!" --write-media hello.mp3

detailed steps

pip install edge-tts
  • After the installation is complete, enter the edge-tts command in the cmd window, and the following prompt information will be output, indicating that the installation has been completed.

insert image description here

  • Try it out and generate the first TTS voice. By default, an audio file of hello.mp3 will be generated locally.
edge-tts --text "Hello, world!" --write-media hello.mp3
  • The default voice model is used here, you can also use the --list-voices option to view the list of available voices, and then use the --voice option to select the voice you want.
edge-tts --list-voices
...

Name: zh-CN-XiaoxiaoNeural
Gender: Female

Name: zh-CN-XiaoyiNeural
Gender: Female

Name: zh-CN-YunjianNeural
Gender: Male

Name: zh-CN-YunxiNeural
Gender: Male

Name: zh-CN-YunxiaNeural
Gender: Male

Name: zh-CN-YunyangNeural
Gender: Male

Name: zh-CN-liaoning-XiaobeiNeural
Gender: Female

Name: zh-CN-shaanxi-XiaoniNeural
Gender: Female

...

edge-tts --voice zh-CN-YunxiNeural --text "你好,我是程序员树先生" --write-media hello_tree.mp3
  • You can also adjust the speech rate and volume through the --rate and --volume options, -50% means to reduce the speech rate/volume.
edge-tts --rate=-50% --text "Hello, world!" --write-media hello_with_rate_halved.mp3
edge-tts --volume=-50% --text "Hello, world!" --write-media hello_with_volume_halved.mp3
  • The above are the instructions executed on the cmd command line. We can also use python to read the content of the file and convert it to voice, and directly present the code.
import edge_tts
import asyncio

TEXT = ""
with open('C:\\Users\\Tree\\Desktop\\text2voicetest.txt', 'rb') as f:
    data = f.read()
    TEXT = data.decode('utf-8')
print(TEXT)
voice = 'zh-CN-YunxiNeural'
output = 'C:\\Users\\Tree\\Desktop\\text2voicetest.mp3'
rate = '-4%'
volume = '+0%'


async def my_function():
    tts = edge_tts.Communicate(text=TEXT, voice=voice, rate=rate, volume=volume)
    await tts.save(output)


if __name__ == '__main__':
    asyncio.run(my_function())

Create a text2voicetest.txt file on the desktop, write the content you want to write, and run the above code directly to generate the corresponding MP3 file, done!

scenes to be used

  • You can use it to add audio reading to your blog posts or e-books, so that your readers can learn as they listen.
  • You can use it to add voice dubbing to your games or animations, making your characters more individual and expressive.
  • You can use it to add voice interaction to your chatGPT chatbot or smart assistant, making your users more convenient and comfortable.
  • You can use it to learn the pronunciation of different languages, or to hear the style and emotion of different voices.
  • You can use it to use text-to-speech services anywhere, whether it's in the cloud, on-premises, or at the edge.

project address

https://github.com/rany2/edge-tts

Guess you like

Origin blog.csdn.net/sj5590155/article/details/129999729