diff --git a/backend/videoflix/content/signals.py b/backend/videoflix/content/signals.py index 337fd1f..aa710af 100644 --- a/backend/videoflix/content/signals.py +++ b/backend/videoflix/content/signals.py @@ -1,8 +1,7 @@ from .models import Video +from content.tasks import convert_video, create_thumbnail from django.dispatch import receiver -from django.conf import settings from django.db.models.signals import post_save, post_delete -import ffmpeg import os @receiver(post_save, sender=Video) @@ -11,24 +10,13 @@ def video_post_save(sender, instance, created, **kwargs): Generates a thumbnail for a newly created `Video` instance. """ if created: - video_file_path = instance.video_file.path - thumbnail_dir = settings.THUMBNAIL_DIR - - thumbnail_filename = os.path.splitext(os.path.basename(video_file_path))[0] + '.jpg' - thumbnail_path = os.path.join(thumbnail_dir, thumbnail_filename) - absolute_thumbnail_path = os.path.abspath(thumbnail_path) - - if not os.path.exists(thumbnail_dir): - os.makedirs(thumbnail_dir) - - try: - ffmpeg.input(video_file_path, ss=1).output(absolute_thumbnail_path, vf='scale=300:-1', vframes=1).run(overwrite_output=True) - instance.thumbnail = os.path.relpath(thumbnail_path, start=settings.MEDIA_ROOT) - instance.save() - - print("New video and thumbnail generated") - except ffmpeg._run.Error as e: - print(f"An error occurred: {e.stderr.decode()}") + #Convert Video + convert_video(instance.video_file.path, "480") + convert_video(instance.video_file.path, "720") + convert_video(instance.video_file.path, "1080") + + #Create Thumbnail + create_thumbnail(instance) @receiver(post_delete, sender=Video) def auto_delete_file_on_delete(sender, instance, **kwargs): diff --git a/backend/videoflix/content/tasks.py b/backend/videoflix/content/tasks.py new file mode 100644 index 0000000..45e2c47 --- /dev/null +++ b/backend/videoflix/content/tasks.py @@ -0,0 +1,36 @@ +import subprocess +import ffmpeg +import os +from django.conf import settings + +def convert_video(source, resolution): + target = source + f'_{resolution}p.mp4' + target = remove_first_mp4(target) + ffmpeg_path = '/opt/homebrew/bin/ffmpeg' + cmd = [ffmpeg_path, '-i', source, '-s', f'hd{resolution}', '-c:v', 'libx264', '-crf', '23', '-c:a', 'aac', '-strict', '-2', target] + result = subprocess.run(cmd, capture_output=True, text=True) + +def remove_first_mp4(filename): + name_part, ext_part = filename.split('.mp4', 1) + new_filename = name_part + ext_part + return new_filename + +def create_thumbnail(source): + video_file_path = source.video_file.path + thumbnail_dir = settings.THUMBNAIL_DIR + + thumbnail_filename = os.path.splitext(os.path.basename(video_file_path))[0] + '.jpg' + thumbnail_path = os.path.join(thumbnail_dir, thumbnail_filename) + absolute_thumbnail_path = os.path.abspath(thumbnail_path) + + if not os.path.exists(thumbnail_dir): + os.makedirs(thumbnail_dir) + + try: + ffmpeg.input(video_file_path, ss=1).output(absolute_thumbnail_path, vf='scale=300:-1', vframes=1).run(overwrite_output=True) + source.thumbnail = os.path.relpath(thumbnail_path, start=settings.MEDIA_ROOT) + source.save() + + print("New video and thumbnail generated") + except ffmpeg._run.Error as e: + print(f"An error occurred: {e.stderr.decode()}") \ No newline at end of file