Python 视频水印批量添加器

news2024/9/20 20:45:42

功能如下可以
一、选择水印位置
二、批量添加水印
三、可添加文本或图片

# -*- 编码:utf-8 -*-
import cv2
import os
import numpy as np
from moviepy.editor import VideoFileClip
from concurrent.futures import ThreadPoolExecutor
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import ImageFont, ImageDraw, Image
 
 
def add_dynamic_watermarks_with_audio(video_path, output_path, watermark_image_paths, watermark_sizes, watermark_speeds,
add_text_watermark, text_watermark_text, text_watermark_color,
text_watermark_position):
try:
video_clip = VideoFileClip(video_path)
audio_clip = video_clip.audio
width, height = video_clip.size
 
if add_text_watermark:
font_path = “msyh.ttc” # Windows字体路径,可以根据需要更改
font_size = 50
font = ImageFont.truetype(font_path, font_size)
text_watermark_canvas = np.zeros((高度,宽度,3),dtype=np.uint8)
text_pil = 图像.fromarray(text_watermark_canvas)
绘制 = ImageDraw.Draw(text_pil)
text_bbox = draw.textbbox((0, 0), text_watermark_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
 
如果 text_watermark_position == “中心”:
text_x = (宽度 - text_width) / 2
text_y = (高度 - text_height) / 2
elif text_watermark_position == “左上”:
text_x = 10
text_y = 10
elif text_watermark_position == “右上”:
text_x = 宽度 - text_width - 10
text_y = 10
elif text_watermark_position == “左下”:
text_x = 10
text_y = 高度 - text_height - 10
elif text_watermark_position == “右下”:
text_x = 宽度 - text_width - 10
text_y = 高度 - text_height - 10
 
draw.text((text_x, text_y), text_watermark_text, font=font, fill=text_watermark_color)
text_watermark_canvas = np.array(text_pil)
 
watermark_positions = [(0, 0)] * len(watermark_image_paths)
 
def process_frame(t, x):
非本地 watermark_positions
frame = video_clip.get_frame(t)
 
for i in range(len(watermark_image_paths)):
watermark_image_path = watermark_image_paths[i]
watermark_size = watermark_sizes[i]
watermark_speed = watermark_speeds[i]
watermark_image = cv2.imread(watermark_image_path)
 
watermark_height, watermark_width, _ = watermark_image.shape
如果宽度或watermark_height >高度watermark_width >:
scale_factor = min(宽度 / watermark_width, 高度 / watermark_height)
watermark_image = cv2.resize(watermark_image, (
int(watermark_width * scale_factor), int(watermark_height * scale_factor)),
interpolation=cv2.INTER_LINEAR)
 
if t % watermark_speed == 0:
while True:
x = np.random.randint(0, width - watermark_image.shape[1])
y = np.random.randint(0, height - watermark_image.shape[0])
too_close = 范围 (i) 中 j 的 False
 
:
dist = np.sqrt((x - watermark_positions[j][0]) ** 2 + (y - watermark_positions[j][1]) ** 2)
if dist < watermark_image.shape[1] 或 dist < watermark_image.shape[0]:
too_close = True
break
 
if not too_close:
watermark_positions[i] = (x, y)
break
else:
x, y = watermark_positions[i]
 
watermark_resized = cv2.resize(watermark_image, (
int(watermark_size * watermark_width), int(watermark_size * watermark_height)))
alpha = watermark_resized[:, :, 0] / 255.0
for c in range(3):
frame[y: y + watermark_resized.shape[0], x: x + watermark_resized.shape[1], c] = (
frame[y: y + watermark_resized.shape[0], x: x + watermark_resized.shape[1], c] * (1 - alpha)
+ watermark_resized[:, :, c] * alpha
)
 
if add_text_watermark:
frame_with_text = cv2.addWeighted(frame, 1, text_watermark_canvas, 0.7, 0)
frame = frame_with_text
 
返回帧
 
processed_clip = video_clip.fl(lambda gf, t: process_frame(t, gf))
final_clip = processed_clip.set_audio(audio_clip)
final_clip.write_videofile(output_path, codec='libx264', audio_codec=“aac”)
 
return “水印添加完成!”
except Exception as e:
print(f“处理视频时出错 {video_path}: {e}”)
return f“处理视频 {video_path} 时出错:{e}”
 
 
def batch_add_watermarks_in_directory_with_audio(directory, output_directory, watermark_image_paths, watermark_sizes,
watermark_speeds, add_text_watermark=False, text_watermark_text=None,
text_watermark_position='右下'):
file_list = os.listdir(directory)
video_files = [文件在file_list if file.lower().endswith(('.avi', '.mp4', '.mov', '.mkv'))]
 
将 ThreadPoolExecutor() 作为执行器:
futures = []
for file_name in video_files:
file_path = os.path.join(directory, file_name)
output_file_path = os.path.join(output_directory, file_name)
futures.append(executor.submit(
add_dynamic_watermarks_with_audio,
file_path, output_file_path, watermark_image_paths, watermark_sizes, watermark_speeds,
add_text_watermark, text_watermark_text, (255, 255, 255), text_watermark_position
))
 
for future in futures:
result = future.result()
print(result)
 
return “批量处理完成!”
 
 
def select_directory(entry):
directory =filedialog.askdirectory()
entry.delete(0, tk.END)
entry.insert(0, directory)
 
 
def select_files(entry, file_list, size_list, speed_list):
files = filedialog.askopenfilenames(filetypes=[(“Image files”, “*.png;*.jpg;*.jpeg”)])
for file in files:
file_list.append(file)
entry.insert(tk.END, file)
size_list.append(float(input(f“请输入水印图片{file}的大小(0~1之间):”)))
speed_list.append(int(input(f“请输入水印图片{file}的速度:”))))def
 
 
start_processing():
directory = input_dir_entry.get()
output_directory = output_dir_entry.get()
os.makedirs(output_directory, exist_ok=True)
 
add_text_watermark = text_watermark_var.get()
text_watermark_text = text_watermark_entry.get() 如果add_text_watermark其他 None text_watermark_position
= text_watermark_position_var.get()
 
batch_add_watermarks_in_directory_with_audio(
directory, output_directory, watermark_image_paths, watermark_sizes, watermark_speeds, add_text_watermark,
text_watermark_text, text_watermark_position
)
 
messagebox.showinfo(“完成”, “批量处理完成!”)
 
 
root = tk.Tk()
root.title(“视频水印批量添加器”)
 
tk.Label(root, text=“输入目录:”).grid(row=0, column=0)
input_dir_entry = tk.Entry(root, width=50)
input_dir_entry.grid(row=0, column=1)
tk.Button(root, text=“选择”, command=lambda: select_directory(input_dir_entry)).grid(row=0, column=2)
 
tk.Label(root, text=“输出目录:”).grid(row=1, column=0)
output_dir_entry = tk.Entry(root, width=50)
output_dir_entry.grid(row=1, column=1)
tk.Button(root, text=“选择”, command=lambda: select_directory(output_dir_entry)).grid(row=1, column=2)
 
tk.Label(root, text=“水印图片:”).grid(row=2, column=0)
watermark_image_listbox = tk.Listbox(root, width=50)
watermark_image_listbox.grid(row=2, column=1)
watermark_image_paths = []
watermark_sizes = []
watermark_speeds = []
tk。Button(root, text=“添加”,
command=lambda: select_files(watermark_image_listbox, watermark_image_paths, watermark_sizes,
watermark_speeds)).grid(row=2, column=2)
 
text_watermark_var = tk.BooleanVar()
tk 中。Checkbutton(root, text=“添加文本水印”, variable=text_watermark_var).grid(row=3, column=0)
text_watermark_entry = tk.Entry(root, width=50)
text_watermark_entry.grid(row=3, column=1)
 
tk.Label(root, text=“文本水印位置:”).grid(row=4, column=0)
text_watermark_position_var = tk.StringVar(value=“右下”)
text_watermark_position_options = [“左上”, “右上”, “左下”, “右下”, “中心”]
text_watermark_position_menu = tk.OptionMenu(root, text_watermark_position_var, *text_watermark_position_options)
text_watermark_position_menu.grid(row=4, column=1)
 
