feat: Video Trimmer and more

This commit is contained in:
Yiannis Christodoulou
2025-05-15 10:43:26 +03:00
committed by GitHub
parent ab4d9d67df
commit a833b606f1
102 changed files with 13266 additions and 523 deletions
+110 -2
View File
@@ -2,19 +2,25 @@
# related content
import itertools
import json
import logging
import os
import random
import re
import tempfile
from datetime import datetime
from django.conf import settings
from django.core.cache import cache
from django.core.files import File
from django.core.mail import EmailMessage
from django.db.models import Q
from django.utils import timezone
from cms import celery_app
from . import models
from . import helpers, models
from .models import Encoding
from .helpers import mask_ip
logger = logging.getLogger(__name__)
@@ -262,7 +268,7 @@ def show_related_media_content(media, request, limit):
"user_featured",
"-user_featured",
]
# TODO: MAke this mess more readable, and add TAGS support - aka related
# TODO: Make this mess more readable, and add TAGS support - aka related
# tags rather than random media
if len(m) < limit:
category = media.category.first()
@@ -398,6 +404,97 @@ def clean_comment(raw_comment):
return cleaned_comment
def copy_video(original_media, copy_encodings=True, title_suffix="(Trimmed)"):
"""Create a copy of a media object
Args:
original_media: Original Media object to copy
copy_encodings: Whether to copy the encodings too
Returns:
New Media object
"""
with open(original_media.media_file.path, "rb") as f:
myfile = File(f)
new_media = models.Media.objects.create(
media_file=myfile,
title=f"{original_media.title} {title_suffix}",
description=original_media.description,
user=original_media.user,
media_type="video",
enable_comments=original_media.enable_comments,
allow_download=original_media.allow_download,
state=original_media.state,
is_reviewed=original_media.is_reviewed,
add_date=timezone.now()
)
if copy_encodings:
for encoding in original_media.encodings.filter(status="success", chunk=False):
if encoding.media_file:
with open(encoding.media_file.path, "rb") as f:
myfile = File(f)
new_encoding = Encoding.objects.create(
media_file=myfile,
media=new_media,
profile=encoding.profile,
status="success",
progress=100,
chunk=False,
logs=f"Copied from encoding {encoding.id}"
)
new_encoding.save()
# Copy categories and tags
for category in original_media.category.all():
new_media.category.add(category)
for tag in original_media.tags.all():
new_media.tags.add(tag)
# Copy thumbnails if they exist
if original_media.thumbnail:
with open(original_media.thumbnail.path, 'rb') as f:
thumbnail_name = helpers.get_file_name(original_media.thumbnail.path)
new_media.thumbnail.save(thumbnail_name, File(f))
if original_media.poster:
with open(original_media.poster.path, 'rb') as f:
poster_name = helpers.get_file_name(original_media.poster.path)
new_media.poster.save(poster_name, File(f))
return new_media
def create_video_trim_request(media, data):
"""Create a video trim request for a media
Args:
media: Media object
data: Dictionary with trim request data
Returns:
VideoTrimRequest object
"""
video_action = "replace"
if data.get('saveIndividualSegments'):
video_action = "create_segments"
elif data.get('saveAsCopy'):
video_action = "save_new"
video_trim_request = models.VideoTrimRequest.objects.create(
media=media,
status="initial",
video_action=video_action,
media_trim_style='no_encoding',
timestamps=data.get('segments', {})
)
return video_trim_request
def list_tasks():
"""Lists celery tasks
To be used in an admin dashboard
@@ -448,3 +545,14 @@ def list_tasks():
ret["task_ids"] = task_ids
ret["media_profile_pairs"] = media_profile_pairs
return ret
def handle_video_chapters(media, chapters):
video_chapter = models.VideoChapterData.objects.filter(media=media).first()
if video_chapter:
video_chapter.data = chapters
video_chapter.save()
else:
video_chapter = models.VideoChapterData.objects.create(media=media, data=chapters)
return media.chapter_data