Auto-transcode on upload, show transcode status to user
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
getVideoByMapAndPlayer,
|
||||
updateVideoPB,
|
||||
updateTranscodedKey,
|
||||
updateTranscodeStatus,
|
||||
} from "../services/db";
|
||||
import {
|
||||
uploadObject,
|
||||
@@ -335,6 +336,26 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
|
||||
|
||||
await completeMultipartUpload(video.videoKey, uploadId, s3Parts);
|
||||
|
||||
// Auto-transcode in background
|
||||
if (
|
||||
!video.transcodedKey &&
|
||||
video.transcodeStatus !== "pending" &&
|
||||
video.transcodeStatus !== "processing"
|
||||
) {
|
||||
updateTranscodeStatus(id, "pending");
|
||||
const videoKey = video.videoKey;
|
||||
transcodeVideo(videoKey)
|
||||
.then((transcodedKey) => {
|
||||
updateTranscodedKey(id, transcodedKey);
|
||||
updateTranscodeStatus(id, "done");
|
||||
console.log(`[transcode] Done: ${id} -> ${transcodedKey}`);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`[transcode] Failed: ${id}`, err);
|
||||
updateTranscodeStatus(id, "failed");
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
...video,
|
||||
videoUrl: getStreamUrl(video.id),
|
||||
@@ -349,6 +370,16 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
|
||||
const video = getVideoById(id);
|
||||
if (!video) return status(404, { error: "Not found" });
|
||||
|
||||
if (
|
||||
video.transcodeStatus === "processing" ||
|
||||
video.transcodeStatus === "pending"
|
||||
) {
|
||||
return status(409, {
|
||||
error: "Transcode already in progress",
|
||||
transcodeStatus: video.transcodeStatus,
|
||||
});
|
||||
}
|
||||
|
||||
if (video.transcodedKey) {
|
||||
return {
|
||||
...video,
|
||||
@@ -360,18 +391,23 @@ export const videoRoutes = new Elysia({ prefix: "/api/videos" })
|
||||
};
|
||||
}
|
||||
|
||||
const transcodedKey = await transcodeVideo(video.videoKey);
|
||||
updateTranscodedKey(id, transcodedKey);
|
||||
// Mark as pending and return immediately
|
||||
updateTranscodeStatus(id, "pending");
|
||||
|
||||
return {
|
||||
...video,
|
||||
transcodedKey,
|
||||
videoUrl: getStreamUrl(video.id),
|
||||
mtvUrl: getObjectUrl(video.mtvKey),
|
||||
thumbnailUrl: video.thumbnailKey
|
||||
? getObjectUrl(video.thumbnailKey)
|
||||
: undefined,
|
||||
};
|
||||
// Run transcode in background
|
||||
const videoKey = video.videoKey;
|
||||
transcodeVideo(videoKey)
|
||||
.then((transcodedKey) => {
|
||||
updateTranscodedKey(id, transcodedKey);
|
||||
updateTranscodeStatus(id, "done");
|
||||
console.log(`[transcode] Done: ${id} -> ${transcodedKey}`);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`[transcode] Failed: ${id}`, err);
|
||||
updateTranscodeStatus(id, "failed");
|
||||
});
|
||||
|
||||
return { id, transcodeStatus: "pending" };
|
||||
})
|
||||
|
||||
.get("/:id/stream", async ({ params: { id }, request, set }) => {
|
||||
|
||||
Reference in New Issue
Block a user