简体   繁体   English

在启动时使用Meteor FS集合将wav转换为mp3

[英]Convert wav to mp3 using Meteor FS Collections on Startup

I'm trying to transcode all wav files into a mp3 using Meteor and Meteor FS Collections. 我正在尝试使用Meteor和Meteor FS Collections将所有wav文件转码为mp3。 My code works when I upload a wav file to the uploader -- That is it will convert the wav to a mp3 and allow me to play the file. 当我将wav文件上传到上传器时,我的代码有效-也就是说,它将wav转换为mp3,并允许我播放该文件。 But, I'm looking for a Meteor Solution that will transcode and add the file to the DB if the file is a wav and exist in a certain directory. 但是,我正在寻找一种Meteor解决方案,如果该文件是wav并且存在于某个目录中,则该文件将转码并将其添加到DB中。 According to the Meteor FSCollection it should be possible if the files have already been stored. 根据Meteor FSCollection,如果文件已经存储,则应该是可能的。 Here is their example code: *GM is for ImageMagik, I've replaced gm with ffmpeg and installed ffmpeg from atmosphereJS. 这是他们的示例代码:* GM适用于ImageMagik,我已经用ffmpeg替换了gm,并从moatnessJS安装了ffmpeg。

Images.find().forEach(function (fileObj) {
  var readStream = fileObj.createReadStream('images');
  var writeStream = fileObj.createWriteStream('images');
  gm(readStream).swirl(180).stream().pipe(writeStream);
});

I'm using Meteor-CollectionFS [ https://github.com/CollectionFS/Meteor-CollectionFS]- 我正在使用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);
    });
      });
      });
}

And here is my FS.Collection and FS.Store information. 这是我的FS.Collection和FS.Store信息。 Currently everything resides in one JS file. 当前,所有内容都驻留在一个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);
      }
    })]
});

When I try and insert the file into the db on the server side I get this error: MongoError: E11000 duplicate key error index: 当我尝试将文件插入服务器端的db时,出现以下错误:MongoError:E11000重复键错误索引:

Otherwise, If I drop a wav file into the directory and restart the server, nothing happens. 否则,如果将wav文件放入目录中并重新启动服务器,则不会发生任何事情。 I'm new to meteor, please help. 我是新来的流星,请帮忙。 Thank you. 谢谢。

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. 您正在尝试插入具有相同(重复)ID的下一个对象,在这里您应该首先“擦除”该ID或只是更新文档而不是添加新的ID。 If you not provide the _id field, it will be automatically added. 如果不提供_id字段,它将自动添加。

delete fileObj._id;
Wavs.insert(fileObj, function(error, result) {
}); 

See this How do I remove a property from a JavaScript object? 请参阅此如何从JavaScript对象中删除属性?

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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM