2023-02-08 22:51:52 +01:00
|
|
|
import { mwFetch, proxiedFetch } from "@/backend/helpers/fetch";
|
|
|
|
import { MWCaption, MWCaptionType } from "@/backend/helpers/streams";
|
|
|
|
import toWebVTT from "srt-webvtt";
|
|
|
|
|
2023-03-03 17:33:30 +01:00
|
|
|
export const CUSTOM_CAPTION_ID = "customCaption";
|
2023-02-08 22:51:52 +01:00
|
|
|
export async function getCaptionUrl(caption: MWCaption): Promise<string> {
|
|
|
|
if (caption.type === MWCaptionType.SRT) {
|
|
|
|
let captionBlob: Blob;
|
|
|
|
|
|
|
|
if (caption.needsProxy) {
|
|
|
|
captionBlob = await proxiedFetch<Blob>(caption.url, {
|
|
|
|
responseType: "blob" as any,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
captionBlob = await mwFetch<Blob>(caption.url, {
|
|
|
|
responseType: "blob" as any,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return toWebVTT(captionBlob);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caption.type === MWCaptionType.VTT) {
|
|
|
|
if (caption.needsProxy) {
|
|
|
|
const blob = await proxiedFetch<Blob>(caption.url, {
|
|
|
|
responseType: "blob" as any,
|
|
|
|
});
|
|
|
|
return URL.createObjectURL(blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
return caption.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("invalid type");
|
|
|
|
}
|
2023-03-03 17:33:30 +01:00
|
|
|
|
|
|
|
export async function convertCustomCaptionFileToWebVTT(file: File) {
|
|
|
|
const header = await file.slice(0, 6).text();
|
|
|
|
const isWebVTT = header === "WEBVTT";
|
|
|
|
if (!isWebVTT) {
|
|
|
|
return toWebVTT(file);
|
|
|
|
}
|
|
|
|
return URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function revokeCaptionBlob(url: string | undefined) {
|
|
|
|
if (url && url.startsWith("blob:")) {
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
}
|
|
|
|
}
|