tk。Button(root, text=“开始处理”, command=start_processing).grid(row=5, column=0, columnspan=3)
 
root.mainloop()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1931510.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【深度学习】FaceChain-SuDe,免训练,AI换脸

https://arxiv.org/abs/2403.06775 FaceChain-SuDe: Building Derived Class to Inherit Category Attributes for One-shot Subject-Driven Generation 摘要 最近&#xff0c;基于主体驱动的生成技术由于其个性化文本到图像生成的能力&#xff0c;受到了广泛关注。典型的研…

PostgreSQL使用(二)

说明&#xff1a;本文介绍PostgreSQL的DML语言&#xff1b; 插入数据 -- 1.全字段插入&#xff0c;字段名可以省略 insert into tb_student values (1, 张三, 1990-01-01, 88.88);-- 2.部分字段插入&#xff0c;字段名必须写全 insert into tb_student (id, name) values (2,…

分享3个好用的启动盘u盘制作工具

对于经常需要安装维护电脑的同学&#xff0c;制作一个可启动的U盘是非常有必要的。小编今天就和大家分享三款优秀的U盘启动盘制作工具&#xff1a;Ventoy、UltraISO和Rufus。 1. Ventoy Ventoy是一款开源的启动U盘制作工具&#xff0c;它支持将ISO、WIM、IMG、VHD(x)和EFI等类…

SpringMVC 控制层框架-上

一、SpringMVC简介 1. 介绍 Spring Web MVC 是基于Servlet API构建的原始Web框架&#xff0c;从一开始就包含在Spring Framework 中。在控制层框架经历Srust、WebWork、Strust2等诸多产品的历代更迭之后&#xff0c;目前业界普遍选择了SpringMVC 作为Java EE项目表述层开发的首…

如何通过网络快速搜寻到自己的STM32设备

目录 一、问题概述 二、解决思路 三、代码实现 1.创建任务 2.UDP广播接收 一、问题概述 以前一直用RS232串口修改设备配置信息&#xff0c;但是现场施工人员的232线太细&#xff0c;经常容易断掉&#xff0c;这次准备用网口去修改&#xff0c;遇到了一个问题&#xff0c;…

WINUI或WPF灵活使用样式、控件模板、自定义控件、用户控件

在WINUI与WPF 中&#xff0c;控件模板&#xff08;ControlTemplate&#xff09;、样式&#xff08;Style&#xff09;、自定义控件&#xff08;CustomControl&#xff09;和用户控件&#xff08;UserControl&#xff09;都是构建复杂和灵活用户界面的重要工具&#xff0c;但它们…

vue3 中 lottie-web 封装组件

用到的JSON文件在“我的资源”里&#xff0c;下面这个链接直达 下面的代码中用到的JSON数据源 Lottie.vue <script setup> import { ref, onMounted } from vue import lottie from lottie-web// 设置组件参数 const props defineProps({renderer: {type: String,def…

手把手带你白嫖10年服务器

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 &#x1f38f;&#xff1a;你只管努力&#xff0c;剩下的交给时间 &#x1f3e0; &#xff1a;小破站 手把手带你白嫖10年服务器 如何获取如何使用成果个人网站 个人邮箱服务 重要的话重要说&#xff…

Ubuntu部署K8S集群-图文并茂(超详细)

Ubuntu部署K8S集群 1. 模版机系统环境准备1.1 安装Ubuntu1.2 设置静态IP地址 2. 主机准备2.1 使用模板机创建主机2.2 主机配置2.2.1 修改静态IP2.2.2 修改主机名2.2.3 主机名-IP地址解析2.2.4 时间同步2.2.5 内核转发、网桥过滤配置2.2.6 安装ipset和ipvsadm2.2.7 关闭SWAP分区…

【射频器件供应】Flann Microwave

国家 United Kingdom 地址 Flann Microwave Ltd Dunmere Road Bodmin, Cornwall PL31 2QL United Kingdom Flann Microwave于1956年成立于泰晤士河畔金斯顿萨里。在过去的四十年里&#xff0c;Flann Microwave一直是市场领先的天线设计公司&#xff0c;其精密微波器件和测试频…

鸿蒙开发:Universal Keystore Kit(密钥管理服务)【获取密钥属性(ArkTS)】

获取密钥属性(ArkTS) HUKS提供了接口供业务获取指定密钥的相关属性。在获取指定密钥属性前&#xff0c;需要确保已在HUKS中生成或导入持久化存储的密钥。 开发步骤 指定待查询的密钥别名keyAlias&#xff0c;密钥别名最大长度为64字节。调用接口[getKeyItemProperties]&…

Qt类 | QAbstractButton类详解

文章目录 一、QAbstractButton类介绍二、Properties&#xff08;属性&#xff09;三、Public Functions&#xff08;公共函数&#xff09;1.构造函数2.autoExclusive与setAutoExclusive函数--获取/设置自动互斥状态3.autoRepeat与setAutoRepeat函数--获取/设置自动重复状态4.au…

Magic Insert的奇特功能

当然可以&#xff01;让我为你详细介绍一下 Magic Insert 这个有趣的项目。 Magic Insert 是一个基于 AI 技术的创意工具&#xff0c;它允许我们从一张图像中提取一个主题&#xff0c;并将其以不同风格插入到另一张图像中&#xff0c;使得插入效果既符合目标图像的风格&#x…

Arduino呼吸灯

本次学习的内容 1、信号的输入与输出以及信号的分类。 2、理解数字信号与模拟信号以及它们的区别。 3、学会通过模拟输出的方式完成灯的呼吸效果。 Arduino中信号的分类 模拟信号|数字信号 模拟信号&#xff1a;是指用连续变化的物理量所表达的信息&#xff0c;如温度、湿…

基于springboot和mybatis的RealWorld后端项目实战一之hello-springboot

新建Maven项目 注意archetype选择quickstart pom.xml 修改App.java App.java同级目录新增controller包 HelloController.java package org.example.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotatio…

VScode终端和外部终端中文乱码问题

VScode终端和外部终端中文乱码问题 前言VScode终端VScode的第二大特点方法一方法二外部终端&#xff08;命令为ctrlf5&#xff09; 总结实现VScode终端和外部终端都能运行可执行文件 心得 前言 如果只想要看解决方案可直接跳转到总结部分&#xff0c;其余的章节只是用来说明原…

解决多源异构数据整合难题“良策“,助企业高效管理数据资产

大数据的兴起&#xff0c;为各行各业的数字化转型注入了新动力&#xff0c;数据量的激增已成为不争的事实。在过去的十年中&#xff0c;我们见证了数据总量的近十倍增长&#xff0c;不同类型、格式和结构的数据不断涌现&#xff0c;数据的来源也变得极为广泛&#xff0c;涵盖了…

Logback日志异步打印接入指南,输出自定义业务数据

背景 随着应用的请求量上升&#xff0c;日志输出量也会成线性比例的上升&#xff0c;给磁盘IO带来压力与性能瓶颈。应用也遇到了线程池满&#xff0c;是因为大量线程卡在输出日志。为了缓解日志同步打印&#xff0c;会采取异步打印日志。这样会引起日志中的追踪id丢失&#xf…

记录些MySQL题集(9)

MySQL之死锁问题分析、事务隔离与锁机制的底层原理剖析 一、MySQL中的死锁现象 所谓的并发事务&#xff0c;本质上就是MySQL内部多条工作线程并行执行的情况&#xff0c;也正由于MySQL是多线程应用&#xff0c;所以需要具备完善的锁机制来避免线程不安全问题的问题产生&#…

leetcode简单题26 N.118 杨辉三角 rust描述

// 动态规划 pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {let mut triangle: Vec<Vec<i32>> vec![];for i in 0..num_rows {let mut row vec![1; (i 1) as usize];for j in 1..i as usize {row[j] triangle[(i - 1) as usize][(j - 1)]…