Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add watermark Feature for the project #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions core/video_processing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
"""
This module contains functions for processing videos.

Functions:
resize_video(video_path, output_video_path, width, height):
"""

from moviepy.editor import VideoFileClip
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip

def resize_video(video_path, output_video_path, width, height):
"""
Expand All @@ -26,3 +19,50 @@ def resize_video(video_path, output_video_path, width, height):
resized_video.close()
video.close()
return output_video_path

def add_watermark(video_path, watermark_text, output_video_path, position=('right', 'bottom')):
"""
Adds a watermark to the input video at the specified position.

Parameters:
video_path (str): Path to the original video file.
watermark_text (str): Text to be used as the watermark.
output_video_path (str): Path to save the watermarked video.
position (tuple): Position of the watermark ('left', 'right', 'top', 'bottom').

Returns:
str: Path to the watermarked video.
"""
video = VideoFileClip(video_path)

watermark = TextClip(
watermark_text,
fontsize=24,
color='white',
font='Arial',
stroke_color='black',
stroke_width=1,
method='caption'
).set_duration(video.duration).set_opacity(0.6)


if position == ('right', 'bottom'):
watermark = watermark.set_position(("right", "bottom"))
elif position == ('left', 'bottom'):
watermark = watermark.set_position(("left", "bottom"))
elif position == ('right', 'top'):
watermark = watermark.set_position(("right", "top"))
elif position == ('left', 'top'):
watermark = watermark.set_position(("left", "top"))
else:
watermark = watermark.set_position(("center", "center"))

watermarked_video = CompositeVideoClip([video, watermark])


watermarked_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac')


video.close()
watermarked_video.close()
return output_video_path
52 changes: 32 additions & 20 deletions tests/test_video_processing.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
'''
This file contains the unit tests for the video_processing module.
'''

import unittest
import os
from core.video_processing import resize_video
from core.video_processing import resize_video, add_watermark

class TestVideoProcessing(unittest.TestCase):
'''Test cases for the video processing functions.'''
'''Test cases for the video processing functions.'''


def setUp(self):
self.video_path = os.path.abspath("tests/files/test_video.mp4")
self.output_video_path = os.path.abspath("tests/files/resized_test_video.mp4")
self.watermarked_video_path = os.path.abspath("tests/files/watermarked_test_video.mp4")
self.width = 640
self.height = 360
self.watermark_text = "KieuHoaB2111982"


def test_resize_video(self):
'''Test the resize_video function.'''
result_path = resize_video(self.video_path, self.output_video_path, self.width, self.height)
self.assertEqual(result_path, self.output_video_path)
self.assertTrue(os.path.exists(result_path))


def setUp(self):
self.video_path = "tests/files/test_video.mp4"
self.output_video_path = "tests/files/resized_test_video.mp4"
self.width = 640
self.height = 360
def test_add_watermark(self):
'''Test the add_watermark function.'''
# Thêm watermark vào video đã resize
result_path = add_watermark(self.output_video_path, self.watermark_text, self.watermarked_video_path)
self.assertEqual(result_path, self.watermarked_video_path)
self.assertTrue(os.path.exists(result_path))

def test_resize_video(self):
'''Test the resize_video function.'''
result_path = resize_video(self.video_path, self.output_video_path, self.width, self.height)
self.assertEqual(result_path, self.output_video_path)
self.assertTrue(os.path.exists(result_path))

def tearDown(self):
if os.path.exists(self.output_video_path):
os.remove(self.output_video_path)
# def tearDown(self):
# '''Log output file status but do not delete it.'''
# if os.path.exists(self.output_video_path):
# print(f"[INFO] Output video '{self.output_video_path}' đã được tạo mới.")
# if os.path.exists(self.watermarked_video_path):
# print(f"[INFO] Watermarked video '{self.watermarked_video_path}' đã được tạo mới.")

if __name__ == "__main__":
unittest.main()
unittest.main()
Loading