Commit 24aa2057 authored by Ilya Simonov's avatar Ilya Simonov

fix generate custom thumbnail

parent be56892f
......@@ -4,6 +4,3 @@ from django.apps import AppConfig
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.core'
def ready(self):
import apps.core.signals
import os
from django.conf import settings
from django.dispatch import receiver
from django.db.models.signals import post_save
from . import models, utils, s3_uploader
# @receiver(post_save, sender=models.Video)
# def generate_default_thumbnail_for_video(sender, instance, **kwargs):
# if not instance.thumbnail and instance.local_file:
# if os.path.isfile(instance.local_file.path):
# thumbnail_name = utils.generate_thumbnail(instance.local_file)
# key = f'videos/{thumbnail_name}'
# thumbnail_path = f'{settings.MEDIA_ROOT}{key}'
#
# content = utils.get_thumbnail_content(thumbnail_path)
# content_type = 'image/jpeg'
# instance.thumbnail = s3_uploader.upload_file(content, key, content_type)
#
# instance.save(update_fields=['thumbnail'])
#
# os.remove(thumbnail_path)
import os
import requests
from django.conf import settings
from .models import Video, READY
from .s3_uploader import upload_file
from .utils import asset_upload
from .utils import asset_upload, generate_thumbnail
from cp_video.celery import app
......@@ -25,6 +24,15 @@ def send_video_to_s3():
local_video.s3_file = upload_file(content, key, 'video/mp4')
local_video.status = READY
local_video.save(update_fields=['s3_file', 'status'])
if not local_video.thumbnail and local_video.local_file:
thumbnail_name = generate_thumbnail(file_path, video_name)
key = f'videos/{thumbnail_name}'
thumbnail_path = settings.DOMAIN_NAME + settings.MEDIA_URL + thumbnail_name
response = requests.get(thumbnail_path)
content = response.content
content_type = 'image/jpeg'
local_video.thumbnail = upload_file(content, key, content_type)
local_video.save(update_fields=['s3_file', 'status', 'thumbnail'])
local_videos.update(local_file=None)
......@@ -10,24 +10,12 @@ def asset_upload(instance, filename):
return f'videos/{filename}'
def generate_thumbnail(video):
clip = VideoFileClip(video.path)
thumbnail_name = video.name.split('.')[0]
def generate_thumbnail(video_path, video_name):
clip = VideoFileClip(video_path)
thumbnail_name = video_name.split('.')[0]
thumbnail_name = f'{settings.MEDIA_ROOT}{thumbnail_name}.jpg'
clip.save_frame(thumbnail_name, t=1.00)
thumbnail_name = thumbnail_name.split('/')[-1]
return thumbnail_name
def get_thumbnail_content(thumbnail_path):
img = PImage.open(thumbnail_path)
roi_img = img.crop()
img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format='JPEG')
content = img_byte_arr.getvalue()
return content
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment