Home >Backend Development >Python Tutorial >How to use Python's gtts library to convert text to audio

How to use Python's gtts library to convert text to audio

WBOY
WBOYforward
2023-06-28 15:51:242340browse

Preface

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

1. Background

Text to audio can help visually impaired people obtain information by listening to sounds; it can also help people easily listen to some long articles or learning materials. Save reading time and fatigue. For some language learners, converting text to audio can help them better master pronunciation and intonation and improve their language expression skills.

2. TTS noun explanation

TTS refers to text-to-speech technology, which can convert text into sound. It enables computer systems to voice-interact with users by converting text into artificial speech.

3. GTTS noun explanation

GTTS is the abbreviation of Google Text-to-Speech, which is a technology that converts text into speech. It can help users quickly generate speech. Through simple API calls, users can easily convert specified text into various audio files with customizable speech. The advantages of GTTS are 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 shortcomings, such as the inability to continuously synthesize audio and the voice-converted audio may not fully meet user expectations.

4. Implementation method gTTS

1. Introduce the library

from gtts import gTTS

2. Define the text that needs to be converted

text = ' Hello everyone, I am Kongkong star. In this article, I will share with you text-to-audio conversion. This is audio converted through gtts. '

3.Set Chinese pronunciation

language and 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, we first convert them to mp4. Everyone passes mp4 sound to hear the transformation effect.
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)

Text to audio effect demonstration

The above is the detailed content of How to use Python's gtts library to convert text to audio. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete