Dearrow fixes

This commit is contained in:
WardPearce
2024-04-04 17:30:10 +13:00
parent 135e3cdcaa
commit cc6ec4c30f
2 changed files with 43 additions and 43 deletions
+1 -1
View File
@@ -241,7 +241,7 @@ export interface DeArrow {
UUID: string;
}[];
thumbnails: {
timestamp?: number;
timestamp: number | null;
original: boolean;
votes: number;
locked: boolean;
+42 -42
View File
@@ -32,64 +32,64 @@
let imageSrc = video.videoThumbnails[4].url;
if (get(deArrowEnabled)) {
let locatedThumbnail = false;
try {
const deArrow = await getDeArrow(video.videoId);
for (const title of deArrow.titles) {
if (title.locked && !title.original) {
if (title.locked || title.votes > 0) {
video.title = title.title.replace('>', '');
console.log(video.title);
break;
}
}
let locatedThumbnail = false;
for (const thumbnail of deArrow.thumbnails) {
if (thumbnail.locked || thumbnail.original) {
if (typeof thumbnail.timestamp !== 'undefined') {
if (thumbnail.locked || thumbnail.original || thumbnail.votes > 0) {
if (thumbnail.timestamp !== null) {
imageSrc = await getThumbnail(video.videoId, thumbnail.timestamp);
}
locatedThumbnail = true;
break;
}
}
if (!locatedThumbnail) {
// Process thumbnail locally.
const canvas = document.getElementById('canvas') as HTMLCanvasElement | null;
function generateThumbnail(): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
if (canvas) {
const context = canvas.getContext('2d');
const mockVideo = document.createElement('video');
const videoContainer = document.getElementById('video-container');
videoContainer?.appendChild(mockVideo);
mockVideo.preload = 'auto';
mockVideo.id = 'video';
mockVideo.crossOrigin = import.meta.env.VITE_DEFAULT_FRONTEND_URL;
mockVideo.src = proxyVideoUrl((await getVideo(video.videoId)).formatStreams[0].url);
mockVideo.addEventListener('loadeddata', () => {
mockVideo.currentTime = mockVideo.duration / 100;
mockVideo.addEventListener('seeked', () => {
if (context) {
context.drawImage(mockVideo, 0, 0, 320, 180);
resolve(canvas.toDataURL('image/png'));
mockVideo.preload = 'none';
mockVideo.pause();
videoContainer?.removeChild(mockVideo);
}
});
});
mockVideo.load();
}
});
}
imageSrc = await generateThumbnail();
}
} catch {}
if (!locatedThumbnail) {
// Process thumbnail locally.
const canvas = document.getElementById('canvas') as HTMLCanvasElement | null;
function generateThumbnail(): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
if (canvas) {
const context = canvas.getContext('2d');
const mockVideo = document.createElement('video');
const videoContainer = document.getElementById('video-container');
videoContainer?.appendChild(mockVideo);
mockVideo.preload = 'auto';
mockVideo.id = 'video';
mockVideo.crossOrigin = import.meta.env.VITE_DEFAULT_FRONTEND_URL;
mockVideo.src = proxyVideoUrl((await getVideo(video.videoId)).formatStreams[0].url);
mockVideo.addEventListener('loadeddata', () => {
mockVideo.currentTime = mockVideo.duration / 100;
mockVideo.addEventListener('seeked', () => {
if (context) {
context.drawImage(mockVideo, 0, 0, canvas.width, canvas.height);
resolve(canvas.toDataURL('image/png'));
mockVideo.preload = 'none';
mockVideo.pause();
videoContainer?.removeChild(mockVideo);
}
});
});
mockVideo.load();
}
});
}
imageSrc = await generateThumbnail();
}
}
img = new Image();