diff --git a/files/models/video_data.py b/files/models/video_data.py index 489244f6..8ce41208 100644 --- a/files/models/video_data.py +++ b/files/models/video_data.py @@ -12,40 +12,14 @@ class VideoChapterData(models.Model): class Meta: unique_together = ['media'] - def save(self, *args, **kwargs): - from .. import tasks - - is_new = self.pk is None - if is_new or (not is_new and self._check_data_changed()): - super().save(*args, **kwargs) - tasks.produce_video_chapters.delay(self.pk) - else: - super().save(*args, **kwargs) - - def _check_data_changed(self): - if self.pk: - old_instance = VideoChapterData.objects.get(pk=self.pk) - return old_instance.data != self.data - return False - @property def chapter_data(self): # ensure response is consistent data = [] - for item in self.data: - if item.get("start") and item.get("title"): - thumbnail = item.get("thumbnail") - if thumbnail: - thumbnail = helpers.url_from_path(thumbnail) - else: - thumbnail = "static/images/chapter_default.jpg" - data.append( - { - "start": item.get("start"), - "title": item.get("title"), - "thumbnail": thumbnail, - } - ) + if self.data and isinstance(self.data, list): + for item in self.data: + if item.get("startTime") and item.get("endTime") and item.get("text"): + data.append(item) return data diff --git a/files/tasks.py b/files/tasks.py index 5d6eca12..ee8b1933 100644 --- a/files/tasks.py +++ b/files/tasks.py @@ -950,45 +950,6 @@ def update_encoding_size(encoding_id): return False -@task(name="produce_video_chapters", queue="short_tasks") -def produce_video_chapters(chapter_id): - # this is not used - return False - chapter_object = VideoChapterData.objects.filter(id=chapter_id).first() - if not chapter_object: - return False - - media = chapter_object.media - video_path = media.media_file.path - output_folder = media.video_chapters_folder - - chapters = chapter_object.data - - width = 336 - height = 188 - - if not os.path.exists(output_folder): - os.makedirs(output_folder) - - results = [] - - for i, chapter in enumerate(chapters): - timestamp = chapter["start"] - title = chapter["title"] - - output_filename = f"thumbnail_{i:02d}.jpg" # noqa - output_path = os.path.join(output_folder, output_filename) - - command = [settings.FFMPEG_COMMAND, "-y", "-ss", str(timestamp), "-i", video_path, "-vframes", "1", "-q:v", "2", "-s", f"{width}x{height}", output_path] - ret = run_command(command) # noqa - if os.path.exists(output_path) and get_file_type(output_path) == "image": - results.append({"start": timestamp, "title": title, "thumbnail": output_path}) - - chapter_object.data = results - chapter_object.save(update_fields=["data"]) - return True - - @task(name="post_trim_action", queue="short_tasks", soft_time_limit=600) def post_trim_action(friendly_token): """Perform post-processing actions after video trimming diff --git a/files/views/pages.py b/files/views/pages.py index cb9e89e9..39d17fb5 100644 --- a/files/views/pages.py +++ b/files/views/pages.py @@ -259,17 +259,19 @@ def video_chapters(request, friendly_token): data = json.loads(request.body)["chapters"] chapters = [] for _, chapter_data in enumerate(data): - start_time = chapter_data.get('start') - title = chapter_data.get('title') - if start_time and title: + start_time = chapter_data.get('startTime') + end_time = chapter_data.get('endTime') + text = chapter_data.get('text') + if start_time and end_time and text: chapters.append( { - 'start': start_time, - 'title': title, + 'startTime': start_time, + 'endTime': end_time, + 'text': text, } ) except Exception as e: # noqa - return JsonResponse({'success': False, 'error': 'Request data must be a list of video chapters with start and title'}, status=400) + return JsonResponse({'success': False, 'error': 'Request data must be a list of video chapters with startTime, endTime, text'}, status=400) ret = handle_video_chapters(media, chapters)