Subtitle label conversion

This commit is contained in:
mrjvs
2023-11-18 23:14:06 +01:00
parent fe4882b43e
commit ab5dcc7b42
4 changed files with 22 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
import ISO6391 from 'iso-639-1';
export const captionTypes = {
srt: 'srt',
vtt: 'vtt',
@@ -17,3 +19,14 @@ export function getCaptionTypeFromUrl(url: string): CaptionType | null {
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);
}

View File

@@ -2,7 +2,7 @@ import crypto from 'crypto-js';
import { flags } from '@/main/targets';
import { makeEmbed } from '@/providers/base';
import { Caption, getCaptionTypeFromUrl } from '@/providers/captions';
import { Caption, getCaptionTypeFromUrl, labelToLanguageCode } from '@/providers/captions';
const { AES, enc } = crypto;
@@ -102,8 +102,10 @@ export const upcloudScraper = makeEmbed({
if (track.kind !== 'captions') return;
const type = getCaptionTypeFromUrl(track.file);
if (!type) return;
const language = labelToLanguageCode(track.label);
if (!language) return;
captions.push({
language: track.label, // TODO Turn language name into ISO code
language,
hasCorsRestrictions: false,
type,
url: track.file,

View File

@@ -1,4 +1,4 @@
import { Caption, getCaptionTypeFromUrl } from '@/providers/captions';
import { Caption, getCaptionTypeFromUrl, isValidLanguageCode } from '@/providers/captions';
import { sendRequest } from '@/providers/sources/superstream/sendRequest';
import { ScrapeContext } from '@/utils/context';
@@ -42,6 +42,9 @@ export async function getSubtitles(
const subtitleType = getCaptionTypeFromUrl(subtitle.file_path);
if (!subtitleType) return;
const validCode = isValidLanguageCode(subtitle.lang);
if (!validCode) return;
output.push({
language: subtitle.lang,
hasCorsRestrictions: true,