環境配置
筆者環境:win10專業版, python3.8.0
pip install ez_setuppip install moviepy
添加圖片水印
import moviepy.editor as mp
#本地視頻位置
video = mp.VideoFileClip(r"D:\resource\myresource\4393644710024974337.mp4")
#准備log圖片
logo = (mp.ImageClip(r"C:\Users\zrail\Downloads\touxiang.jpeg")
.set_duration(video.duration) # 水印持續時間
.resize(height=100) # 水印的高度,會等比縮放
.margin(right=8, top=8, opacity=1) # 水印邊距和透明度
.set_pos(("left","center"))) # 水印的位置
final = mp.CompositeVideoClip([video, logo])
# mp4文件默認用libx264編碼, 比特率單位bps
final.write_videofile("test.mp4", codec="libx264", bitrate="10000000")
報錯
RuntimeError: The current Numpy installation ('C:\\Users\\zrail\\.conda\\envs\\py380\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime. See this issue for more information: https://tinyurl.com/y3dm3h86
- 使用搜索引擎找到解決方案,切換numpy版本,鏈接:https://github.com/numpy/numpy/issues/17726
添加文字水印
# -*- coding: utf-8 -*-
from moviepy.editor import VideoFileClip,CompositeVideoClip
#本地視頻位置
from moviepy.video.VideoClip import TextClip
#subclip視頻截取開始時間和結束時間
video = VideoFileClip(r"D:\resource\myresource\4393644710024974337.mp4").subclip(0,1)
#制作文字,指定文字大小和顏色
txt_clip = ( TextClip("zxl shuiyin",fontsize=30,color='red')
.set_position('center')#水印內容居中
.set_duration(1) )#水印持續時間
result = CompositeVideoClip([video, txt_clip]) #在視頻上覆蓋文本
result.write_videofile("tttttest.mp4",fps=25)#fps:視頻文件中每秒的幀數
報錯
- `OSError: MoviePy Error: creation of None failed because of the following error:
[WinError 2] 系統找不到指定的文件。.
.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect`
- 翻譯報錯信息並使用搜索引擎找到解決方案,鏈接:https://stackoverflow.com/questions/51928807/moviepy-cant-detect-imagemagick-binary-on-windows
- 安裝ImageMagick,並修改源碼鏈接:https://blog.csdn.net/qq_23944915/article/details/86514301
具體沒有仔細研究,水印的各種細節有待處理,這里只是拋磚引玉。剩下的靠大家了
