mirror of
https://github.com/p-stream/providers.git
synced 2026-05-11 19:40:53 +00:00
32 lines
802 B
TypeScript
32 lines
802 B
TypeScript
import ISO6391 from 'iso-639-1';
|
|
|
|
export const captionTypes = {
|
|
srt: 'srt',
|
|
vtt: 'vtt',
|
|
};
|
|
export type CaptionType = keyof typeof captionTypes;
|
|
|
|
export type Caption = {
|
|
type: CaptionType;
|
|
url: string;
|
|
hasCorsRestrictions: boolean;
|
|
language: string;
|
|
};
|
|
|
|
export function getCaptionTypeFromUrl(url: string): CaptionType | null {
|
|
const extensions = Object.keys(captionTypes) as CaptionType[];
|
|
const type = extensions.find((v) => url.endsWith(`.${v}`));
|
|
if (!type) return null;
|
|
return type;
|
|
}
|
|
|
|
export function labelToLanguageCode(label: string): string | null {
|
|
const code = ISO6391.getCode(label);
|
|
if (code.length === 0) return null;
|
|
return code;
|
|
}
|
|
|
|
export function isValidLanguageCode(code: string | null): boolean {
|
|
if (!code) return false;
|
|
return ISO6391.validate(code);
|
|
}
|