Add video transcoding to H264/MP4, use high quality CRF 18

This commit is contained in:
CallMeVerity
2026-06-03 04:32:01 +01:00
parent 17ea6ce7c4
commit cc3904705c
5 changed files with 199 additions and 46 deletions
+37 -1
View File
@@ -8,6 +8,7 @@ import {
updateTier,
getVideoByMapAndPlayer,
updateVideoPB,
updateTranscodedKey,
} from "../services/db";
import {
uploadObject,
@@ -19,6 +20,7 @@ import {
completeMultipartUpload,
abortMultipartUpload,
getVideoStream,
transcodeVideo,
} from "../services/rustfs";
import { parseMtvFile } from "../services/mtv-parser";
import { getMapInfo } from "../services/momentum-api";
@@ -343,6 +345,35 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
};
})
.post("/:id/transcode", async ({ params: { id } }) => {
const video = getVideoById(id);
if (!video) return status(404, { error: "Not found" });
if (video.transcodedKey) {
return {
...video,
videoUrl: getStreamUrl(video.id),
mtvUrl: getObjectUrl(video.mtvKey),
thumbnailUrl: video.thumbnailKey
? getObjectUrl(video.thumbnailKey)
: undefined,
};
}
const transcodedKey = await transcodeVideo(video.videoKey);
updateTranscodedKey(id, transcodedKey);
return {
...video,
transcodedKey,
videoUrl: getStreamUrl(video.id),
mtvUrl: getObjectUrl(video.mtvKey),
thumbnailUrl: video.thumbnailKey
? getObjectUrl(video.thumbnailKey)
: undefined,
};
})
.get("/:id/stream", async ({ params: { id }, request, set }) => {
const video = getVideoById(id);
if (!video) return status(404, { error: "Not found" });
@@ -350,7 +381,10 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
const rangeHeader = request.headers.get("Range");
try {
const result = await getVideoStream(video.videoKey, rangeHeader);
const result = await getVideoStream(
video.transcodedKey ?? video.videoKey,
rangeHeader,
);
const headers = new Headers();
headers.set("Content-Type", result.contentType);
@@ -465,6 +499,8 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
deleteObject(video.mtvKey),
];
if (video.thumbnailKey) deletes.push(deleteObject(video.thumbnailKey));
if (video.transcodedKey)
deletes.push(deleteObject(video.transcodedKey));
await Promise.all(deletes);
deleteVideoById(id);