我正在尝试使用Meteor和Meteor FS Collections将所有wav文件转码为mp3。当我将wav文件上传到上传器时,我的代码可以工作 - 也就是说它会将wav转换为mp3并允许我播放该文件。但是,我正在寻找一个流星解决方案,如果文件是一个wav并存在于某个目录中,它将转码并将文件添加到数据库中。根据Meteor FSCollection,如果文件已经存储,应该是可能的。这是他们的示例代码:* GM用于ImageMagik,我用ffmpeg替换gm并从atmosphereJS安装了ffmpeg。
Images.find().forEach(function (fileObj) {
var readStream = fileObj.createReadStream('images');
var writeStream = fileObj.createWriteStream('images');
gm(readStream).swirl(180).stream().pipe(writeStream);
});
我正在使用Meteor-CollectionFS [https://github.com/CollectionFS/Meteor-CollectionFS]-
if (Meteor.isServer) {
Meteor.startup(function () {
Wavs.find().forEach(function (fileObj) {
var readStream = fileObj.createReadStream('.wavs/mp3');
var writeStream = fileObj.createWriteStream('.wavs/mp3');
this.ffmpeg(readStream).audioCodec('libmp3lame').format('mp3').pipe(writeStream);
Wavs.insert(fileObj, function(err) {
console.log(err);
});
});
});
}
这是我的FS.Collection和FS.Store信息。目前,所有内容都存在于一个JS文件中。
Wavs = new FS.Collection("wavs", {
stores: [new FS.Store.FileSystem("wav"),
new FS.Store.FileSystem("mp3",
{
path: '~/wavs/mp3',
beforeWrite: function(fileObj) {
return {
extension: 'mp3',
fileType: 'audio/mp3'
};
},
transformWrite: function(fileObj, readStream, writeStream) {
ffmpeg(readStream).audioCodec('libmp3lame').format('mp3').pipe(writeStream);
}
})]
});
当我尝试将文件插入服务器端的数据库时,我收到此错误:MongoError:E11000重复键错误索引:
否则,如果我将wav文件放入目录并重新启动服务器,则没有任何反应。我是流星的新手,请帮忙。谢谢。
答案 0 :(得分:1)
Error is clear. You're trying to insert a next object with this same (duplicated) id, here you should first 'erase' the id or just update the document instead of adding the new one. If you not provide the _id field, it will be automatically added.
delete fileObj._id;
Wavs.insert(fileObj, function(error, result) {
});
See this How do I remove a property from a JavaScript object?
Why do you want to convert the files only on startup, I mean only one time? Probably you want to do this continuously, if yes then you should use this:
Tracker.autorun(function(){
//logic
});