Home >Backend Development >Python Tutorial >How to use Python's gtts library to convert text to audio
Hello everyone, I am Kongkong star. In this article, I will share with you how to use python to convert text into audio.
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.
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.
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.
from gtts import gTTS
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. '
language and other pronunciations of ja and en
language = "zh-cn"
tts = gTTS(text=text, lang=language)
local = '/Users/kkstar/Downloads/video/' tts.save(local+"audio_gtts.mp3")
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!