jack2049 commited on
Commit
196d316
·
verified ·
1 Parent(s): 5eb6a6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from PyQt5.QtWidgets import (
3
+ QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QFileDialog, QMessageBox
4
+ )
5
+ from moviepy.editor import VideoFileClip
6
+ import cv2
7
+ import numpy as np
8
+ import os
9
+
10
+ def remove_watermark_from_frame(frame, region):
11
+ # region: (x, y, w, h)
12
+ x, y, w, h = region
13
+ roi = frame[y:y+h, x:x+w]
14
+ # 使用inpaint方法去除水印
15
+ mask = np.ones(roi.shape[:2], np.uint8) * 255
16
+ inpainted = cv2.inpaint(roi, mask, 3, cv2.INPAINT_TELEA)
17
+ frame[y:y+h, x:x+w] = inpainted
18
+ return frame
19
+
20
+ class WatermarkRemover(QWidget):
21
+ def __init__(self):
22
+ super().__init__()
23
+ self.initUI()
24
+ self.video_path = None
25
+
26
+ def initUI(self):
27
+ self.setWindowTitle('视频2水印去除工具')
28
+ self.layout = QVBoxLayout()
29
+
30
+ self.label = QLabel('请选择一个视频文件')
31
+ self.layout.addWidget(self.label)
32
+
33
+ self.btn_open = QPushButton('打开视频')
34
+ self.btn_open.clicked.connect(self.open_video)
35
+ self.layout.addWidget(self.btn_open)
36
+
37
+ self.btn_remove = QPushButton('去除水印')
38
+ self.btn_remove.setEnabled(False)
39
+ self.btn_remove.clicked.connect(self.process_video)
40
+ self.layout.addWidget(self.btn_remove)
41
+
42
+ self.setLayout(self.layout)
43
+
44
+ def open_video(self):
45
+ path, _ = QFileDialog.getOpenFileName(self, '选择视频文件', '', 'Video Files (*.mp4 *.avi *.mov)')
46
+ if path:
47
+ self.video_path = path
48
+ self.label.setText(f'已选择: {os.path.basename(path)}')
49
+ self.btn_remove.setEnabled(True)
50
+
51
+ def process_video(self):
52
+ if not self.video_path:
53
+ return
54
+
55
+ # 这里假设水印在右下角,区域为宽高均50像素(可根据实际情况调整)
56
+ region = self.get_watermark_region(self.video_path)
57
+ output_path = self.video_path.rsplit('.', 1)[0] + '_no_watermark.mp4'
58
+
59
+ clip = VideoFileClip(self.video_path)
60
+ w, h = clip.size
61
+
62
+ def process_frame(frame):
63
+ # frame: RGB, uint8
64
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
65
+ result_bgr = remove_watermark_from_frame(frame_bgr, region)
66
+ return cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB)
67
+
68
+ new_clip = clip.fl_image(process_frame)
69
+ new_clip.write_videofile(output_path, audio=True, threads=4, preset='ultrafast')
70
+ QMessageBox.information(self, '完成', f'处理完成,文件已保存为:\n{output_path}')
71
+
72
+ def get_watermark_region(self, video_path):
73
+ # 可根据实际水印位置调整
74
+ # 这里只是一个例子:右下角50x50
75
+ clip = VideoFileClip(video_path)
76
+ w, h = clip.size
77
+ region_w, region_h = 50, 50
78
+ x = w - region_w - 10 # 离右边10像素
79
+ y = h - region_h - 10 # 离下边10像素
80
+ return (x, y, region_w, region_h)
81
+
82
+ if __name__ == '__main__':
83
+ app = QApplication(sys.argv)
84
+ remover = WatermarkRemover()
85
+ remover.show()
86
+ sys.exit(app.exec_())