标签:engine,视频,fps,img,python,音频,video,file,path From: https://www.cnblogs.com/lld76/p/16718089.html
import os
import cv2,re
import time
import pyttsx3
import ffmpeg
import subprocess
from PIL import Image
import numpy as np
import imageio
imageio.plugins.freeimage.download()
# 语音合成
def audio(content):
engine = pyttsx3.init()
zh_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0"
engine.setProperty('voice', zh_voice_id) #设置语音
rate = engine.getProperty('rate') #语速 0-200 当为200时一秒五个字
volume = engine.getProperty('volume') #音量 0-1
print(rate,volume)
engine.save_to_file(content,'test.mp3')
engine.runAndWait() #一定要弄否则报错,数据无法输入保存
engine.stop()
print('audio')
# 图片合成视频
def video(path,fps,size):
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video = cv2.VideoWriter( 'output.mp4', fourcc,fps,size) #fps帧数率: 1.00帧/秒 1张一秒
for file in os.listdir(path):
img = cv2.imread('%s/%s'%(path,file)) #路径不能带中文
if img is None:
continue
img = cv2.resize(img,size)
video.write(img)
video.release()
def gif(path):
import imageio
gifs = []
for file in os.listdir(path):
gifs.append(imageio.imread('%s/%s'%(path,file)))
imageio.mimsave('text.gif',gifs,fps=1)
def unite(audio,video):
file_name = 'img/'
video_file = 'output.mp4'
audio_file = 'test.mp3'
outfile_name = file_name + '-txt.mp4'
subprocess.call('ffmpeg -i ' + video_file
+ ' -i ' + audio_file + ' -strict -2 -f mp4 '
+ outfile_name, shell=True)
# d = '''语语速快慢具有相对性'''
# print(len(re.sub('[,。 ]','',d)))
# audio(d) #
# video(path='img',fps=1,size=(350,250)) #fps帧数率: 1.00帧/秒 1张一秒
gif(path='img')
# os.popen('test.mp3')
# os.popen('output.mp4')
os.popen('text.gif')