Skip to content

Commit

Permalink
added auto translated youtube subtitles
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyhalight committed Aug 25, 2024
1 parent a614375 commit 709388d
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions src/utils/youtubeUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { availableLangs } from "vot.js/consts";

import debug from "./debug.js";
import { localizationProvider } from "../localization/localizationProvider.js";
import { langTo6391, cleanText } from "./utils.js";
import { detect } from "./translateApis.js";

Expand Down Expand Up @@ -185,25 +186,59 @@ function isMusic() {

function getSubtitles() {
const response = getPlayerResponse();
let captionTracks =
response?.captions?.playerCaptionsTracklistRenderer?.captionTracks ?? [];
const playerCaptions = response?.captions?.playerCaptionsTracklistRenderer;
if (!playerCaptions) {
return [];
}

let captionTracks = playerCaptions.captionTracks ?? [];
const translationLanguages = playerCaptions.translationLanguages ?? [];
const userLang = localizationProvider.lang;
const userLangSupported = translationLanguages.find(
(language) => language.languageCode === userLang,
);
const asrLang =
captionTracks.find((captionTrack) => captionTrack?.kind === "asr")
?.languageCode ?? "en";
captionTracks = captionTracks.reduce((result, captionTrack) => {
if ("languageCode" in captionTrack) {
const language = captionTrack?.languageCode
? langTo6391(captionTrack?.languageCode)
: undefined;
const url = captionTrack?.url || captionTrack?.baseUrl;
language &&
url &&
result.push({
source: "youtube",
language,
isAutoGenerated: captionTrack?.kind === "asr",
url: `${
url.startsWith("http") ? url : `${window.location.origin}/${url}`
}&fmt=json3`,
});
if (!("languageCode" in captionTrack)) {
return result;
}

const language = captionTrack.languageCode
? langTo6391(captionTrack.languageCode)
: undefined;
const url = captionTrack?.url || captionTrack?.baseUrl;
if (!language || !url) {
return result;
}

const captionUrl = `${
url.startsWith("http") ? url : `${window.location.origin}/${url}`
}&fmt=json3`;
result.push({
source: "youtube",
language,
isAutoGenerated: captionTrack?.kind === "asr",
url: captionUrl,
});

if (
userLangSupported &&
captionTrack.isTranslatable &&
captionTrack.languageCode === asrLang &&
userLang !== language
) {
// add translated youtube subtitles (if it possible)
result.push({
source: "youtube",
language: userLang,
isAutoGenerated: captionTrack?.kind === "asr",
translatedFromLanguage: language,
url: `${captionUrl}&tlang=${userLang}`,
});
}

return result;
}, []);
debug.log("youtube subtitles:", captionTracks);
Expand Down

0 comments on commit 709388d

Please sign in to comment.