sub to audio
1.0.0
字幕到音频,使用coqui-ai tts从任何字幕文件中生成音频/语音,并根据字幕时间同步音频时间。
演示:
ffmpeg,pydub,librosa,coqui-ai tts,ffmpeg-python
pip install TTS
pip install git+https://github.com/bnsantoso/sub-to-audiopip install TTS
pip install subtoaudioLinux上的FFMPEG
apt-get install ffmpeg基本用途与Coqui-ai TTS非常相似,您可以检查其文档和<lang-iso_code>。
!注意:使用无重叠的字幕,每秒最佳字符 / cps以获得最佳结果
!注意:使用aegisub之类的软件来编辑您的字幕
from subtoaudio import SubToAudio
# list all model
SubToAudio (). coqui_model ()
# get model index
model = SubToAudio (). coqui_model ()[ 1 ]
# The code will output 'yoursubtitle.wav' in the current directory.
sub = SubToAudio ( model_name = model )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle )
# you can choose 1100 different language using fairseq model
sub = SubToAudio ( fairseq_language = '<lang-iso_code>' )
subtitle = sub . subtitle ( "yoursubtitle.ass" )
sub . convert_to_audio ( sub_data = subtitle )
# specify model name
sub = SubToAudio ( model_name = "tts_models/multilingual/multi-dataset/your_tts" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , output_path = "subtitle.wav" )
# specify model and config path
sub = SubToAudio ( model_path = "path/to/your/model.pth" config_path = "config/path.json" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle )
# speaker=tts.speakers[0] or None if model doesnt have multiple speakers
# language=tts.languages[0] or None if doesnt have multiple languages
# list speaker
sub . speakers ()
speaker1 = sub . speakers ()[ 1 ]
# list languages
sub . languages ()
langu = sub . languages ()[ 0 ]
sub = SubToAudio ( model_name = "tts_models/multilingual/multi-dataset/your_tts" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , language = langu , speaker = speaker1 , output_path = "subtitle.wav" )
# Save temporary audio to current folder
sub = SubToAudio ( model_name = "tts_models/multilingual/multi-dataset/your_tts" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , output_path = "subtitle.wav" , save_temp = True )要使用语音转换方法,您必须通过voice_conversion:bool and speaker_wav:str paramater on self.convert_to_audio 。如果您的模型有多个扬声器,则语音转换将无法运行。
from subtoaudio import SubToAudio
sub = SubToAudio ( fairseq_language = "eng" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , voice_conversion = True , speaker_wav = "voice.wav" , language = "en" )要使用Coqui Studio API,您需要配置COQUI_STUDIO_TOKEN环境变量。
import os
os . environ [ 'COQUI_STUDIO_TOKEN' ] = # yourapi令牌设置后,您可以获取Coqui Studio模型,您可以遵循此名称gudent coqui_studio/en/<studio_speaker_name>/coqui_studio
from subtoaudio import SubToAudio
sub = SubToAudio ( model_name = "coqui_studio/en/Torcull Diarmuid/coqui_studio" , progress_bar = False )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , output_path = "subtitle.wav" , save_temp = True )
# use emotion paramater and speed paramater
sub . convert_to_audio ( sub_data = subtitle , output_path = "subtitle.wav" , emotion = "Happy" , speed = 1.5 )使用tempo_mode参数加快音频。有三种速度模式:
tempo_mode="all" :这加速了所有音频。使用tempo_speed=float指定速度。tempo_mode="overflow" :这加速了音频以匹配总字幕持续时间以及在出现下一个字幕之前的空白持续时间。 'tempo_limit'将限制溢出期间的速度提高。tempo_mode="precise" :这加速了音频以匹配字幕显示的持续时间。” from subtoaudio import SubToAudio
# Speed up tempo or speech rate
sub = SubToAudio ( model_name = "tts_models/de/thorsten/tacotron2-DDC" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "all" , tempo_speed = 1.3 )
# Change the tempo or speech rate of all audio files , default is 1.2
sub = SubToAudio ( "tts_models/multilingual/multi-dataset/xtts_v1" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "all" , tempo_speed = 1.3 )
# Change tempo or speech rate to audio that doesn't match the subtitle duration
sub = SubToAudio ( fairseq_language = "ind" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "overflow" )
# Limit tempo speed on the overflow mode
sub = SubToAudio ( fairseq_language = "ind" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "overflow" , tempo_limit = 1.2 )
# Match audio length to subtitle duration
sub = SubToAudio ( fairseq_language = "ind" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "precise" )shift_mode参数将移动不匹配字幕持续时间的音频。
shift_mode="right" :将音频时间向右移动并防止音频重叠。shift_mode="left" :将音频向左移动并防止音频重叠,但由于某些音频可能会消失,左侧的空间有限。shift_mode="interpose" :将音频转移到中位置,并防止音频重叠的左右。 (注意:此模式可能很笨拙,因此请谨慎使用。)shift_mode="left-overlap" :向左移动音频时间,允许重叠。shift_mode="interpose-overlap" :将音频转移到中位置,允许重叠。shift_limit=int or "str" :限制音频移动,将整数用于毫秒或字符串,例如2.5s秒 from subtoaudio import SubToAudio
# shift mode with limit of 2 second to the right.
sub = SubToAudio ( fairseq_language = "vie" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = sub , tempo_mode = "overflow" , shift_mode = "right" , limit_shift = "2s" )
# shift audio to left position or, time before next subtitle appear
sub = SubToAudio ( fairseq_language = "fra" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = sub , shift_mode = "left-overlap" )
# shift to left, and limit shift only 1 sec.
sub = SubToAudio ( fairseq_language = "ind" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = sub , shift_mode = "left" , shift_limit = 1000 ) # 1000 = 1s from subtoaudio import SubToAudio
# Random Speaker will give you weird result when using bark model with SubToAudio
# Bark random
sub = SubToAudio ( "tts_models/multilingual/multi-dataset/bark" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "overflow" )
# Tortoise random
sub = SubToAudio ( "tts_models/en/multi-dataset/tortoise-v2" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , shift_mode = "overflow" , preset = "fast" )
# To use voice clone you need voice_dir and speaker paramater
# Voice Clone expecting .wav or .npz file inside folder speaker_1
# voice/speaker_1/hana.wav or voice/speaker_1/hana.npz
# if your speaker folder only have .wav file, it will generate .npz file after you runing it.
sub = SubToAudio ( "tts_models/multilingual/multi-dataset/bark" )
subtitle = sub . subtitle ( "yoursubtitle.srt" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "overflow" , voice_dir = "voice/" , speaker = "speaker_1" )
# same with bark, the folder structure like this 'voice/speaker2/ron.wav'
sub = SubToAudio ( "tts_models/en/multi-dataset/tortoise-v2" )
subtitle = sub . subtitle ( "yoursubtitle.ass" )
sub . convert_to_audio ( sub_data = subtitle , tempo_mode = "overflow" , voice_dir = "voice/" , speaker = "speaker2" )Eren,G。和Coqui TTS团队。 (2021)。 Coqui TTS(1.4版)[计算机软件]。 https://doi.org/10.5281/Zenodo.6334862