当前位置:   article > 正文

python实战之去除视频水印&字幕_python 去除视频水印

python 去除视频水印

一、python去除视频水印的操作步骤

获取资源链接:https://null119.lanzoul.com/b050jscbg

1.下载资源包并解压

        结果如下图所示:

2.安装运行代码所必须的解释器和包

        python软件在官网下载即可,这里我们仅对如何批量安装包进行介绍,如下所示

  1. pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
  2. #-r后是需要安装的包的名称构成的txt文档
  3. #-i后国内的镜像地址,下载会快一点

3.放入相关视频至video文件夹

        注意:要求文件的分辨率一致,水印位置相同,否则无法进行批量操作。

4.启动脚本

        打开命令行,先是输入python,回车,再数据脚本名即可运行脚本。

 5.进行命令选择,然后框选去除区域即可

 

 6.查找处理好的视频

        在output视频之中。

二、实战总体代码

  1. import os
  2. import sys
  3. import cv2
  4. import numpy
  5. from moviepy import editor
  6. VIDEO_PATH = 'video'
  7. OUTPUT_PATH = 'output'
  8. TEMP_VIDEO = 'temp.mp4'
  9. class WatermarkRemover():
  10. def __init__(self, threshold: int, kernel_size: int):
  11. self.threshold = threshold # 阈值分割所用阈值
  12. self.kernel_size = kernel_size # 膨胀运算核尺寸
  13. #根据用户手动选择的ROI(Region of Interest,感兴趣区域)框选水印或字幕位置。
  14. def select_roi(self, img: numpy.ndarray, hint: str) -> list:
  15. '''
  16. 框选水印或字幕位置,SPACE或ENTER键退出
  17. :param img: 显示图片
  18. :return: 框选区域坐标
  19. '''
  20. COFF = 0.7
  21. w, h = int(COFF * img.shape[1]), int(COFF * img.shape[0])
  22. resize_img = cv2.resize(img, (w, h))
  23. roi = cv2.selectROI(hint, resize_img, False, False)
  24. cv2.destroyAllWindows()
  25. watermark_roi = [int(roi[0] / COFF), int(roi[1] / COFF), int(roi[2] / COFF), int(roi[3] / COFF)]
  26. return watermark_roi
  27. #对输入的蒙版进行膨胀运算,扩大蒙版的范围
  28. def dilate_mask(self, mask: numpy.ndarray) -> numpy.ndarray:
  29. '''
  30. 对蒙版进行膨胀运算
  31. :param mask: 蒙版图片
  32. :return: 膨胀处理后蒙版
  33. '''
  34. kernel = numpy.ones((self.kernel_size, self.kernel_size), numpy.uint8)
  35. mask = cv2.dilate(mask, kernel)
  36. return mask
  37. #根据手动选择的ROI区域,在单帧图像中生成水印或字幕的蒙版。
  38. def generate_single_mask(self, img: numpy.ndarray, roi: list, threshold: int) -> numpy.ndarray:
  39. '''
  40. 通过手动选择的ROI区域生成单帧图像的水印蒙版
  41. :param img: 单帧图像
  42. :param roi: 手动选择区域坐标
  43. :param threshold: 二值化阈值
  44. :return: 水印蒙版
  45. '''
  46. # 区域无效,程序退出
  47. if len(roi) != 4:
  48. print('NULL ROI!')
  49. sys.exit()
  50. # 复制单帧灰度图像ROI内像素点
  51. roi_img = numpy.zeros((img.shape[0], img.shape[1]), numpy.uint8)
  52. start_x, end_x = int(roi[1]), int(roi[1] + roi[3])
  53. start_y, end_y = int(roi[0]), int(roi[0] + roi[2])
  54. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  55. roi_img[start_x:end_x, start_y:end_y] = gray[start_x:end_x, start_y:end_y]
  56. # 阈值分割
  57. _, mask = cv2.threshold(roi_img, threshold, 255, cv2.THRESH_BINARY)
  58. return mask
  59. #通过截取视频中多帧图像生成多张水印蒙版,并通过逻辑与计算生成最终的水印蒙版
  60. def generate_watermark_mask(self, video_path: str) -> numpy.ndarray:
  61. '''
  62. 截取视频中多帧图像生成多张水印蒙版,通过逻辑与计算生成最终水印蒙版
  63. :param video_path: 视频文件路径
  64. :return: 水印蒙版
  65. '''
  66. video = cv2.VideoCapture(video_path)
  67. success, frame = video.read()
  68. roi = self.select_roi(frame, 'select watermark ROI')
  69. mask = numpy.ones((frame.shape[0], frame.shape[1]), numpy.uint8)
  70. mask.fill(255)
  71. step = video.get(cv2.CAP_PROP_FRAME_COUNT) // 5
  72. index = 0
  73. while success:
  74. if index % step == 0:
  75. mask = cv2.bitwise_and(mask, self.generate_single_mask(frame, roi, self.threshold))
  76. success, frame = video.read()
  77. index += 1
  78. video.release()
  79. return self.dilate_mask(mask)
  80. #根据手动选择的ROI区域,在单帧图像中生成字幕的蒙版。
  81. def generate_subtitle_mask(self, frame: numpy.ndarray, roi: list) -> numpy.ndarray:
  82. '''
  83. 通过手动选择ROI区域生成单帧图像字幕蒙版
  84. :param frame: 单帧图像
  85. :param roi: 手动选择区域坐标
  86. :return: 字幕蒙版
  87. '''
  88. mask = self.generate_single_mask(frame, [0, roi[1], frame.shape[1], roi[3]], self.threshold) # 仅使用ROI横坐标区域
  89. return self.dilate_mask(mask)
  90. def inpaint_image(self, img: numpy.ndarray, mask: numpy.ndarray) -> numpy.ndarray:
  91. '''
  92. 修复图像
  93. :param img: 单帧图像
  94. :parma mask: 蒙版
  95. :return: 修复后图像
  96. '''
  97. telea = cv2.inpaint(img, mask, 1, cv2.INPAINT_TELEA)
  98. return telea
  99. def merge_audio(self, input_path: str, output_path: str, temp_path: str):
  100. '''
  101. 合并音频与处理后视频
  102. :param input_path: 原视频文件路径
  103. :param output_path: 封装音视频后文件路径
  104. :param temp_path: 无声视频文件路径
  105. '''
  106. with editor.VideoFileClip(input_path) as video:
  107. audio = video.audio
  108. with editor.VideoFileClip(temp_path) as opencv_video:
  109. clip = opencv_video.set_audio(audio)
  110. clip.to_videofile(output_path)
  111. def remove_video_watermark(self):
  112. '''
  113. 去除视频水印
  114. '''
  115. if not os.path.exists(OUTPUT_PATH):
  116. os.makedirs(OUTPUT_PATH)
  117. filenames = [os.path.join(VIDEO_PATH, i) for i in os.listdir(VIDEO_PATH)]
  118. mask = None
  119. for i, name in enumerate(filenames):
  120. if i == 0:
  121. # 生成水印蒙版
  122. mask = self.generate_watermark_mask(name)
  123. # 创建待写入文件对象
  124. video = cv2.VideoCapture(name)
  125. fps = video.get(cv2.CAP_PROP_FPS)
  126. size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
  127. video_writer = cv2.VideoWriter(TEMP_VIDEO, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
  128. # 逐帧处理图像
  129. success, frame = video.read()
  130. while success:
  131. frame = self.inpaint_image(frame, mask)
  132. video_writer.write(frame)
  133. success, frame = video.read()
  134. video.release()
  135. video_writer.release()
  136. # 封装视频
  137. (_, filename) = os.path.split(name)
  138. output_path = os.path.join(OUTPUT_PATH, filename.split('.')[0] + '_no_watermark.mp4') # 输出文件路径
  139. self.merge_audio(name, output_path, TEMP_VIDEO)
  140. if os.path.exists(TEMP_VIDEO):
  141. os.remove(TEMP_VIDEO)
  142. def remove_video_subtitle(self):
  143. '''
  144. 去除视频字幕
  145. '''
  146. if not os.path.exists(OUTPUT_PATH):
  147. os.makedirs(OUTPUT_PATH)
  148. filenames = [os.path.join(VIDEO_PATH, i) for i in os.listdir(VIDEO_PATH)]
  149. roi = []
  150. for i, name in enumerate(filenames):
  151. # 创建待写入文件对象
  152. video = cv2.VideoCapture(name)
  153. fps = video.get(cv2.CAP_PROP_FPS)
  154. size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
  155. video_writer = cv2.VideoWriter(TEMP_VIDEO, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
  156. # 逐帧处理图像
  157. success, frame = video.read()
  158. if i == 0:
  159. roi = self.select_roi(frame, 'select subtitle ROI')
  160. while success:
  161. mask = self.generate_subtitle_mask(frame, roi)
  162. frame = self.inpaint_image(frame, mask)
  163. video_writer.write(frame)
  164. success, frame = video.read()
  165. video.release()
  166. video_writer.release()
  167. # 封装视频
  168. (_, filename) = os.path.split(name)
  169. output_path = os.path.join(OUTPUT_PATH, filename.split('.')[0] + '_no_sub.mp4') # 输出文件路径
  170. self.merge_audio(name, output_path, TEMP_VIDEO)
  171. if os.path.exists(TEMP_VIDEO):
  172. os.remove(TEMP_VIDEO)
  173. if __name__ == '__main__':
  174. sel=input('1:去水印, 2:去字幕\n')
  175. if sel=='1':
  176. remover = WatermarkRemover(threshold=80, kernel_size=5)
  177. remover.remove_video_watermark()
  178. if sel=='2':
  179. remover = WatermarkRemover(threshold=80, kernel_size=5)
  180. remover.remove_video_subtitle()

三、图片去除水印方法

(一)手机——乐奇爱水印精灵

        有点免费去除水印,可以无效其操作,但是每天只能保存一张,好就好在邀请一个人可以活得60此保存机会,那个被邀请的也能获得十次,可以P图。

(二)电脑——quququ.cn

        把图片拖到网站,调整画笔大小,抹除文字就可以p图完成,免费下载就可以了。

四、参考引用

[1]使用Python轻松去除视频水印/字幕,手把手教程,一学就会_哔哩哔哩_bilibili

[2]https://ztjun.fun/(B站:治廷君)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/254661
推荐阅读
相关标签
  

闽ICP备14008679号