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 django.conf import settings
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from .class_assets import VIDEO_GENRES
|
from .class_assets import VIDEO_GENRES
|
||||||
import uuid
|
|
||||||
|
|
||||||
class Video(models.Model):
|
class Video(models.Model):
|
||||||
creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default=1)
|
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)
|
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_path = models.FileField(upload_to='videos/', blank=True, null=True)
|
||||||
file_name = models.CharField(max_length=50, blank=True, null=True)
|
file_name = models.CharField(max_length=50, blank=True, null=True)
|
||||||
|
is_available = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'({self.id}) {self.title}'
|
return f'({self.id}) {self.title}'
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os, uuid
|
||||||
import uuid
|
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from .models import Video
|
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 .tasks import convert_video_to_hls, create_thumbnails, delete_original_video
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.db.models.signals import post_save, post_delete
|
from django.db.models.signals import post_save, post_delete
|
||||||
import os
|
import os, shutil, django_rq
|
||||||
import django_rq
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Video)
|
@receiver(post_save, sender=Video)
|
||||||
def video_post_save(sender, instance, created, **kwargs):
|
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)
|
- Convert video to different resolutions (HLS)
|
||||||
- Delete original video
|
- Delete original video
|
||||||
"""
|
"""
|
||||||
if not created:
|
if not created and instance.is_available:
|
||||||
return
|
return
|
||||||
|
|
||||||
if instance.file_path and instance.file_name:
|
if instance.file_path and instance.file_name:
|
||||||
try:
|
try:
|
||||||
create_thumbnails(instance, instance.id)
|
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)
|
queue = django_rq.get_queue("default", autocommit=True)
|
||||||
resolutions = ["1280x720", "640x360", "1920x1080"]
|
resolutions = ["1280x720", "640x360", "1920x1080"]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
import shutil
|
import shutil, subprocess, ffmpeg, os
|
||||||
import subprocess
|
|
||||||
import ffmpeg
|
|
||||||
import os
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
def convert_video_to_hls(source, resolution, model_id):
|
def convert_video_to_hls(source, resolution, model_id):
|
||||||
|
|
@ -59,11 +56,9 @@ def create_thumbnails(instance, model_id):
|
||||||
os.makedirs(thumbnail_dir)
|
os.makedirs(thumbnail_dir)
|
||||||
|
|
||||||
try:
|
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_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)
|
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)
|
create_video_thumbnail(video_file_path, thumbnail_dir, base_filename)
|
||||||
|
|
||||||
except ffmpeg._run.Error as e:
|
except ffmpeg._run.Error as e:
|
||||||
|
|
@ -82,7 +77,7 @@ def create_video_thumbnail(video_file_path, thumbnail_dir, base_filename):
|
||||||
video_thumbnail_path,
|
video_thumbnail_path,
|
||||||
vf='scale=1280:-1',
|
vf='scale=1280:-1',
|
||||||
vcodec='libx264',
|
vcodec='libx264',
|
||||||
an=None # Disable audio
|
an=None
|
||||||
).run(overwrite_output=True)
|
).run(overwrite_output=True)
|
||||||
|
|
||||||
except ffmpeg._run.Error as e:
|
except ffmpeg._run.Error as e:
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ import os
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def video_list(request):
|
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)
|
serializer = VideoSerializer(videos, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue