diff --git a/core/video_processing.py b/core/video_processing.py index 1344a78..f04870d 100644 --- a/core/video_processing.py +++ b/core/video_processing.py @@ -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): """ @@ -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 diff --git a/tests/test_video_processing.py b/tests/test_video_processing.py index 8945e61..e35fd4e 100644 --- a/tests/test_video_processing.py +++ b/tests/test_video_processing.py @@ -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()