本文主要是介绍利用opencv + moviepy制作批量剪辑脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近工作上需要剪辑大量的视频,网上找不到合适的模板。所以直接用python 做一个
完整代码如下:
from moviepy.editor import *
from PIL import Image
import cv2
import os
import numpy as np
def format_img(path):img = cv2.imread(path)print(path)top = (1280 - img.shape[0])//2left = (720 - img.shape[1])//2img_new = cv2.copyMakeBorder(img, top, top, left, left, cv2.BORDER_CONSTANT, None, (0,0, 0))cv2.imwrite(path, img_new)def format_mask():path = './mask'for file in os.listdir(path):out_path = f'{path}/{file}'img = cv2.imread(out_path)img1 =cv2.resize(img, (720, 1280), interpolation=cv2.INTER_CUBIC)cv2.imwrite(out_path, img1) # format_mask()
def format_video(fileName, input_path, out_path):my_clip = VideoFileClip(input_path, audio=False)#获取书法作品hand_path = f'./photo/{fileName}.png'# 人物照片man_path = f'./man/{fileName}.png'format_img(hand_path)format_img(man_path)# 获取音乐music_list = os.listdir('./music')music_path = np.random.choice(music_list)music = AudioFileClip(f'./music/{music_path}')music.max_volume()# 字幕text_clip = TextClip('小金熊趣味硬笔', fontsize=70, font='./msyhl.ttc').set_duration(my_clip.duration).set_position(('left','bottom'))my_clip1 = CompositeVideoClip([my_clip, text_clip])clip = ImageSequenceClip([hand_path, man_path], fps = 1, durations=3)cc = concatenate_videoclips([my_clip1, clip])cc.set_audio(music)cc.audio = musiccc.write_videofile(out_path, audio_codec='aac')def main():path = './video'for file in os.listdir(path):out_path = f'./out_video/{file}'video_path = f'{path}/{file}'print(file.split('.')[0])format_video(file.split('.')[0], video_path, out_path)
main()
工程目录如下:
这篇关于利用opencv + moviepy制作批量剪辑脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!