feat: ensure video entry is only visible after thumbnail creation and processing ("is_available")
This commit is contained in:
parent
91dc969c6d
commit
3fa00c0aa9
5 changed files with 12 additions and 16 deletions
|
|
@ -2,7 +2,6 @@ from django.db import models
|
|||
from django.conf import settings
|
||||
from datetime import date
|
||||
from .class_assets import VIDEO_GENRES
|
||||
import uuid
|
||||
|
||||
class Video(models.Model):
|
||||
creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default=1)
|
||||
|
|
@ -12,6 +11,7 @@ class Video(models.Model):
|
|||
genre = models.CharField(max_length=20, choices=VIDEO_GENRES, blank=True, null=True)
|
||||
file_path = models.FileField(upload_to='videos/', blank=True, null=True)
|
||||
file_name = models.CharField(max_length=50, blank=True, null=True)
|
||||
is_available = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.id}) {self.title}'
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
import os
|
||||
import uuid
|
||||
import os, uuid
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.conf import settings
|
||||
from .models import Video
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ from .models import Video
|
|||
from .tasks import convert_video_to_hls, create_thumbnails, delete_original_video
|
||||
from django.dispatch import receiver
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
import os
|
||||
import django_rq
|
||||
import shutil
|
||||
import os, shutil, django_rq
|
||||
|
||||
@receiver(post_save, sender=Video)
|
||||
def video_post_save(sender, instance, created, **kwargs):
|
||||
|
|
@ -14,12 +12,16 @@ def video_post_save(sender, instance, created, **kwargs):
|
|||
- Convert video to different resolutions (HLS)
|
||||
- Delete original video
|
||||
"""
|
||||
if not created:
|
||||
if not created and instance.is_available:
|
||||
return
|
||||
|
||||
if instance.file_path and instance.file_name:
|
||||
try:
|
||||
create_thumbnails(instance, instance.id)
|
||||
|
||||
if not instance.is_available:
|
||||
instance.is_available = True
|
||||
instance.save(update_fields=['is_available'])
|
||||
|
||||
queue = django_rq.get_queue("default", autocommit=True)
|
||||
resolutions = ["1280x720", "640x360", "1920x1080"]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
import shutil
|
||||
import subprocess
|
||||
import ffmpeg
|
||||
import os
|
||||
import shutil, subprocess, ffmpeg, os
|
||||
from django.conf import settings
|
||||
|
||||
def convert_video_to_hls(source, resolution, model_id):
|
||||
|
|
@ -59,11 +56,9 @@ def create_thumbnails(instance, model_id):
|
|||
os.makedirs(thumbnail_dir)
|
||||
|
||||
try:
|
||||
# Generate the image thumbnails
|
||||
ffmpeg.input(video_file_path, ss=1).output(thumbnail_1080p_path, vf='scale=1920:-1', vframes=1).run(overwrite_output=True)
|
||||
ffmpeg.input(video_file_path, ss=1).output(thumbnail_480_path, vf='scale=720:-1', vframes=1).run(overwrite_output=True)
|
||||
|
||||
# Create the 10-second video thumbnail
|
||||
create_video_thumbnail(video_file_path, thumbnail_dir, base_filename)
|
||||
|
||||
except ffmpeg._run.Error as e:
|
||||
|
|
@ -82,7 +77,7 @@ def create_video_thumbnail(video_file_path, thumbnail_dir, base_filename):
|
|||
video_thumbnail_path,
|
||||
vf='scale=1280:-1',
|
||||
vcodec='libx264',
|
||||
an=None # Disable audio
|
||||
an=None
|
||||
).run(overwrite_output=True)
|
||||
|
||||
except ffmpeg._run.Error as e:
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ import os
|
|||
@permission_classes([IsAuthenticated])
|
||||
def video_list(request):
|
||||
"""
|
||||
List all videos
|
||||
List all videos with is_available=True
|
||||
"""
|
||||
videos = Video.objects.all()
|
||||
videos = Video.objects.filter(is_available=True)
|
||||
serializer = VideoSerializer(videos, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue