python运用百度AI语音辨别合成短视频

python运用百度AI语音辨别合成短视频

用python搭配语音辨别api合成短视频的思绪从来有,即是没有付诸动作,即日看到了有大神仍旧做出来了,就搬运过来,留着此后参考进修

思绪也大略,一段笔墨,一段后台音乐,一段视频,就不妨轻快合成了。

from aip import AipSpeech

from mutagen.mp3 import MP3

from moviepy.editor import *

import traceback

def Generatetxt(somebody,something,other_word):

txt = '''{}{}是如何回事呢?{}断定大师确定很熟习,然而{}{}是如何回事呢,底下就让小编来带着大师一道领会吧!

{}{},本来即是{},大师大概会很诧异{}如何会{}呢?但究竟即是如许,小编也感触特殊诧异。

这即是对于{}{}的工作了,大师有什么办法呢,欢送在指摘区报告小编来一道计划哦!

'''

txt = txt.format(somebody,something,somebody,somebody,something,somebody,something,other_word,somebody,something,somebody,something)

return txt

def GenerateMP3(txt):

APP_ID = '************'

API_KEY = '****************'

SECRET_KEY = '************************'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result = client.synthesis(txt, 'zh', 1, {

'vol': 4, 'per': 0, 'spd': 5

})

if not isinstance(result, dict):

with open('./sound.mp3', 'wb') as f:

f.write(result)

def get_mp3length(path):

audio = MP3(path)

return audio.info.length

def video_add_mp3(file_name, mp31_file,mp32_file,new_filename, time):

video = VideoFileClip(file_name)

audioclip = AudioFileClip(mp31_file)

audioclip2 = AudioFileClip(mp32_file)

compo = CompositeAudioClip([audioclip.set_start(2),

audioclip2.set_start(0)])

print(type(compo))

videoclip = video.set_audio(compo)

videoclip = videoclip.subclip(0, time)

try:

videoclip.write_videofile(new_filename)

video.reader.close()

return new_filename

except:

traceback.print_exc()

return None

if __name__ == '__main__':

print(Generatetxt("健身", "妨害身材", "疏通过渡肌肉伤害"))

GenerateMP3(Generatetxt("健身", "妨害身材", "疏通过渡肌肉伤害"))

Mp3TimeLength = get_mp3length("sound.mp3")

video_add_mp3("video_ori.mp4", 'sound.mp3','bgm.mp3','After.mp4',Mp3TimeLength + 3)

分享到 :

Leave a Reply

Your email address will not be published. Required fields are marked *