我想根据用户发送的数据来设置mp3文件的作者。
到目前为止,我设法获得了用户发送的数据(无论如何还是很难被利用),但是我无法改变文件的作者。根据{{3}}的建议,我尝试同时使用node-id3软件包和ffmetadata软件包,但这都没有用。
node-id3方法
这是我为node-id3方法编写的代码的一部分,而readTags
中显示的标记确实是我在update
方法中添加的标记,但并非如此当我在计算机(带iTunes)或Android手机(带三星音乐)上阅读文件时,更改文件的作者,这意味着这种方法不起作用。
const NodeID3 = require('node-id3')
//getting the filename and fileAuthor, as well as creating the file on the server
let tags = {
title: filename,
composer: fileAuthor,
artist: fileAuthor,
remixArtist: fileAuthor,
conductor: fileAuthor,
originalArtist: fileAuthor,
}
let success = NodeID3.update(tags, toBeDownloadedFilePath)
console.log(success)
let readTags = NodeID3.read(toBeDownloadedFilePath)
console.log(readTags)
ffmetadata方法
这是用ffmetadata方法编写的同一部分:
const ffmetadata = require("ffmetadata");
//getting the filename and fileAuthor, as well as creating the file on the server
let tags = {
artist: fileAuthor,
}
ffmetadata.write(toBeDownloadedFilePath, tags, function(err) {
if (err) console.error("Error writing metadata", err);
else console.log("Data written");
});
使用这种方法,我得到了错误:
[mp3 @ 0x7f90f8000000]仅以低分1检出mp3格式。 [mp3 @ 0x7f90f8000000]无法读取帧大小:找不到1026。 music1.mp3:无效的参数
(music1.mp3
是我的文件名),并且我测试过的所有音频阅读器都可以完美识别我的mp3文件。
非常感谢您的帮助。
答案 0 :(得分:0)
所以我终于找到了问题所在(至少使用node-id3方法):
为了更好地理解它,我将在在服务器上创建文件步骤中添加一些细节。
这是我无法运行的代码:
const NodeID3 = require('node-id3')
const fs = require('fs-extra'); // file system
fs.ensureFileSync(toBeDownloadedFilePath); //because I need to create the file if it doesn't exist
const toBeDownloadedFile = fs.createWriteStream(toBeDownloadedFilePath);
//the way the audio stream is created isn't relevant, but if you're interested, it's a youtube stream, as per https://www.npmjs.com/package/youtube-audio-stream
let fileWriteStream = audioStream.pipe(toBeDownloadedFile)
let tags = {
title: filename,
composer: fileAuthor,
artist: fileAuthor,
remixArtist: fileAuthor,
conductor: fileAuthor,
originalArtist: fileAuthor,
}
let success = NodeID3.update(tags, toBeDownloadedFilePath)
console.log(success)
let readTags = NodeID3.read(toBeDownloadedFilePath)
console.log(readTags)
问题是我的标签已被写入,但立即被audiostream.pipe
因此,解决方案非常简单,我最终得到了以下代码:
const NodeID3 = require('node-id3')
const fs = require('fs-extra'); // file system
fs.ensureFileSync(toBeDownloadedFilePath); //because I need to create the file if it doesn't exist
const toBeDownloadedFile = fs.createWriteStream(toBeDownloadedFilePath);
let fileWriteStream = audiSstream.pipe(toBeDownloadedFile)
fileWriteStream.on('finish', () => {
let tags = {
title: filename,
artist: fileAuthor,
}
NodeID3.update(tags, toBeDownloadedFilePath)
//any additional action, in my case, send the file for a download
})
希望这可以帮助有类似问题的人。