Convert text to audio through Python's gtts library


foreword

Hello everyone, I am Kongkong star. In this article, I will share with you how to convert text to audio using python.


1. Background

Text-to-audio can help the visually impaired obtain information by listening to sounds; it can also help people listen to some long articles or learning materials conveniently, saving reading time and fatigue. At the same time, for some language learners, text-to-audio can also help them better learn the pronunciation and intonation and improve their language expression ability.

2. Explanation of TTS terms

TTS, short for Text To Speech, is a technology that converts text into speech. It can convert text into artificial voice, enabling computer systems to communicate with users through voice interaction.

3. GTTS Glossary

GTTS is the abbreviation of Google Text to Speech, which is a technology for converting text to speech. It can help users quickly generate speech, and through simple API calls, users can easily convert specified text into various audio files with customizable speech. The advantage of GTTS is that it has high voice quality and fast voice conversion, while being easy to use, making it the first choice for developers and ordinary users. In addition, GTTS also supports multiple languages ​​and audio formats. However, GTTS also has some disadvantages, such as the inability to achieve continuous audio synthesis and the voice-converted audio may not perfectly match the user's expectations.

4. Implementation method

gTTS

1. Import library

from gtts import gTTS

2. Define the text that needs to be converted

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

3. Set Chinese pronunciation

Language has other pronunciations of ja and en

language = "zh-cn"

4. Convert text to speech

tts = gTTS(text=text, lang=language)

5. Save the voice file

local = '/Users/kkstar/Downloads/video/'
tts.save(local+"audio_gtts.mp3")

5. Voice effect

Since mp3 files cannot be inserted into the blog, first convert them to mp4, and everyone can listen to the conversion effect through the sound of mp4.
from moviepy.editor import *
local = '/Users/kkstar/Downloads/video/'
audio = AudioFileClip(local+“audio_gtts.mp3”)
video = ImageClip(local+“demo.jpg”).set_duration(audio.duration)
video = video.set_audio(audio)
video.write_videofile(local+“audio_gtts.mp4”, fps=24)

Demo of text-to-audio effect


Guess you like

Origin blog.csdn.net/weixin_38093452/article/details/130133889