style: auto-fix linting issues
This commit is contained in:
parent
1c1d202e91
commit
4aaffd2c22
6 changed files with 51 additions and 43 deletions
|
|
@ -1113,7 +1113,7 @@ export class LosslessAPI {
|
||||||
try {
|
try {
|
||||||
// MP3_320 is not a native TIDAL quality, we download LOSSLESS and convert
|
// MP3_320 is not a native TIDAL quality, we download LOSSLESS and convert
|
||||||
const downloadQuality = quality === 'MP3_320' ? 'LOSSLESS' : quality;
|
const downloadQuality = quality === 'MP3_320' ? 'LOSSLESS' : quality;
|
||||||
|
|
||||||
const lookup = await this.getTrack(id, downloadQuality);
|
const lookup = await this.getTrack(id, downloadQuality);
|
||||||
let streamUrl;
|
let streamUrl;
|
||||||
let blob;
|
let blob;
|
||||||
|
|
|
||||||
|
|
@ -53,13 +53,13 @@ function createTextFrame(frameId, text) {
|
||||||
// ID3v2.3 UTF-16 encoding with BOM
|
// ID3v2.3 UTF-16 encoding with BOM
|
||||||
const bom = new Uint8Array([0xff, 0xfe]); // UTF-16LE BOM
|
const bom = new Uint8Array([0xff, 0xfe]); // UTF-16LE BOM
|
||||||
const utf16Bytes = new Uint8Array(text.length * 2);
|
const utf16Bytes = new Uint8Array(text.length * 2);
|
||||||
|
|
||||||
for (let i = 0; i < text.length; i++) {
|
for (let i = 0; i < text.length; i++) {
|
||||||
const charCode = text.charCodeAt(i);
|
const charCode = text.charCodeAt(i);
|
||||||
utf16Bytes[i * 2] = charCode & 0xff;
|
utf16Bytes[i * 2] = charCode & 0xff;
|
||||||
utf16Bytes[i * 2 + 1] = (charCode >> 8) & 0xff;
|
utf16Bytes[i * 2 + 1] = (charCode >> 8) & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
const frameSize = 1 + bom.length + utf16Bytes.length;
|
const frameSize = 1 + bom.length + utf16Bytes.length;
|
||||||
const frame = new Uint8Array(10 + frameSize);
|
const frame = new Uint8Array(10 + frameSize);
|
||||||
const view = new DataView(frame.buffer);
|
const view = new DataView(frame.buffer);
|
||||||
|
|
|
||||||
|
|
@ -53,9 +53,9 @@ export async function addMetadataToAudio(audioBlob, track, api, _quality) {
|
||||||
// DASH Hi-Res streams may return fragmented MP4 instead of raw FLAC
|
// DASH Hi-Res streams may return fragmented MP4 instead of raw FLAC
|
||||||
const buffer = await audioBlob.slice(0, 12).arrayBuffer();
|
const buffer = await audioBlob.slice(0, 12).arrayBuffer();
|
||||||
const view = new DataView(buffer);
|
const view = new DataView(buffer);
|
||||||
|
|
||||||
const format = detectAudioFormat(view, audioBlob.type);
|
const format = detectAudioFormat(view, audioBlob.type);
|
||||||
|
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case 'flac':
|
case 'flac':
|
||||||
return await addFlacMetadata(audioBlob, track, api);
|
return await addFlacMetadata(audioBlob, track, api);
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,16 @@ class MP3EncodingError extends Error {
|
||||||
|
|
||||||
async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) {
|
async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) {
|
||||||
const audioData = await audioBlob.arrayBuffer();
|
const audioData = await audioBlob.arrayBuffer();
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const worker = new Worker(new URL('./mp3-encoder.worker.js', import.meta.url), { type: 'module' });
|
const worker = new Worker(new URL('./mp3-encoder.worker.js', import.meta.url), { type: 'module' });
|
||||||
|
|
||||||
// Handle abort signal
|
// Handle abort signal
|
||||||
const abortHandler = () => {
|
const abortHandler = () => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
reject(new MP3EncodingError('MP3 encoding aborted'));
|
reject(new MP3EncodingError('MP3 encoding aborted'));
|
||||||
};
|
};
|
||||||
|
|
||||||
if (signal) {
|
if (signal) {
|
||||||
if (signal.aborted) {
|
if (signal.aborted) {
|
||||||
abortHandler();
|
abortHandler();
|
||||||
|
|
@ -25,10 +25,10 @@ async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) {
|
||||||
}
|
}
|
||||||
signal.addEventListener('abort', abortHandler);
|
signal.addEventListener('abort', abortHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.onmessage = (e) => {
|
worker.onmessage = (e) => {
|
||||||
const { type, blob, message, stage, progress } = e.data;
|
const { type, blob, message, stage, progress } = e.data;
|
||||||
|
|
||||||
if (type === 'complete') {
|
if (type === 'complete') {
|
||||||
if (signal) signal.removeEventListener('abort', abortHandler);
|
if (signal) signal.removeEventListener('abort', abortHandler);
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
|
@ -43,17 +43,20 @@ async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) {
|
||||||
console.log('[FFmpeg]', message);
|
console.log('[FFmpeg]', message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
worker.onerror = (error) => {
|
worker.onerror = (error) => {
|
||||||
if (signal) signal.removeEventListener('abort', abortHandler);
|
if (signal) signal.removeEventListener('abort', abortHandler);
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
reject(new MP3EncodingError('Worker failed: ' + error.message));
|
reject(new MP3EncodingError('Worker failed: ' + error.message));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Transfer audio data to worker
|
// Transfer audio data to worker
|
||||||
worker.postMessage({
|
worker.postMessage(
|
||||||
audioData
|
{
|
||||||
}, [audioData]);
|
audioData,
|
||||||
|
},
|
||||||
|
[audioData]
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +66,7 @@ export async function encodeToMp3(audioBlob, onProgress = null, signal = null) {
|
||||||
if (typeof Worker !== 'undefined') {
|
if (typeof Worker !== 'undefined') {
|
||||||
return await encodeToMp3Worker(audioBlob, onProgress, signal);
|
return await encodeToMp3Worker(audioBlob, onProgress, signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new MP3EncodingError('Web Workers are required for MP3 encoding');
|
throw new MP3EncodingError('Web Workers are required for MP3 encoding');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('MP3 encoding failed:', error);
|
console.error('MP3 encoding failed:', error);
|
||||||
|
|
|
||||||
|
|
@ -6,63 +6,68 @@ let loadingPromise = null;
|
||||||
|
|
||||||
async function loadFFmpeg() {
|
async function loadFFmpeg() {
|
||||||
if (loadingPromise) return loadingPromise;
|
if (loadingPromise) return loadingPromise;
|
||||||
|
|
||||||
loadingPromise = (async () => {
|
loadingPromise = (async () => {
|
||||||
ffmpeg = new FFmpeg();
|
ffmpeg = new FFmpeg();
|
||||||
|
|
||||||
ffmpeg.on('log', ({ message }) => {
|
ffmpeg.on('log', ({ message }) => {
|
||||||
self.postMessage({ type: 'log', message });
|
self.postMessage({ type: 'log', message });
|
||||||
});
|
});
|
||||||
|
|
||||||
ffmpeg.on('progress', ({ progress, time }) => {
|
ffmpeg.on('progress', ({ progress, time }) => {
|
||||||
self.postMessage({
|
self.postMessage({
|
||||||
type: 'progress',
|
type: 'progress',
|
||||||
stage: 'encoding',
|
stage: 'encoding',
|
||||||
progress: progress * 100,
|
progress: progress * 100,
|
||||||
time
|
time,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
self.postMessage({ type: 'progress', stage: 'loading', message: 'Loading FFmpeg...' });
|
self.postMessage({ type: 'progress', stage: 'loading', message: 'Loading FFmpeg...' });
|
||||||
|
|
||||||
const baseURL = 'https://unpkg.com/@ffmpeg/core@0.12.6/dist/esm';
|
const baseURL = 'https://unpkg.com/@ffmpeg/core@0.12.6/dist/esm';
|
||||||
await ffmpeg.load({
|
await ffmpeg.load({
|
||||||
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
|
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
|
||||||
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm')
|
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'),
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return loadingPromise;
|
return loadingPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.onmessage = async (e) => {
|
self.onmessage = async (e) => {
|
||||||
const { audioData } = e.data;
|
const { audioData } = e.data;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await loadFFmpeg();
|
await loadFFmpeg();
|
||||||
|
|
||||||
self.postMessage({ type: 'progress', stage: 'encoding', message: 'Encoding to MP3 320kbps...' });
|
self.postMessage({ type: 'progress', stage: 'encoding', message: 'Encoding to MP3 320kbps...' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Write input file to FFmpeg virtual filesystem
|
// Write input file to FFmpeg virtual filesystem
|
||||||
await ffmpeg.writeFile('input', new Uint8Array(audioData));
|
await ffmpeg.writeFile('input', new Uint8Array(audioData));
|
||||||
|
|
||||||
// Encode to MP3 with 320kbps CBR, strip source metadata to avoid duplicate ID3 tags
|
// Encode to MP3 with 320kbps CBR, strip source metadata to avoid duplicate ID3 tags
|
||||||
await ffmpeg.exec([
|
await ffmpeg.exec([
|
||||||
'-i', 'input',
|
'-i',
|
||||||
'-map_metadata', '-1',
|
'input',
|
||||||
'-c:a', 'libmp3lame',
|
'-map_metadata',
|
||||||
'-b:a', '320k',
|
'-1',
|
||||||
'-ar', '44100',
|
'-c:a',
|
||||||
'output.mp3'
|
'libmp3lame',
|
||||||
|
'-b:a',
|
||||||
|
'320k',
|
||||||
|
'-ar',
|
||||||
|
'44100',
|
||||||
|
'output.mp3',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
self.postMessage({ type: 'progress', stage: 'finalizing', message: 'Finalizing MP3...' });
|
self.postMessage({ type: 'progress', stage: 'finalizing', message: 'Finalizing MP3...' });
|
||||||
|
|
||||||
// Read output file - use Uint8Array directly to avoid extra bytes from ArrayBuffer
|
// Read output file - use Uint8Array directly to avoid extra bytes from ArrayBuffer
|
||||||
const data = await ffmpeg.readFile('output.mp3');
|
const data = await ffmpeg.readFile('output.mp3');
|
||||||
const mp3Blob = new Blob([data], { type: 'audio/mpeg' });
|
const mp3Blob = new Blob([data], { type: 'audio/mpeg' });
|
||||||
|
|
||||||
self.postMessage({ type: 'complete', blob: mp3Blob });
|
self.postMessage({ type: 'complete', blob: mp3Blob });
|
||||||
} finally {
|
} finally {
|
||||||
// Always cleanup virtual filesystem files
|
// Always cleanup virtual filesystem files
|
||||||
|
|
|
||||||
|
|
@ -150,12 +150,12 @@ export const detectAudioFormat = (view, mimeType = '') => {
|
||||||
export const getExtensionFromBlob = async (blob) => {
|
export const getExtensionFromBlob = async (blob) => {
|
||||||
const buffer = await blob.slice(0, 12).arrayBuffer();
|
const buffer = await blob.slice(0, 12).arrayBuffer();
|
||||||
const view = new DataView(buffer);
|
const view = new DataView(buffer);
|
||||||
|
|
||||||
const format = detectAudioFormat(view, blob.type);
|
const format = detectAudioFormat(view, blob.type);
|
||||||
|
|
||||||
if (format === 'mp4') return 'm4a';
|
if (format === 'mp4') return 'm4a';
|
||||||
if (format) return format;
|
if (format) return format;
|
||||||
|
|
||||||
// Default fallback
|
// Default fallback
|
||||||
return 'flac';
|
return 'flac';
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue