code outsourced to tasks.py, videos are converted during upload
This commit is contained in:
parent
19a4de1e2d
commit
b91dfc5223
2 changed files with 44 additions and 20 deletions
|
|
@ -1,8 +1,7 @@
|
||||||
from .models import Video
|
from .models import Video
|
||||||
|
from content.tasks import convert_video, create_thumbnail
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.conf import settings
|
|
||||||
from django.db.models.signals import post_save, post_delete
|
from django.db.models.signals import post_save, post_delete
|
||||||
import ffmpeg
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@receiver(post_save, sender=Video)
|
@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.
|
Generates a thumbnail for a newly created `Video` instance.
|
||||||
"""
|
"""
|
||||||
if created:
|
if created:
|
||||||
video_file_path = instance.video_file.path
|
#Convert Video
|
||||||
thumbnail_dir = settings.THUMBNAIL_DIR
|
convert_video(instance.video_file.path, "480")
|
||||||
|
convert_video(instance.video_file.path, "720")
|
||||||
thumbnail_filename = os.path.splitext(os.path.basename(video_file_path))[0] + '.jpg'
|
convert_video(instance.video_file.path, "1080")
|
||||||
thumbnail_path = os.path.join(thumbnail_dir, thumbnail_filename)
|
|
||||||
absolute_thumbnail_path = os.path.abspath(thumbnail_path)
|
#Create Thumbnail
|
||||||
|
create_thumbnail(instance)
|
||||||
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()}")
|
|
||||||
|
|
||||||
@receiver(post_delete, sender=Video)
|
@receiver(post_delete, sender=Video)
|
||||||
def auto_delete_file_on_delete(sender, instance, **kwargs):
|
def auto_delete_file_on_delete(sender, instance, **kwargs):
|
||||||
|
|
|
||||||
36
backend/videoflix/content/tasks.py
Normal file
36
backend/videoflix/content/tasks.py
Normal file
|
|
@ -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()}")
|
||||||
Loading…
Reference in a new issue