Add Discovery Videos feature
Let users upload videos, tag them with one or more entries, and watch them from a new Discovery Videos page. Backend: - Add Video and VideoEntry models and relations - Add video controller and /video routes - Add video thumbnail generation service (ffmpeg-static) - Add video upload storage location and config Frontend: - Add DiscoveryVideosPage with sidebar link and route - Add AddVideoDialog, VideoDropzone, EntryMultiSelect, VideoCard and VideoPlayerDialog components - Add discoveryVideos API client and useDiscoveryVideos hook - Add format helpers, video types and normalizers - Add skeleton for the page Docs: - Document the feature in docs-vault
This commit is contained in:
1 parent
4d88747649
commit
eb3a12f6cb
41 files changed
+2163
-15
No files matched your search
@@ -120,6 +120,7 @@ class ConfigService {
|
||||
"peoples",
|
||||
"profiles",
|
||||
);
|
||||
const videosDir = path.join(dataPath, "videos");
|
||||
|
||||
fs.mkdirSync(posterDir, { recursive: true });
|
||||
fs.mkdirSync(backdropDir, { recursive: true });
|
||||
@@ -129,6 +130,7 @@ class ConfigService {
|
||||
fs.mkdirSync(seasonsPostersDir, { recursive: true });
|
||||
fs.mkdirSync(episodesStillsDir, { recursive: true });
|
||||
fs.mkdirSync(peopleProfilesDir, { recursive: true });
|
||||
fs.mkdirSync(videosDir, { recursive: true });
|
||||
}
|
||||
|
||||
getTmdbApiUrl() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const multer = require("multer");
|
||||
const fs = require("fs");
|
||||
const { v4: uuidv4 } = require("uuid");
|
||||
const path = require("path");
|
||||
const configService = require("./configService");
|
||||
@@ -57,8 +58,27 @@ const peoplePicturesUpload = multer({
|
||||
}),
|
||||
});
|
||||
|
||||
// ? discovery videos
|
||||
const videosUpload = multer({
|
||||
storage: multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
const dataPath = configService.getSettings().userDataPath;
|
||||
const videosDir = path.join(dataPath, "videos");
|
||||
// ensure the folder exists even if createMediaFolders() was never called
|
||||
fs.mkdirSync(videosDir, { recursive: true });
|
||||
cb(null, videosDir);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = file.originalname.split(".").pop();
|
||||
cb(null, `${uniqueFilename}.${extension}`);
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
entryPicturesUpload,
|
||||
collectionPicturesUpload,
|
||||
peoplePicturesUpload,
|
||||
videosUpload,
|
||||
};
|
||||
@@ -0,0 +1,151 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { execFile } = require("child_process");
|
||||
|
||||
const configService = require("./configService");
|
||||
const Video = require("../models/Video");
|
||||
|
||||
const { logToFile } = require("./logService");
|
||||
|
||||
// Limit concurrent ffmpeg jobs so a page full of missing thumbnails
|
||||
// doesn't start one heavy process per image request at once.
|
||||
const MAX_CONCURRENT_JOBS = 2;
|
||||
let activeJobs = 0;
|
||||
const jobQueue = [];
|
||||
|
||||
function resolveFfmpeg() {
|
||||
try {
|
||||
const bin = require("ffmpeg-static");
|
||||
if (bin && fs.existsSync(bin)) return bin;
|
||||
} catch {
|
||||
// ffmpeg-static not installed
|
||||
}
|
||||
return "ffmpeg"; // fall back to a system-wide ffmpeg on PATH
|
||||
}
|
||||
|
||||
function extractFrame(ffmpeg, inputPath, outputPath, seek) {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(
|
||||
ffmpeg,
|
||||
[
|
||||
"-y",
|
||||
"-ss",
|
||||
seek,
|
||||
"-i",
|
||||
inputPath,
|
||||
"-frames:v",
|
||||
"1",
|
||||
"-vf",
|
||||
"scale=640:-2",
|
||||
"-q:v",
|
||||
"3",
|
||||
outputPath,
|
||||
],
|
||||
{ timeout: 60000, windowsHide: true },
|
||||
(err) => (err ? reject(err) : resolve()),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function runLimited(job) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const run = async () => {
|
||||
try {
|
||||
resolve(await job());
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
} finally {
|
||||
activeJobs -= 1;
|
||||
const next = jobQueue.shift();
|
||||
if (next) {
|
||||
activeJobs += 1;
|
||||
next();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (activeJobs < MAX_CONCURRENT_JOBS) {
|
||||
activeJobs += 1;
|
||||
run();
|
||||
} else {
|
||||
jobQueue.push(run);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a thumbnail for a video: extracts a frame at 1s (falls back
|
||||
* to frame 0 for very short videos) and stores `{uuid}.jpg` in
|
||||
* data/videos/thumbnails/.
|
||||
*
|
||||
* Returns the thumbnail filename on success, or null on failure.
|
||||
* Never throws — a missing thumbnail must not break requests.
|
||||
*/
|
||||
async function generateThumbnail(video) {
|
||||
try {
|
||||
// Re-check against the DB — the thumbnail may have been generated
|
||||
// since this video instance was loaded.
|
||||
const fresh = await Video.findByPk(video.id);
|
||||
if (!fresh) return null;
|
||||
|
||||
const dataPath = configService.getSettings().userDataPath;
|
||||
const videosDir = path.join(dataPath, "videos");
|
||||
const thumbDir = path.join(videosDir, "thumbnails");
|
||||
|
||||
const inputPath = path.join(videosDir, fresh.filename);
|
||||
if (!fs.existsSync(inputPath)) return null;
|
||||
|
||||
const thumbName = `${fresh.filename.split(".")[0]}.jpg`;
|
||||
const outputPath = path.join(thumbDir, thumbName);
|
||||
|
||||
// Already generated → just make sure the DB knows about it
|
||||
if (fs.existsSync(outputPath)) {
|
||||
if (!fresh.thumbnail_path) {
|
||||
await fresh.update({ thumbnail_path: thumbName });
|
||||
}
|
||||
return thumbName;
|
||||
}
|
||||
|
||||
fs.mkdirSync(thumbDir, { recursive: true });
|
||||
|
||||
const ffmpeg = resolveFfmpeg();
|
||||
try {
|
||||
await extractFrame(ffmpeg, inputPath, outputPath, "1");
|
||||
} catch {
|
||||
// very short videos — grab the first frame instead
|
||||
await extractFrame(ffmpeg, inputPath, outputPath, "0");
|
||||
}
|
||||
|
||||
if (!fs.existsSync(outputPath)) return null;
|
||||
|
||||
await fresh.update({ thumbnail_path: thumbName });
|
||||
logToFile(`Video Thumbnail | Generated for video ${fresh.id}`);
|
||||
return thumbName;
|
||||
} catch (err) {
|
||||
logToFile(`Video Thumbnail | Error for video ${video.id}`);
|
||||
logToFile(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const inFlight = new Map(); // videoId -> Promise<string | null>
|
||||
|
||||
/**
|
||||
* On-demand thumbnail generation.
|
||||
* - Per-video dedup: concurrent requests for the same video share one job.
|
||||
* - Global concurrency limit (MAX_CONCURRENT_JOBS ffmpeg processes).
|
||||
* Called only from the thumbnail endpoint, i.e. when a client actually
|
||||
* requests a thumbnail that doesn't exist yet.
|
||||
*/
|
||||
function ensureThumbnail(video) {
|
||||
if (inFlight.has(video.id)) return inFlight.get(video.id);
|
||||
|
||||
const tracked = runLimited(() => generateThumbnail(video));
|
||||
const shared = tracked.finally(() => {
|
||||
inFlight.delete(video.id);
|
||||
});
|
||||
inFlight.set(video.id, shared);
|
||||
return shared;
|
||||
}
|
||||
|
||||
module.exports = { ensureThumbnail, generateThumbnail };
|
||||
Reference in new issue
Block a user