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