Skip to content

Commit

Permalink
stories audio conversion optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck ALARY committed Feb 22, 2024
1 parent 82a150f commit 9954b31
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
56 changes: 51 additions & 5 deletions public/MainEvents/Processes/BinFiles/FFmpegCommand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn } from 'child_process'
import { getBinPath } from '../Helpers/AppPaths.js'
import * as fs from 'fs'

const
getFFmpegFileName = () => {
Expand All @@ -8,10 +9,27 @@ const
getFFmpegFilePath = () => {
return getBinPath(getFFmpegFileName())
},
convertAudioToMp3 = (srcFile, dstMp3) => {
getAudioInfos = (srcFile) => {
return new Promise((resolve, reject) => {
const stream = spawn(getFFmpegFilePath(), ['-i', srcFile])
let data = ''
stream.stderr.on('data', (d) => {
data += d.toString()
})
stream.on('close', () => {
const infos = data.toString().match(/Stream #[0-9]:[0-9]: Audio: ([A-Za-z0-9,/ ]+)/i)
if (infos === null || infos.length < 2) {
reject('audio-infos-not-found')
} else {
resolve(infos[1].toLowerCase().split(', '))
}
})
})
},
ffmpegAudioToMp3 = (srcFile, dstMp3) => {
return new Promise((resolve, reject) => {
const stream = spawn(getFFmpegFilePath(), ['-i', srcFile, '-map_metadata', '-1', '-map_chapters', '-1', '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', dstMp3])
stream.on('close', code => {
stream.on('close', (code) => {
if (code === 0) {
resolve()
} else {
Expand All @@ -20,10 +38,37 @@ const
})
})
},
convertAudioToMp3 = (srcFile, dstMp3, forceConverting) => {
if (!forceConverting) {
return new Promise((resolve, reject) => {
getAudioInfos(srcFile)
.then((infos) => {
if (infos[0] === 'mp3' && infos[1] === '44100 hz' && (infos[2] === 'stereo' || infos[2] === 'mono')) {
try {
fs.copyFileSync(srcFile, dstMp3)
resolve()
} catch (ignored) {
reject()
}
} else {
ffmpegAudioToMp3(srcFile, dstMp3)
.then(() => resolve())
.catch(() => reject())
}
})
.catch((e) => {
ffmpegAudioToMp3(srcFile, dstMp3)
.then(() => resolve())
.catch(() => reject())
})
})
}
return ffmpegAudioToMp3(srcFile, dstMp3)
},
convertImageToPng = (srcFile, dstPng, width, height) => {
return new Promise((resolve, reject) => {
const stream = spawn(getFFmpegFilePath(), ['-i', srcFile, '-vf', 'scale=' + width + 'x' + height + ':flags=bilinear', dstPng])
stream.on('close', code => {
stream.on('close', (code) => {
if (code === 0) {
resolve()
} else {
Expand All @@ -35,7 +80,7 @@ const
extractMetadataFromMp3 = (srcFile, dstTxt) => {
return new Promise((resolve, reject) => {
const stream = spawn(getFFmpegFilePath(), ['-i', srcFile, '-f', 'ffmetadata', dstTxt])
stream.on('close', code => {
stream.on('close', (code) => {
if (code === 0) {
resolve()
} else {
Expand All @@ -47,7 +92,7 @@ const
extractPngFromMp3 = (srcFile, dstPng) => {
return new Promise((resolve, reject) => {
const stream = spawn(getFFmpegFilePath(), ['-i', srcFile, '-filter:v', 'scale=256x256', '-an', dstPng])
stream.on('close', code => {
stream.on('close', (code) => {
if (code === 0) {
resolve()
} else {
Expand All @@ -60,6 +105,7 @@ const
export {
getFFmpegFileName,
getFFmpegFilePath,
getAudioInfos,
convertAudioToMp3,
convertImageToPng,
extractMetadataFromMp3,
Expand Down
3 changes: 2 additions & 1 deletion public/MainEvents/Processes/Import/ConvertFolderSTUdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ function convertFolderSTUdio (srcPath, storyName) {
}
fs.writeFileSync(path.join(dstPath, 'metadata.json'), JSON.stringify(metadata))
process.stdout.write('success')
}
},
false
)
)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion public/MainEvents/Processes/Import/ConvertMusic.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function convertMusic (srcPath) {
return
}

convertAudio(srcPath, musicDstPath)
convertAudio(srcPath, musicDstPath, true)
.then(() => {
process.stdout.write('*music-searching-cover*2*3*')

Expand Down
12 changes: 6 additions & 6 deletions public/MainEvents/Processes/Import/Helpers/AudioFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const
return ext === '.mp3' || ext === '.ogg' || ext === '.flac' || ext === '.wav' || ext === '.aac'
},

convertAudio = async (fromPath, toPath) => {
await convertAudioToMp3(fromPath, toPath)
convertAudio = async (fromPath, toPath, forceConverting) => {
await convertAudioToMp3(fromPath, toPath, forceConverting)
},

convertAudios = (srcAudios, dstAudios, index, length, onEnd) => {
convertAudios = (srcAudios, dstAudios, index, length, onEnd, forceConverting) => {
if (!srcAudios.length) {
onEnd(index)
return
Expand All @@ -24,9 +24,9 @@ const

process.stdout.write('*converting-audio*' + index + '*' + length + '*')

convertAudio(srcAudio, dstAudio)
.then(() => convertAudios(srcAudios, dstAudios, index + 1, length, onEnd))
.catch(() => convertAudios(srcAudios, dstAudios, index + 1, length, onEnd))
convertAudio(srcAudio, dstAudio, forceConverting)
.then(() => convertAudios(srcAudios, dstAudios, index + 1, length, onEnd, forceConverting))
.catch(() => convertAudios(srcAudios, dstAudios, index + 1, length, onEnd, forceConverting))
},

checkCoverExists = (artist, album, coverPath) => {
Expand Down

0 comments on commit 9954b31

Please sign in to comment.