Skip to content

Commit 9b70d86

Browse files
committed
all files added
1 parent 0c87b0b commit 9b70d86

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

Python/Video_Watermarker/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# VSCODE
2+
.vscode
3+
4+
# Video files
5+
sample.mp4
6+
output.mp4

Python/Video_Watermarker/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Video Watermarker
2+
3+
This script watermarks the entire video given the watermark text, it's position and margin. First the frames of the video are converted into images, then text is drawn and then saved in a list of images. After this entire process, the watermarked images are combined back to video and then audio is added to the final output. The residual files created during the process are deleted when they are of no use.
4+
5+
# How to use?
6+
7+
Just type:
8+
9+
`python app.py video-location watermark_location margin watermark_text `
10+
11+
video_location: location of the video
12+
13+
watermark_location:
14+
- RT (Right Top)
15+
- RB (Right Bottom)
16+
- LB (Left Bottom)
17+
- LT (Left Top)
18+
- C (Center)
19+
20+
Example:
21+
22+
`python app.py sample.mp4 RT 5 Kaustubh Gupta`
23+
24+
# Requirements
25+
26+
Before running the script, just run the command
27+
28+
`pip install -r requirements.txt`
29+
30+
and all dependices will be installed
31+
32+
# Sample
33+
34+
![](preview.png)

Python/Video_Watermarker/app.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from PIL import Image, ImageDraw, ImageFont
2+
from cv2 import cv2
3+
from tqdm import tqdm
4+
import moviepy.editor as mpe
5+
import sys
6+
import os
7+
8+
9+
def capture(path, choice, margin, text):
10+
video = cv2.VideoCapture(path) # video object
11+
countFrame = 0
12+
img_array = [] # store all the watermaked frames
13+
progress_bar = tqdm(unit=' Frames Processed', unit_scale=True)
14+
while(video.isOpened()):
15+
sucess, frame = video.read()
16+
if sucess == False:
17+
break
18+
cv2.imwrite('frame'+str(countFrame)+'.jpg',frame)
19+
image = Image.open('frame'+str(countFrame)+'.jpg')
20+
width, height = image.size
21+
size = (width, height)
22+
draw = ImageDraw.Draw(image) # drawing object
23+
font = ImageFont.truetype('arial.ttf', 36)
24+
textwidth, textheight = draw.textsize(text, font)
25+
if choice == 'C':
26+
x = (width - textwidth)/2
27+
y = (height - textheight)/2
28+
elif choice == 'RB':
29+
x = width - textwidth - margin
30+
y = height - textheight - margin
31+
elif choice == 'LT':
32+
x = margin
33+
y = margin
34+
elif choice == 'LB':
35+
x = margin
36+
y = height - textheight - margin
37+
elif choice == 'RT':
38+
x = width - textwidth - margin
39+
y = margin
40+
else:
41+
print("Invalid choice!")
42+
break
43+
44+
draw.text((x, y), text, font=font)
45+
image.save('frame converted'+str(countFrame)+'.jpg')
46+
img = cv2.imread('frame converted'+str(countFrame)+'.jpg')
47+
img_array.append(img)
48+
os.remove('frame'+str(countFrame)+'.jpg')
49+
os.remove('frame converted'+str(countFrame)+'.jpg')
50+
progress_bar.update()
51+
countFrame += 1
52+
53+
video.release()
54+
video_write = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc(*'DIVX'), 30, size)
55+
for i in range(len(img_array)):
56+
video_write.write(img_array[i])
57+
video_write.release()
58+
59+
my_clip = mpe.VideoFileClip('output.avi')
60+
audio_background = mpe.VideoFileClip(path).audio
61+
final_clip = my_clip.set_audio(audio_background)
62+
final_clip.write_videofile("output.mp4")
63+
64+
os.remove('output.avi')
65+
66+
if __name__ == "__main__":
67+
capture(sys.argv[1], sys.argv[2], int(sys.argv[3]), " ".join(sys.argv[4:]))

Python/Video_Watermarker/preview.PNG

1.29 MB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
opencv-python==4.4.0.42
2+
tqdm==4.48.2
3+
moviepy==1.0.3
4+
Pillow==7.2.0
5+
numpy==1.19.1

0 commit comments

Comments
 (0)