1. 程式人生 > 實用技巧 >python為視訊新增水印

python為視訊新增水印

環境配置

筆者環境:win10專業版, python3.8.0

  1. pip install ez_setup
  2. pip 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")
報錯
  1. 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

新增文字水印

# -*- 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:視訊檔案中每秒的幀數
報錯
  1. `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`

具體沒有仔細研究,水印的各種細節有待處理,這裡只是拋磚引玉。剩下的靠大家了