Python + Moviepy编辑视频

安装moviepy:

pip install moviepy==1.0.0

1.视频加水印

import moviepy.editor as mp

# 待加水印的视频
src = r'D:\Users\PC3\Desktop\t\1.mp4'
# 加好水印的存储目标文件
dst = r'D:\Users\PC3\Desktop\t\2.mp4'
#  水印地址
logo_png = r'D:\Users\PC3\Desktop\t\logo.png'

video = mp.VideoFileClip(src)

logo = (mp.ImageClip(logo_png)
          .set_duration(video.duration)  # 水印持续时间
          .resize(height=100)  # 水印的高度,会等比缩放
          .margin(right=0, top=0, opacity=100)  # 水印边距和透明度
          .set_pos(("left","top")))  # 水印的位置

final = mp.CompositeVideoClip([video, logo])

# mp4文件默认用libx264编码, 比特率单位bps
final.write_videofile(dst, codec="libx264", bitrate="10000000")

2. 视频固定位置加色块

from moviepy.editor import *
# 待加水印的视频
src = r'D:\Users\PC3\Desktop\t\1.mp4'
# 加好水印的存储目标文件
dst = r'D:\Users\PC3\Desktop\t\2.mp4'

video = VideoFileClip(src)
mask = (ColorClip((300,100),(225,225,225))
        .set_position((0,5))
        .set_duration(video.duration))
CompositeVideoClip([video,mask]).write_videofile(dst)

你可能感兴趣的:(经验总结)