python 音频合成,图片转视频


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')
posted @   记录——去繁就简  阅读(190)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示