使用带有MeteorJS的节点youtube-dl获取有关文件的信息

时间:2015-07-09 18:50:14

标签: javascript node.js meteor youtube-dl

我使用节点youtube-dl modul和Meteor.js获取有关youtube视频的信息,bc我正在学习与Meteor服务器端合作,但是我得到了这个错误我无法解决半个问题天。由于youtube-dl是npm模块,Meteor可以使用没有进一步定制的那些吗?

客户端代码:



if (Meteor.isClient) {
  Template.front.events({
    'click #buttondl': function () {
      // if submited link to input
      if (inputdl.value != '') {
        var link = inputdl.value;
        Meteor.call('information', link);
      }
    }
  });
}




服务器代码:



if (Meteor.isServer) {
  Meteor.methods({
    information: function (link) {
      var youtubedl = Meteor.require('youtube-dl');
      var url = youtubedl(link);

      youtubedl.getInfo(url, function(err, info) {
        if (err) throw err;

        console.log('id:', info.id);
        console.log('title:', info.title);
        console.log('url:', info.url);
        console.log('thumbnail:', info.thumbnail);
        console.log('description:', info.description);
        console.log('filename:', info._filename);
        console.log('duration:', info.duration);
        console.log('format_id:', info.format_id);
      });
    }
  });
}




我得到的错误:



W20150709-14:35:19.472(-4)? (STDERR) events.js:72
W20150709-14:35:19.472(-4)? (STDERR)         throw er; // Unhandled 'error' event
W20150709-14:35:19.472(-4)? (STDERR)               ^
W20150709-14:35:19.477(-4)? (STDERR) Error: Command failed:   File "/Users/matejhlavacka/node_modules/youtube-dl/bin/youtube-dl", line 2
W20150709-14:35:19.478(-4)? (STDERR) SyntaxError: Non-ASCII character '\xc4' in file /Users/matejhlavacka/node_modules/youtube-dl/bin/youtube-dl on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
W20150709-14:35:19.478(-4)? (STDERR) 
W20150709-14:35:19.478(-4)? (STDERR)     at ChildProcess.exithandler (child_process.js:658:15)
W20150709-14:35:19.478(-4)? (STDERR)     at ChildProcess.emit (events.js:98:17)
W20150709-14:35:19.478(-4)? (STDERR)     at maybeClose (child_process.js:766:16)
W20150709-14:35:19.478(-4)? (STDERR)     at Socket.<anonymous> (child_process.js:979:11)
W20150709-14:35:19.478(-4)? (STDERR)     at Socket.emit (events.js:95:17)
W20150709-14:35:19.479(-4)? (STDERR)     at Pipe.close (net.js:466:12)
&#13;
&#13;
&#13;

修改

我终于解决了它。而不是node-youtube-dl我在python youtube-dl上使用了基本的how to execute unix command with Meteor和本教程。

在服务器上获取视频描述的示例代码如下:

&#13;
&#13;
Meteor.methods({
    information: function (link) {

        exec = Npm.require('child_process').exec;

        runCommand = function (error, stdout, stderr) {
          console.log('stdout: ' + stdout);
          console.log('stderr: ' + stderr);

          if(error !== null) {
            console.log('exec error: ' + error);
          }
        }

        exec("youtube-dl --get-description " + link, runCommand);

    }
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

它失败可能是因为你有一个太旧的python版本,因为documented in the youtube-dl FAQ你需要python 2.6或更高版本。升级python应该可以解决问题。