Skip to content

Commit

Permalink
moved ntsuspend to optionalDependencies
Browse files Browse the repository at this point in the history
Added Infinity to codec-data blocker
  • Loading branch information
Mattk70 committed Oct 29, 2024
1 parent af80530 commit 55d170a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
82 changes: 58 additions & 24 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {trackEvent} from './tracking.js';
import {extractWaveMetadata} from './metadata.js';
let isWin32 = false;

const ntsuspend = require('ntsuspend');
let ntsuspend;
if (process.platform === 'win32') {
ntsuspend = require('ntsuspend');
isWin32 = true;
}
const DEBUG = true;
const DEBUG = false;

// Function to join Buffers and not use Buffer.concat() which leads to detached ArrayBuffers
function joinBuffers(buffer1, buffer2) {
Expand Down Expand Up @@ -1061,28 +1062,59 @@ function onAbort({

}

const getDuration = async (src) => {
let audio;
return new Promise(function (resolve, reject) {
audio = new Audio();
audio.src = src.replaceAll('#', '%23').replaceAll('?', '%3F'); // allow hash and ? in the path (https://github.com/Mattk70/Chirpity-Electron/issues/98)
audio.addEventListener("loadedmetadata", function () {
const duration = audio.duration;
audio = undefined;
// Tidy up - cloning removes event listeners
const old_element = document.getElementById("audio");
const new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);
// const getDuration = async (src) => {
// let audio;
// return new Promise(function (resolve, reject) {
// audio = new Audio();
// audio.src = src.replaceAll('#', '%23').replaceAll('?', '%3F'); // allow hash and ? in the path (https://github.com/Mattk70/Chirpity-Electron/issues/98)
// audio.addEventListener("loadedmetadata", function () {
// const duration = audio.duration === Infinity ? Number.MAX_SAFE_INTEGER : audio.duration;
// audio = undefined;
// // Tidy up - cloning removes event listeners
// const old_element = document.getElementById("audio");
// const new_element = old_element.cloneNode(true);
// old_element.parentNode.replaceChild(new_element, old_element);

resolve(duration);
});
audio.addEventListener('error', (error) => {
generateAlert({type: 'error', message: 'Unable to extract essential metadata from ' + src})
reject(error, src)
})
// resolve(duration);
// });
// audio.addEventListener('error', (error) => {
// generateAlert({type: 'error', message: 'Unable to extract essential metadata from ' + src})
// reject(error, src)
// })
// });
// }
const getDuration = async (src) => {
return new Promise((resolve, reject) => {
// Replace special characters in the file path as needed
// const formattedSrc = src.replaceAll('#', '%23').replaceAll('?', '%3F');
try {
const command = ffmpeg('file:' + src)
.format(null)
.save('null.wav')
.on('codecData', (data) => {
let duration;
if ( ! ['N/A', Infinity].includes(data.duration)) {
// Update Metadata with accurate duration
const [hours, minutes, seconds] = data.duration.split(':').map(parseFloat);
duration = (hours * 3600) + (minutes * 60) + seconds;
} else {
duration = Number.MAX_SAFE_INTEGER;
}
console.log(duration , 'seconds')
resolve(duration);
command.kill(); // Stop processing after getting the duration
})
.on('error', (error) => {
if (!error.message.includes('ffmpeg was killed')){
generateAlert({ type: 'error', message: 'Unable to extract essential metadata from ' + src });
reject(error);
}
})
} catch (error) {
console.log(error)
}
});
}

};

/**
* getWorkingFile's purpose is to locate a file and set its metadata.
Expand Down Expand Up @@ -1239,7 +1271,9 @@ const setMetadata = async ({ file, source_file = file }) => {
// savedMeta may just have a locationID if it was set by onSetCUstomLocation
if (! savedMeta?.duration) {
try {
console.time('get duration')
METADATA[file].duration = await getDuration(file)
console.timeEnd('get duration')
} catch (e) {
throw new Error('Unable to determine file duration ', e);
}
Expand Down Expand Up @@ -1630,11 +1664,11 @@ function processAudio (file, start, end, chunkStart, highWaterMark, samplesInBat
checkBacklog(command)
})
command.on('codecData', function(data) {
if (data.duration !== 'N/A') {
if ( ! ['N/A', Infinity].includes(data.duration)) {
// Update Metadata with accurate duration
const [hours, minutes, seconds] = data.duration.split(':').map(parseFloat);
const totalSeconds = (hours * 3600) + (minutes * 60) + seconds;
METADATA[file].duration = totalSeconds
METADATA[file].duration = totalSeconds;
}
})
const STREAM = command.pipe();
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@
"fast-png": "^6.1.0",
"fluent-ffmpeg": "^2.1.2",
"lodash.merge": "^4.6.2",
"ntsuspend": "^1.0.2",
"p-limit": "3.1.0",
"sqlite3": "5.1.6",
"suncalc": "^1.9.0",
"utimes": "5.2.1",
"uuid": "^8.3.2",
"wavefile-reader": "^1.1.1",
"wavesurfer.js": "6.6.4"
},
"optionalDependencies": {
"ntsuspend": "^1.0.2"
}
}

0 comments on commit 55d170a

Please sign in to comment.