Skip to content

Commit 4dad68d

Browse files
committed
fix(downloader): code splitting (happy-dom)
1 parent 33a09cc commit 4dad68d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/plugins/downloader/main/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import { Innertube, UniversalCache, Utils, YTNodes } from 'youtubei.js';
77
import is from 'electron-is';
88
import filenamify from 'filenamify';
99
import { Mutex } from 'async-mutex';
10-
import { createFFmpeg } from '@ffmpeg.wasm/main';
1110
import NodeID3, { TagConstants } from 'node-id3';
12-
13-
import { Window } from 'happy-dom';
1411
import { BG, type BgConfig } from 'bgutils-js';
12+
import { lazy } from 'lazy-var';
1513

1614
import {
1715
cropMaxWidth,
@@ -44,11 +42,13 @@ import type {
4442

4543
type CustomSongInfo = SongInfo & { trackId?: string };
4644

47-
const ffmpeg = createFFmpeg({
48-
log: false,
49-
logger() {}, // Console.log,
50-
progress() {}, // Console.log,
51-
});
45+
const ffmpeg = lazy(async () =>
46+
(await import('@ffmpeg.wasm/main')).createFFmpeg({
47+
log: false,
48+
logger() {}, // Console.log,
49+
progress() {}, // Console.log,
50+
}),
51+
);
5252
const ffmpegMutex = new Mutex();
5353

5454
let yt: Innertube;
@@ -142,7 +142,7 @@ export const onMainLoad = async ({
142142
try {
143143
const [width, height] = win.getSize();
144144
// emulate jsdom using linkedom
145-
const window = new Window({
145+
const window = new (await import('happy-dom')).Window({
146146
width,
147147
height,
148148
console,
@@ -505,12 +505,13 @@ async function iterableStreamToProcessedUint8Array(
505505

506506
return await ffmpegMutex.runExclusive(async () => {
507507
try {
508-
if (!ffmpeg.isLoaded()) {
509-
await ffmpeg.load();
508+
const ffmpegInstance = await ffmpeg.get();
509+
if (!ffmpegInstance.isLoaded()) {
510+
await ffmpegInstance.load();
510511
}
511512

512513
sendFeedback(t('plugins.downloader.backend.feedback.preparing-file'));
513-
ffmpeg.FS(
514+
ffmpegInstance.FS(
514515
'writeFile',
515516
safeVideoName,
516517
Buffer.concat(
@@ -525,7 +526,7 @@ async function iterableStreamToProcessedUint8Array(
525526

526527
sendFeedback(t('plugins.downloader.backend.feedback.converting'));
527528

528-
ffmpeg.setProgress(({ ratio }) => {
529+
ffmpegInstance.setProgress(({ ratio }) => {
529530
sendFeedback(
530531
t('plugins.downloader.backend.feedback.conversion-progress', {
531532
percent: Math.floor(ratio * 100),
@@ -537,23 +538,23 @@ async function iterableStreamToProcessedUint8Array(
537538

538539
const safeVideoNameWithExtension = `${safeVideoName}.${extension}`;
539540
try {
540-
await ffmpeg.run(
541+
await ffmpegInstance.run(
541542
'-i',
542543
safeVideoName,
543544
...presetFfmpegArgs,
544545
...getFFmpegMetadataArgs(metadata),
545546
safeVideoNameWithExtension,
546547
);
547548
} finally {
548-
ffmpeg.FS('unlink', safeVideoName);
549+
ffmpegInstance.FS('unlink', safeVideoName);
549550
}
550551

551552
sendFeedback(t('plugins.downloader.backend.feedback.saving'));
552553

553554
try {
554-
return ffmpeg.FS('readFile', safeVideoNameWithExtension);
555+
return ffmpegInstance.FS('readFile', safeVideoNameWithExtension);
555556
} finally {
556-
ffmpeg.FS('unlink', safeVideoNameWithExtension);
557+
ffmpegInstance.FS('unlink', safeVideoNameWithExtension);
557558
}
558559
} catch (error: unknown) {
559560
sendError(error as Error, safeVideoName);

0 commit comments

Comments
 (0)