NGU-Web/server/src/migrations/036_clip_jobs.sql
Zaldimmar beca01c351 Add a video clip tool: download our videos, cut, reshape, export
Tools → Video clips (migration 036, admins and up). Paste a YouTube
or Facebook link and the server downloads it with yt-dlp; then cut
clips: start and end set while watching, a shape (as filmed, 16:9,
square or 9:16 at the sizes the platforms ask for, cropped around a
chosen point shown over the video), and MP4 video or MP3 audio.

Jobs queue and run one at a time with progress. Only YouTube and
Facebook links are accepted, and both tools run with argument lists,
never a shell. A clip's file is deleted once it has been downloaded
in full, unless it's saved to the media library first, which now
holds video and audio (image fields still pick images only).
Everything is swept a day after it's made. Setup is in
docs/video-clips.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 02:43:05 -05:00

53 lines
2.6 KiB
SQL

-- ═══════════════════════════════════════════════════════════════
-- 036 VIDEO CLIPS
--
-- The admin's video clip tool (Tools → Video clips): download one of
-- our videos from YouTube or Facebook, cut clips from it, and take
-- them away as MP4 or MP3. One row per job, of two kinds:
--
-- source a download (yt-dlp) of source_url. When done, file is
-- the video in CLIPS_DIR and title/duration describe it.
-- clip a cut (ffmpeg) of a source: params holds the JSON
-- { start, end, shape, focus, format }. When done, file
-- is the clip in CLIPS_DIR.
--
-- Jobs run one at a time, oldest queued first (server/src/clips.js).
-- A job left 'running' by a restart is marked failed at boot.
--
-- Files are temporary. A clip's file is deleted once it has been
-- downloaded, unless it was saved to the media library first
-- (saved_media_id), and anything older than a day is swept, sources
-- included. Deleting a source deletes its clips.
--
-- PRAGMA user_version; -- reads 35 before this file
-- ═══════════════════════════════════════════════════════════════
CREATE TABLE clip_jobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
kind TEXT NOT NULL CHECK (kind IN ('source', 'clip')),
status TEXT NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued', 'running', 'done', 'failed')),
progress REAL NOT NULL DEFAULT 0 CHECK (progress BETWEEN 0 AND 1),
source_url TEXT, -- kind 'source'
source_id INTEGER REFERENCES clip_jobs (id) ON DELETE CASCADE, -- kind 'clip'
params TEXT, -- kind 'clip': JSON
title TEXT,
duration REAL, -- seconds
file TEXT, -- a name in CLIPS_DIR, once done
bytes INTEGER,
error TEXT,
saved_media_id INTEGER REFERENCES media (id) ON DELETE SET NULL,
downloaded_at TEXT, -- a clip's file went out, and was deleted
created_by INTEGER REFERENCES admin_users (id) ON DELETE SET NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
finished_at TEXT,
CHECK ((kind = 'source') = (source_url IS NOT NULL)),
CHECK ((kind = 'clip') = (source_id IS NOT NULL))
) STRICT;
CREATE INDEX clip_jobs_queue_idx ON clip_jobs (status, id);
CREATE INDEX clip_jobs_source_idx ON clip_jobs (source_id);