|
|
@ -193,21 +193,48 @@ class Video(LowercaseSlugMixin, models.Model): |
|
|
|
|
|
|
|
|
super().save(*args, **kwargs) |
|
|
super().save(*args, **kwargs) |
|
|
|
|
|
|
|
|
if (file_changed or self.video_time == datetime.time(0, 0, 0)) and self.video_file and self.video_type == self.VedioTypeChoices.VIDEO_FILE: |
|
|
|
|
|
from utils.media import get_media_duration_time |
|
|
|
|
|
try: |
|
|
|
|
|
duration_time = get_media_duration_time(self.video_file.path) |
|
|
|
|
|
if duration_time: |
|
|
|
|
|
self.video_time = duration_time |
|
|
|
|
|
Video.objects.filter(pk=self.pk).update(video_time=duration_time) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.video_type == self.VedioTypeChoices.VIDEO_FILE and self.video_file: |
|
|
|
|
|
from utils.media import get_media_duration_time, extract_video_frame_bytes |
|
|
|
|
|
from django.core.files.base import ContentFile |
|
|
|
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
fields_to_update = [] |
|
|
|
|
|
|
|
|
|
|
|
if (file_changed or self.video_time == datetime.time(0, 0, 0)): |
|
|
|
|
|
try: |
|
|
|
|
|
duration_time = get_media_duration_time(self.video_file.path) |
|
|
|
|
|
if duration_time: |
|
|
|
|
|
self.video_time = duration_time |
|
|
|
|
|
fields_to_update.append('video_time') |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
import logging |
|
|
|
|
|
logging.getLogger(__name__).error(f"Failed to calculate video duration: {e}") |
|
|
|
|
|
|
|
|
|
|
|
if not self.thumbnail: |
|
|
|
|
|
try: |
|
|
|
|
|
thumb_bytes = extract_video_frame_bytes(self.video_file.path, timestamp_sec=1.0) |
|
|
|
|
|
if thumb_bytes: |
|
|
|
|
|
base_name = os.path.splitext(os.path.basename(self.video_file.name))[0] |
|
|
|
|
|
filename = f"thumb_{base_name}_{self.pk}.jpg" |
|
|
|
|
|
self.thumbnail.save(filename, ContentFile(thumb_bytes), save=False) |
|
|
|
|
|
fields_to_update.append('thumbnail') |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
import logging |
|
|
|
|
|
logging.getLogger(__name__).error(f"Failed to extract video thumbnail: {e}") |
|
|
|
|
|
|
|
|
|
|
|
if fields_to_update: |
|
|
|
|
|
update_dict = {field: getattr(self, field) for field in fields_to_update} |
|
|
|
|
|
Video.objects.filter(pk=self.pk).update(**update_dict) |
|
|
|
|
|
|
|
|
|
|
|
if 'video_time' in fields_to_update: |
|
|
|
|
|
try: |
|
|
# Update playlists that contain this video |
|
|
# Update playlists that contain this video |
|
|
for appearance in self.playlist_appearances.all(): |
|
|
for appearance in self.playlist_appearances.all(): |
|
|
playlist = appearance.playlist |
|
|
playlist = appearance.playlist |
|
|
playlist.total_time = playlist.calculate_total_time() |
|
|
playlist.total_time = playlist.calculate_total_time() |
|
|
playlist.save(update_fields=['total_time']) |
|
|
playlist.save(update_fields=['total_time']) |
|
|
except Exception as e: |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
if self.video_type == self.VedioTypeChoices.YOUTUBE_LINK: |
|
|
if self.video_type == self.VedioTypeChoices.YOUTUBE_LINK: |
|
|
try: |
|
|
try: |
|
|
|