python 视频图片合成固定尺寸
2020-04-13 本文已影响0人
Edviin_2de8
from moviepy.editor import VideoFileClip, concatenate_videoclips,CompositeVideoClip
import cv2
from moviepy.editor import *
from tes import ImgText
import os, sys
# 背景图大小 #彩图
# 5.5 存 2202 1242
#6.6 寸 2688 1242
# 背景视频
# 6.5 1920 * 1080
#5.5 1920* 886
mp3_file = 'resource/mymusic.mp3'
cp_back_file_5 = 'resource/威尼斯/1-5.5.jpg'
cp_back_file_6 = 'resource/威尼斯/1-6.5.jpg'
normal_back_file_5 = 'temp/backResult.png'
normal_back_file_6 = 'temp/backResult.png'
record_file_5 = 'resource/5cun.mov'
record_file_6 = 'resource/5cun.mov'
class myObject(object):
height = 0
tem_name = ''
def __init__(self):
print("开始")
def comBin(self,clip_back, caipiao, recored,fin_name):
print('正在合成')
video = CompositeVideoClip([clip_back, recored.set_pos((1200, 0)), caipiao], size=(1920,self.height))
print("合成成功")
video.write_videofile('temp/'+fin_name,codec='libx264', fps=clip_back.fps, audio=True)
# self.addAudio(fin_name)
def one_pic_to_video(self,image_path, output_video_path, fps, time):
"""
一张图片合成视频
one_pic_to_video('./../source/1.jpeg', './../source/output.mp4', 25, 10)
:param path: 图片文件路径
:param output_video_path:合成视频的路径
:param fps:帧率
:param time:时长
:return:
"""
print('正在写背景')
image_clip = ImageClip(image_path)
# img_width, img_height = image_clip.w, image_clip.h
img_width, img_height = 1920, self.height
# 总共的帧数
frame_num = (int)(fps * time)
img_size = (int(img_width), int(img_height))
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video = cv2.VideoWriter(output_video_path, fourcc, fps, img_size)
for index in range(frame_num):
frame = cv2.imread(image_path)
# 直接缩放到指定大小
frame_suitable = cv2.resize(frame, (img_width, img_height), interpolation=cv2.INTER_CUBIC)
# 把图片写进视频
# 重复写入多少次
video.write(frame_suitable)
# 释放资源
video.release()
print('背景写入成功')
return VideoFileClip(output_video_path)
def creat6CunVideo(self):
self.tem_name = '6cun.mp4'
self.height = 886
# 背景图片
clip_back = self.one_pic_to_video(normal_back_file_6, "temp/back.mp4", 30, 20)
print(clip_back.size)
# 彩票
clip_caipia = self.one_pic_to_video(cp_back_file_6, "temp/caipiao.mp4", 100, 0.01).set_start(17.0)
print(clip_back.size)
# 录屏
clip_recored = VideoFileClip(record_file_6).resize(height=self.height).set_duration(20)
self.comBin(clip_back,clip_caipia,clip_recored,self.tem_name)
def creat5CunVideo(self):
self.tem_name = '5cun.mp4'
self.height = 1080
# 背景图片
clip_back = self.one_pic_to_video(normal_back_file_5, "temp/back.mp4", 30, 20)
# 彩票
clip_caipia = self.one_pic_to_video(cp_back_file_5, "temp/caipiao.mp4", 100, 0.01).set_start(17)
# 录屏
clip_recored = VideoFileClip(record_file_5).resize(height=self.height).set_duration(20)
self.comBin(clip_back,clip_caipia,clip_recored,self.tem_name)
def addAudio(self,file_name):
print('正在合成声音')
path_audio = AudioFileClip(mp3_file)
print(path_audio.duration)
clip1 = VideoFileClip('temp/' + file_name)
clip2= clip1.set_audio(path_audio)
clip2.write_videofile('final/' + file_name,codec='libx264', fps=30,audio_codec="aac")
# os.remove("temp/back.mp4")
# os.remove("temp/caipiao.mp4")
# os.remove("temp/"+self.tem_name)
print("生成成功")
if __name__ == '__main__':
obj = myObject()
# obj.creat6CunVideo()
# obj.creat5CunVideo()