0

Im using django-video-encoding this and django-rq to generate a thumbnail but instead it generates another video AND it makes it smaller(in size)

here is signals.py:

`
from django.db.models.signals import post_save
from django.dispatch import receiver
from typing import Type
from . import tasks , signals
from .models import Post
from django_rq import enqueue


@receiver(post_save, sender=Post)
def create_thumbnail(sender, instance, **kwargs):
    enqueue(tasks.create_thumbnail, instance.pk)

here is tasks.py:

`
from django.core.files import File
from video_encoding.backends import get_backend
import os
from .models import Post


def create_thumbnail(post_pk):
   video = Post.objects.get(pk=post_pk)
   if not video.video:
      print('no video file attached')
      # no video file attached


   if video.thumbnail:
        print('thumbnail has already been generated')
      # thumbnail has already been generated


   encoding_backend = get_backend()
   thumbnail_path = encoding_backend.get_thumbnail(video.video.path)
   path = video.video.url
   filename = os.path.basename(path),

   try:
      with open(thumbnail_path, 'rb') as file_handler:
         django_file = File(file_handler)
         video.thumbnail.save(filename[0], django_file)
      video.save()
   finally:
      os.unlink(thumbnail_path)`

and models.py:

`from django.db import models
from django.contrib.auth.models import User
from django.core.validators import FileExtensionValidator
from django.contrib.contenttypes.fields import GenericRelation
from video_encoding.fields import VideoField, ImageField
from video_encoding.models import Format


class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    title = models.CharField(max_length=500)
    description = models.TextField()
    format_set = GenericRelation(Format)
    video = VideoField(upload_to='uploads/video_files/%y/%d', validators =  [FileExtensionValidator(allowed_extensions=['mp4'])])
    thumbnail = ImageField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self) -> str:
        return self.title + "\n" + self.description`

I want just to generate a thumbnail at this point, maybe encode videos later, but i honesty have no idea what im doing, i just followed the tutorial from github.

Here are some more detailed photos from the dajngo-rq:

pic - rq work log

pic1 - post in admin panel

pic2 - django-rq

I dont understand why it encodes videos when there is no encode task, from the examples ive seen on github there should be a 'convert_video' in signals.py but in mine is none

MrLonely
  • 23
  • 1
  • 5

0 Answers0