mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Use parseFile for audio file paths instead of converting to stream (#1529)
When extracting duration from an audio file provided as a file path, use music-metadata's parseFile method instead of manually creating a read stream and using parseStream. parseFile utilizes random access, which can significantly improve performance over streaming in many cases. However, actual speed gains depend on the file format and the structure of tag headers. Co-authored-by: Borewit <Borewit@users.noreply.github.com>
This commit is contained in:
@@ -184,17 +184,15 @@ export const mediaMessageSHA256B64 = (message: WAMessageContent) => {
|
||||
export async function getAudioDuration(buffer: Buffer | string | Readable) {
|
||||
const musicMetadata = await import('music-metadata')
|
||||
let metadata: IAudioMetadata
|
||||
const options = {
|
||||
duration: true
|
||||
}
|
||||
if (Buffer.isBuffer(buffer)) {
|
||||
metadata = await musicMetadata.parseBuffer(buffer, undefined, { duration: true })
|
||||
metadata = await musicMetadata.parseBuffer(buffer, undefined, options)
|
||||
} else if (typeof buffer === 'string') {
|
||||
const rStream = createReadStream(buffer)
|
||||
try {
|
||||
metadata = await musicMetadata.parseStream(rStream, undefined, { duration: true })
|
||||
} finally {
|
||||
rStream.destroy()
|
||||
}
|
||||
metadata = await musicMetadata.parseFile(buffer, options)
|
||||
} else {
|
||||
metadata = await musicMetadata.parseStream(buffer, undefined, { duration: true })
|
||||
metadata = await musicMetadata.parseStream(buffer, undefined, options)
|
||||
}
|
||||
|
||||
return metadata.format.duration
|
||||
|
||||
Reference in New Issue
Block a user