使用python的moviepy库剪辑视频

1. 安装python 和 moviepy

pip install moviepy
版本:
PS D:\server\code\python> pip show moviepy
Name: moviepy
Version: 1.0.3
Summary: Video editing with Python
Home-page: https://zulko.github.io/moviepy/
Author: Zulko 2017
Author-email:
License: MIT License
Location: C:\Users\xxxx\AppData\Roaming\Python\Python311\site-packages
Requires: decorator, imageio, imageio-ffmpeg, numpy, numpy, proglog, requests, tqdm
Required-by:

如果要使用文字/字幕功能, 需要安装 ImageMagick

- 下载地址: ImageMagick – Download 

- 安装: 双击ImageMagick-7.1.1-15-Q16-HDRI-x64-dll.exe安装(安装时, 要选择把目录添加到环境变量中)

- 安装后, 将magick.exe 复制一份, 并改名为 convert.exe (moviepy要求的)

2. 功能实战

2.1 截取视频片段, 并加上字幕

from moviepy.editor import  *

# 截取一段视频并加上文字
file = "135950408.mp4"
video = VideoFileClip(file).subclip(0,5); # 加载视频, 并截取前5秒
# print(video.size); //视频长宽

#文字
txt = TextClip("Tracy Lamar McGrady Jr.", fontsize=30, color='white')

#底部中间偏上一些, 持续5秒钟
txt = txt.set_position(('center', video.size[1] - 60)).set_duration(5) 

# 另一种用法
# txt_clip = txt_clip.set_position(("center", "bottom")).set_duration(5)

rs = CompositeVideoClip([video, txt]); # 合并视频和文字
rs.write_videofile('tmac.mp4'); # 导出视频

2.2 仅截取片段(天工AI写的, 可用)

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

video_path = "135950408.mp4"
start_time = 3  # 起始时间(以秒为单位)
end_time = 8  # 结束时间(以秒为单位)
target_path = "135950408_05.mp4"

ffmpeg_extract_subclip(video_path, start_time, end_time, targetname=target_path)

2.3 截取指定区域的画面

file = "video.rm"
video = VideoFileClip(file); # 加载视频
vc = video.subclip(0, 5) # 截取前5秒的视频

#视频长宽
print(video.size);

# 截取指定区域: x1,y1是左上角坐标, x2,y2是右下角坐标
width = video.size[0]
height = video.size[1]
x1 = 30;
y1 = 80;
x2 = width - x1
y2 = height - y1

vc = vc.fx(crop, x1, y1, x2, y2) 
vc.write_videofile('video_new.mp4');

2.4 输出为GIF动态图片

from moviepy.editor import VideoFileClip
from moviepy.editor import ImageSequenceClip

file = "./tmac_101106.mp4"
video = VideoFileClip(file);
video.to_gif(file +'.gif');

2.5 扩充视频中的画面(margin)

from moviepy.editor import VideoFileClip
import math

# 加载商品
file = "./tmac_92100.mp4"
video = VideoFileClip(file);
print(video.size)

# 计算长宽需要增加的像素数
width = 720;
height = 720;
wx = width - video.size[0]
hx = height - video.size[1]
left = math.ceil(wx / 2)
right = math.ceil(wx / 2)
top = math.ceil(hx / 2)
bottom = math.ceil(hx / 2)

print([left, right, top, bottom])

# 扩充, 参数说明: https://moviepy.readthedocs.io/en/latest/ref/videofx/moviepy.video.fx.all.margin.html#moviepy.video.fx.all.margin
vc = video.margin(left=left, right=right, top=top, bottom=bottom)

# 导出
vc.write_videofile(file.replace('.mp4', '_'+str(width)+str(height)+'.mp4'));

2.6 合并视频

from moviepy.editor import  *
import datetime

def	ymdhis():
	time = datetime.datetime.now()
	return str(time.year) + str(time.year) + str(time.month) + str(time.day) + str(time.hour) + str(time.minute) + str(time.second)


path = 'D:/server/code/movieclip/output/';
file_list = [path+'2119076041.mp4', path+'1439935443.mp4']
video_list = [];
for file in file_list:
	print(file)
	vc = VideoFileClip(file);
	video_list.append(vc) 

merge = concatenate_videoclips(video_list);
new_file_name = './output/m' + ymdhis() + '.mp4';
merge.write_videofile(new_file_name);

 

3. moviepy文档

moviepy-cn 文档

Classes of Video Clips — MoviePy 1.0.2 documentation (zulko.github.io)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值