Cleanup translation console logs

This commit is contained in:
vlOd2 2025-12-26 23:23:17 +02:00
parent edb9cad8dc
commit 9ad28631e9
2 changed files with 46 additions and 51 deletions

View file

@ -446,9 +446,7 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
clearTranslateTask() { clearTranslateTask() {
set((s) => { set((s) => {
console.log("Clearing translate task");
if (s.caption.translateTask) { if (s.caption.translateTask) {
console.log("Cancelling ongoing translate task");
s.caption.translateTask.cancel(); s.caption.translateTask.cancel();
} }
s.caption.translateTask = null; s.caption.translateTask = null;
@ -475,7 +473,9 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
done: false, done: false,
error: false, error: false,
cancel() { cancel() {
console.log("Translation task cancelled by user"); if (!this.done || !this.error) {
console.log("Translation task was cancelled");
}
cancelled = true; cancelled = true;
}, },
}; };
@ -535,7 +535,6 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
}; };
s.caption.translateTask.done = true; s.caption.translateTask.done = true;
s.caption.translateTask.translatedCaption = translatedCaption; s.caption.translateTask.translatedCaption = translatedCaption;
console.log("Caption translation completed", s.caption.translateTask);
}); });
} catch (err) { } catch (err) {
handleError(err); handleError(err);

View file

@ -2,7 +2,7 @@ import subsrt from "subsrt-ts";
import { Caption, ContentCaption } from "subsrt-ts/dist/types/handler"; import { Caption, ContentCaption } from "subsrt-ts/dist/types/handler";
const API_URL = const API_URL =
"https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&oe=UTF-8&sl=auto&tl={TARGET_LANG}&q="; "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&oe=UTF-8&sl=auto";
const RETRY_COUNT = 3; const RETRY_COUNT = 3;
const FETCH_RATE = 100; const FETCH_RATE = 100;
const SUBTITLES_CACHE: Map<string, ArrayBuffer> = new Map< const SUBTITLES_CACHE: Map<string, ArrayBuffer> = new Map<
@ -29,6 +29,18 @@ async function decompressStr(byteArray: ArrayBuffer): Promise<string> {
}); });
} }
function tryUseCachedCaption(
caption: ContentCaption,
cache: Map<string, string>,
): boolean {
const text: string | undefined = cache.get(caption.text);
if (text) {
caption.text = text;
return true;
}
return false;
}
async function translateText( async function translateText(
text: string, text: string,
targetLang: string, targetLang: string,
@ -38,15 +50,12 @@ async function translateText(
} }
const response = await ( const response = await (
await fetch( await fetch(`${API_URL}&tl=${targetLang}&q=${encodeURIComponent(text)}`, {
`${API_URL.replace("{TARGET_LANG}", targetLang)}${encodeURIComponent(text)}`, method: "GET",
{ headers: {
method: "GET", Accept: "application/json",
headers: {
Accept: "application/json",
},
}, },
) })
).json(); ).json();
if (!response) { if (!response) {
@ -75,11 +84,11 @@ async function translateCaption(
break; break;
} }
} catch (error) { } catch (error) {
console.warn("[CTR] Re-trying caption translation", caption, error); console.warn("Re-trying caption translation", caption, error);
} }
} }
if (!text) { if (!text) {
console.error("[CTR] Failed to translate caption"); console.error("Failed to translate caption");
caption.text = `(CAPTION COULD NOT BE TRANSLATED)\n${caption.text}}`; caption.text = `(CAPTION COULD NOT BE TRANSLATED)\n${caption.text}}`;
return false; return false;
} }
@ -91,7 +100,7 @@ async function translateCaptions(
captions: ContentCaption[], captions: ContentCaption[],
targetLang: string, targetLang: string,
): Promise<boolean> { ): Promise<boolean> {
console.log("[CTR] Translating", captions.length, "captions"); // console.log("Translating", captions.length, "captions");
try { try {
const results: boolean[] = await Promise.all( const results: boolean[] = await Promise.all(
captions.map((c) => translateCaption(c, targetLang)), captions.map((c) => translateCaption(c, targetLang)),
@ -101,42 +110,25 @@ async function translateCaptions(
const failedCount = results.length - successCount; const failedCount = results.length - successCount;
const successPercentange = (successCount / results.length) * 100; const successPercentange = (successCount / results.length) * 100;
const failedPercentange = (failedCount / results.length) * 100; const failedPercentange = (failedCount / results.length) * 100;
console.log( // console.log(
"[CTR] Done translating captions", // "Done translating captions",
results.length, // results.length,
successCount, // successCount,
failedCount, // failedCount,
successPercentange, // successPercentange,
failedPercentange, // failedPercentange,
); // );
if (failedPercentange > successPercentange) { if (failedPercentange > successPercentange) {
throw new Error("Success percentage is not acceptable"); throw new Error("Success percentage is not acceptable");
} }
} catch (error) { } catch (error) {
console.error( console.error("Could not translate", captions.length, "captions", error);
"[CTR] Could not translate",
captions.length,
"captions",
error,
);
return false; return false;
} }
return true; return true;
} }
function tryUseCached(
caption: ContentCaption,
cache: Map<string, string>,
): boolean {
const text: string | undefined = cache.get(caption.text);
if (text) {
caption.text = text;
return true;
}
return false;
}
async function translateSRTData( async function translateSRTData(
data: string, data: string,
targetLang: string, targetLang: string,
@ -145,7 +137,7 @@ async function translateSRTData(
try { try {
captions = subsrt.parse(data); captions = subsrt.parse(data);
} catch (error) { } catch (error) {
console.error("[CTR] Failed to parse subtitle data", error); console.error("Failed to parse subtitle data", error);
return undefined; return undefined;
} }
@ -166,7 +158,7 @@ async function translateSRTData(
} }
for (let i = 0; i < contentCaptions.length; i += 1) { for (let i = 0; i < contentCaptions.length; i += 1) {
if (tryUseCached(contentCaptions[i], translatedCache)) { if (tryUseCachedCaption(contentCaptions[i], translatedCache)) {
continue; continue;
} }
const batch: ContentCaption[] = [contentCaptions[i]]; const batch: ContentCaption[] = [contentCaptions[i]];
@ -176,7 +168,7 @@ async function translateSRTData(
if (i + j >= contentCaptions.length) { if (i + j >= contentCaptions.length) {
break; break;
} }
if (tryUseCached(contentCaptions[i + j], translatedCache)) { if (tryUseCachedCaption(contentCaptions[i + j], translatedCache)) {
continue; continue;
} }
batch.push(contentCaptions[i + j]); batch.push(contentCaptions[i + j]);
@ -196,18 +188,21 @@ async function translateSRTData(
: undefined; : undefined;
} }
// TODO: make this support multiple providers rather than just google translate
export async function translateSubtitle( export async function translateSubtitle(
id: string, id: string,
srtData: string, srtData: string,
targetLang: string, targetLang: string,
): Promise<string | undefined> { ): Promise<string | undefined> {
id = `${id}_${targetLang}`; const cacheID = `${id}_${targetLang}`;
const cachedData: ArrayBuffer | undefined = SUBTITLES_CACHE.get(id);
const cachedData: ArrayBuffer | undefined = SUBTITLES_CACHE.get(cacheID);
if (cachedData) { if (cachedData) {
console.log("[CTR] Using cached translation for", id); // console.log("Using cached translation for", id, cacheID);
return decompressStr(cachedData); return decompressStr(cachedData);
} }
console.log("[CTR] Translating", id);
// console.log("Translating", id);
const translatedData: string | undefined = await translateSRTData( const translatedData: string | undefined = await translateSRTData(
srtData, srtData,
targetLang, targetLang,
@ -215,7 +210,8 @@ export async function translateSubtitle(
if (!translatedData) { if (!translatedData) {
return undefined; return undefined;
} }
console.log("[CTR] Caching translation for", id);
SUBTITLES_CACHE.set(id, await compressStr(translatedData)); // console.log("Caching translation for", id, cacheID);
SUBTITLES_CACHE.set(cacheID, await compressStr(translatedData));
return translatedData; return translatedData;
} }