From cedebff1ce462f054a465fbd1bda55d291639ae1 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Tue, 24 Oct 2023 00:28:19 +1100 Subject: [PATCH] Add LocalArchive upload backend --- cutter/cutter/main.py | 6 ++++-- cutter/cutter/upload_backends.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cutter/cutter/main.py b/cutter/cutter/main.py index 784425a..e5ff70f 100644 --- a/cutter/cutter/main.py +++ b/cutter/cutter/main.py @@ -17,11 +17,11 @@ from psycopg2 import sql import common from common.database import DBManager, query, get_column_placeholder -from common.segments import get_best_segments, fast_cut_segments, full_cut_segments, smart_cut_segments, extract_frame, ContainsHoles +from common.segments import get_best_segments, archive_cut_segments, fast_cut_segments, full_cut_segments, smart_cut_segments, extract_frame, ContainsHoles from common.images import compose_thumbnail_template from common.stats import timed -from .upload_backends import Youtube, Local, UploadError +from .upload_backends import Youtube, Local, LocalArchive, UploadError videos_uploaded = prom.Counter( @@ -937,6 +937,8 @@ def main( backend_type = Youtube elif backend_type == 'local': backend_type = Local + elif backend_type == 'local-archive': + backend_type = LocalArchive else: raise ValueError("Unknown upload backend type: {!r}".format(backend_type)) backend = backend_type(credentials, **backend_config) diff --git a/cutter/cutter/upload_backends.py b/cutter/cutter/upload_backends.py index cb9cdfb..5787102 100644 --- a/cutter/cutter/upload_backends.py +++ b/cutter/cutter/upload_backends.py @@ -4,6 +4,7 @@ import json import logging import os import re +import time import uuid import common @@ -342,3 +343,30 @@ class Local(UploadBackend): 'tags': tags, 'public': public, }) + '\n') + + def set_thumbnail(self, video_id, thumbnail): + filepath = os.path.join(self.path, "{}.png".format(video_id)) + common.atomic_write(filepath, thumbnail) + + +class LocalArchive(Local): + """Similar to Local() but does archive cuts. See archive_cut_segments().""" + encoding_settings = "archive" + + def upload_video(self, title, description, tags, public, tempfiles): + # make title safe by removing offending characters, replacing with '-' + safe_title = re.sub('[^A-Za-z0-9_]', '-', title) + # To aid in finding the "latest" version if re-edited, prefix with current time. + prefix = str(time.time()) + video_dir = "{}-{}".format(prefix, safe_title) + common.ensure_directory(os.path.join(self.path, video_dir)) + for n, tempfile in enumerate(tempfiles): + filepath = os.path.join(self.path, video_dir, "{}-{}.mkv".format(safe_title, n)) + # We're assuming these are on the same filesystem. This may not always be true + # but it will be in our normal setup. If we ever need this in the future, we'll fix it then. + os.rename(tempfile, filepath) + if self.url_prefix is not None: + url = self.url_prefix + video_dir + else: + url = "file://{}".format(video_dir) + return prefix, url