File size: 3,211 Bytes
7804245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import cv2
import numpy as np
import random
import tempfile
import os
from moviepy.video.io.VideoFileClip import VideoFileClip  # Updated import path

def add_and_detect_watermark_video(video_path, watermark_text, num_watermarks=5):
    def add_watermark_to_frame(frame):
        watermark_positions = []

        # Resize frame to be divisible by 8 (required for DCT)
        h, w, _ = frame.shape
        h_new = (h // 8) * 8
        w_new = (w // 8) * 8
        frame_resized = cv2.resize(frame, (w_new, h_new))
        
        # Convert to YCrCb color space and extract Y channel
        ycrcb_image = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2YCrCb)
        y_channel, cr_channel, cb_channel = cv2.split(ycrcb_image)
        
        # Apply DCT to the Y channel
        dct_y = cv2.dct(np.float32(y_channel))
        
        # Add watermark in the DCT domain
        rows, cols = dct_y.shape
        font = cv2.FONT_HERSHEY_SIMPLEX
        for _ in range(num_watermarks):
            text_size = cv2.getTextSize(watermark_text, font, 0.5, 1)[0]
            text_x = random.randint(0, cols - text_size[0])
            text_y = random.randint(text_size[1], rows)
            watermark = np.zeros_like(dct_y)
            watermark = cv2.putText(watermark, watermark_text, (text_x, text_y), font, 0.5, (1, 1, 1), 1, cv2.LINE_AA)
            dct_y += watermark * 0.01
            watermark_positions.append((text_x, text_y, text_size[0], text_size[1]))
        
        # Apply inverse DCT
        idct_y = cv2.idct(dct_y)
        
        # Merge channels and convert back to BGR
        ycrcb_image[:, :, 0] = idct_y
        watermarked_frame = cv2.cvtColor(ycrcb_image, cv2.COLOR_YCrCb2BGR)
        
        # Highlight watermarks for visualization
        watermark_highlight = watermarked_frame.copy()
        for (text_x, text_y, text_w, text_h) in watermark_positions:
            cv2.putText(watermark_highlight, watermark_text, (text_x, text_y), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
            cv2.rectangle(watermark_highlight, (text_x, text_y - text_h), (text_x + text_w, text_y), (0, 0, 255), 2)
        
        return watermarked_frame, watermark_highlight

    try:
        # Load video using MoviePy
        video = VideoFileClip(video_path)
        
        # Apply watermark to each frame
        video_with_watermark = video.fl_image(lambda frame: add_watermark_to_frame(frame)[0])
        video_with_highlight = video.fl_image(lambda frame: add_watermark_to_frame(frame)[1])

        # Create temporary files for output videos
        temp_fd, watermarked_video_path = tempfile.mkstemp(suffix=".mp4")
        temp_fd_highlight, highlight_video_path = tempfile.mkstemp(suffix=".mp4")
        os.close(temp_fd)
        os.close(temp_fd_highlight)
        
        # Write output videos
        video_with_watermark.write_videofile(watermarked_video_path, codec='libx264')
        video_with_highlight.write_videofile(highlight_video_path, codec='libx264')

        return watermarked_video_path, highlight_video_path

    except Exception as e:
        print(f"An error occurred: {e}")
        return None, None