如何获取目录中的所有音频MP3文件,并将MP3的长度和文件大小获取出来。直接看代码
const fs = require('fs'), path = require('path') var ffmpeg = require('ffmpeg') // 音频目录 const mp3DirPath = '/Users/administrator/Desktop/' let files = fs.readdirSync(mp3DirPath) /** * 获取音频文件的信息 */ async function getMP3Info(mp3Path) { let stat = fs.statSync(mp3Path) var process = await new ffmpeg(mp3Path) let metadata = process.metadata let duration = metadata.duration.seconds let size = stat.size return { duration, size } } async function main() { let mp3InfoMap = {} for (let i = 0; i < files.length; i++) { let filename = files[i] // 获取音频文件的文件名,不包括后缀 let name = path.basename(filename, '.mp3') let url = mp3DirPath + filename let {duration, size} = await getMP3Info(url) mp3InfoMap[name] = { duration, size, filename:filename } console.log(filename,duration,size) } } main()