import os

os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
-1] f_mp3 = '{}{}{}'.format(this_file_dirname, os_sep, 'auido.mp3')
from playsound import playsound import time
import math this_time = time.time() # playsound(f_mp3) # t_spend = time.time() - this_time
t_spend = 58.777058839797974
# 音频的秒数
t_spend = math.ceil(t_spend)
import cv2
import glob '''
python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
链接:https://www.zhihu.com/question/49558804/answer/343058915 OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html '''
# 每秒传输帧数(Frames Per Second)
fps = 100 # 保存视频的FPS,可以适当调整 FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会愈流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。
fps = 15
fps = 5 fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0 f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi')
f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
imgs = glob.glob(f_img_d) img = cv2.imread(imgs[0])
img_size = (img.shape[1], img.shape[0])
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size) """
用图片总数均分音频时间
"""
os_delay_factor = 0.14
os_delay_factor = 0.11
myinterval = t_spend / len(imgs) * os_delay_factor f, l = 'mybaidu.parp.txt', []
with open(f, 'r', encoding='utf-8') as fr:
for i in fr:
ii = i.replace('\n', '')
l.append(ii)
char_loop_l = []
for i in l:
mystr, le = '', len(i)
for ii in range(le):
iii = i[ii]
print('-----', iii)
mystr = '{}{}'.format(mystr, iii)
print(mystr)
char_loop_l.append(iii) #
# from fontTools.ttLib import TTFont
# myfont = TTFont('simhei.ttf') from PIL import Image, ImageDraw, ImageFont myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") import cv2
import numpy as np os_delay_factor = 0.23
myinterval = t_spend / (len(char_loop_l) * 1) * os_delay_factor
for i in l:
i_index=l.index(i)
img_index=i_index%len(imgs)
imgname=imgs[img_index] mystr, le = '', len(i)
for ii in range(le):
iii = i[ii]
print('-----', iii)
if len(mystr) % 15 == 0:
mystr = '{}{}'.format(mystr, '\n')
mystr = '{}{}'.format(mystr, iii)
print(mystr) this_time = time.time()
while time.time() - this_time < myinterval:
print(imgname) frame = cv2.imread(imgname)
frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式 draw = ImageDraw.Draw(frame_pil)
font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8")
# 第一个参数为字体,中文黑体
# 第二个为字体大小
ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font) '''
frame_pil:目标图像
第一个参数为打印的坐标
第二个为打印的文本
第三个为字体颜色
第四个为字体
''' frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
img = frame_cv2
videoWriter.write(img) videoWriter.release()

  

原理是将字幕写到图片中,然后再写入视频的帧中

对图片适应

import os, time, glob
import cv2 os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
-1] f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi')
f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
imgs, img_size_d = glob.glob(f_img_d), {}
for i in imgs:
img = cv2.imread(i)
w_h_s = '{},{}'.format(img.shape[1], img.shape[0])
if w_h_s not in img_size_d:
img_size_d[w_h_s] = 1
else:
img_size_d[w_h_s] += 1 mode_img_size_wh = [int(i) for i in sorted(img_size_d.items(), key=lambda img_size_d: img_size_d[0], reverse=True)[0][0].split(',')] import os os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[-1] f_mp3 = '{}{}{}'.format(this_file_dirname, os_sep, 'auido.mp3')
from playsound import playsound import time
import math this_time = time.time() # playsound(f_mp3) # t_spend = time.time() - this_time
t_spend = 58.777058839797974
# 音频的秒数
t_spend = math.ceil(t_spend)
import cv2
import glob '''
python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
链接:https://www.zhihu.com/question/49558804/answer/343058915 OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html '''
# 每秒传输帧数(Frames Per Second)
fps = 100 # 保存视频的FPS,可以适当调整 FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会愈流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。 fps, fourcc = 15, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
# fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[0], mode_img_size_wh[1])) f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi')
f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg')
imgs = glob.glob(f_img_d) """
用图片总数均分音频时间
"""
os_delay_factor = 0.14
os_delay_factor = 0.11
myinterval = t_spend / len(imgs) * os_delay_factor f, l = 'mybaidu.parp.txt', []
with open(f, 'r', encoding='utf-8') as fr:
for i in fr:
ii = i.replace('\n', '')
l.append(ii)
char_loop_l = []
for i in l:
mystr, le = '', len(i)
for ii in range(le):
iii = i[ii]
print('-----', iii)
mystr = '{}{}'.format(mystr, iii)
print(mystr)
char_loop_l.append(iii) #
# from fontTools.ttLib import TTFont
# myfont = TTFont('simhei.ttf') def resize_rescale_pilimg(img_f, w_h_tuple=(mode_img_size_wh[0],mode_img_size_wh[1]), mid_factor=1):
img_n,img_type=img_f.split('.')[-2],img_f.split('.')[-1]
img_type = 'PNG'
img_f_new = '{}{}{}'.format(img_n, 'resize_rescale.', img_type)
mid_icon = Image.open(img_f)
mid_icon_w, mid_icon_h = w_h_tuple[0] * mid_factor, w_h_tuple[1] * mid_factor
mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS)
mid_icon.save(img_f_new,img_type)
return img_f_new from PIL import Image, ImageDraw, ImageFont myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8") import cv2
import numpy as np os_delay_factor = 0.245
myinterval = t_spend / (len(char_loop_l) * 1) * os_delay_factor
for i in l:
i_index = l.index(i)
img_index = i_index % len(imgs)
imgname = imgs[img_index] mystr, le = '', len(i)
for ii in range(le):
iii = i[ii]
print('-----', iii)
if len(mystr) % 15 == 0:
mystr = '{}{}'.format(mystr, '\n')
mystr = '{}{}'.format(mystr, iii)
print(mystr) this_time = time.time()
while time.time() - this_time < myinterval:
print(imgname) frame = cv2.imread(imgname) if (frame.shape[1], frame.shape[0]) != (mode_img_size_wh[0], mode_img_size_wh[1]):
imgname = resize_rescale_pilimg(imgname)
frame = cv2.imread(imgname)
else:
pass frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_pil = Image.fromarray(frame_cv2) # 转为PIL的图片格式 draw = ImageDraw.Draw(frame_pil)
font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8")
# 第一个参数为字体,中文黑体
# 第二个为字体大小
ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font) '''
frame_pil:目标图像
第一个参数为打印的坐标
第二个为打印的文本
第三个为字体颜色
第四个为字体
''' frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
img = frame_cv2
videoWriter.write(img) videoWriter.release() '''
待解决:
0-同一批的图片的尺寸不同
1-视频、音频播放时长的同步系数的自计算
2- 字幕 ''' from PIL import Image #
#
# logo = 'fugu.png'
# mid_icon = Image.open(logo)
# mid_icon_w, mid_icon_h = mid_icon.size
# mid_factor = 5
# mid_icon_w, mid_icon_h = mid_icon_w * mid_factor, mid_icon_h * mid_factor
# mid_icon = mid_icon.resize((mid_icon_w * 3, mid_icon_h), Image.ANTIALIAS)
# mid_icon.save('fugu.2.png', 'PNG')

  

百度MP3+图片+文字:生成结果文件;(声音58秒,视频59秒,同步性需要进一步优化)的更多相关文章

  1. 一篇文章搞定百度OCR图片文字识别API

    一篇文章搞定百度OCR图片文字识别API https://www.jianshu.com/p/7905d3b12104

  2. 百度UEditor图片上传或文件上传路径自定义

    最近在项目中使用到百度UEditor的图片以及文件上传功能,但在上传的时候路径总是按照预设规则来自动生成,不方便一些特殊文件的维护.于是开始查看文档和源代码,其实操作还是比较简单的,具体如下: 1.百 ...

  3. 记录:将图片数据生成 tfrecords 文件并在训练使用时读取

    直接用别人的就行了: https://github.com/myCVs/GenTFRecords

  4. 图片字节流生成bmp文件

    1 BITMAPFILEHEADER bfh;//文件头 2 bfh.bfType=0x4d42; bfh.bfOffBits=sizeof(bfh)+sizeof(BITMAPINFOHEADER) ...

  5. JavaWeb项目生成PDF文件添加水印图片并导出

    一.前言 首先需要在Maven中添加相应的jar包依赖,若项目没用到Maven,也可自行下载相应所需的jar包(itextpdf.jar 与 itext-asian.jar),如下图所示.点此下载 M ...

  6. 多图片生成pdf文件

    这里记录多个图片合并生成一个pdf文件的方法. 首先maven引入所需jar包: <dependency> <groupId>com.itextpdf</groupId& ...

  7. Delphi文字转语音TTS【支持选择语音库,播放,暂停,开始,停止,生成语音文件,设置音量,设置语速】

    作者QQ:(648437169) 点击下载➨文字转语音TTS [Delphi 文字转语音TTS]调用系统自带的TTS组件,支持XP,vista,win7,win8,win10系统,支持选择语音库,播放 ...

  8. 用 Python 简单生成 WAV 波形声音文件

    Python 简单生成 WAV 波形声音文件 让机器发出声响,本身就是一件充满魔法的事情.有没有想过,用一段简单的代码,生成一个最简单的声音呢?Python 这门脚本语言的库十分丰富,借助于其中的三个 ...

  9. 百度开源富文本编辑器 UEditor配置:图片上传和文件上传独立使用方法

    使用UEditor编辑器自带的插件实现图片上传和文件上传功能,这里通过配置UEditor单独使用其内置的第三方插件swfupload来实现图片和文件的上传,通过对UEditor配置轻松实现图片批量上传 ...

随机推荐

  1. 还是关于编码——decode & encode的探究

    最近被py3.4中的编码折磨的不要不要的,decode & encode的使用.功能貌似在2.7—3.0有一个巨大的变化.网上查询的一些解答很多是基于2.7中的unicode功能,给出的解答是 ...

  2. swift protocol 与类继承结合时的bug

    protocol CommonTrait: class { func commonBehavior() -> String } extension CommonTrait { func comm ...

  3. sharepoint services

    I have got solution for authentication to share point web service I have use fedAuth Cookie and rtfa ...

  4. jmeter的JDBC Request接口测试

    Jmeter操作Mysql 测试计划添加.jar包 mysql-connector-java-5.1.7-bin.jar用于使Jmeter可以读取Mysql: 线程组添加 JDBC Connectio ...

  5. eclipse 新建 maven 项目 + 消除错误

    安装eclips以及maven自行去了解,这里不讲解 一.新建一个 maven 项目. 二.下一步选择项目架构 三.填写相关信息 group id: 一般都是 com点 项目名 aftriact.id ...

  6. CAD如何直接打印,不出现打印对话框?

    主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...

  7. Spring自定义拦截器

    HandlerInterceptorAdapter由Spring MVC提供,用来拦截请求. 实现自定义拦截器需要继承HandlerInterceptorAdapter或实现HandlerInterc ...

  8. String类的转换功能

    /* * String类的转换功能 * char[] toCharArray():把字符串转换为字符数组 * String toLowerCase():把字符串转换为小写字符串 * String to ...

  9. Windows 硬件开发人员怎样选择代码签名证书类型

    在建立 Windows 开发人员中心硬件仪表板帐户之前,你需要获取代码签名证书以保护数字信息的安全.此证书是用于建立你的公司对你所提交代码的所有权的接受标准.它让你可以用数字形式签署 PE 二进制文件 ...

  10. Java Web学习总结(30)——Service层在MVC框架中的意义和职责

    mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层设计, ...