【发布时间】:2015-07-09 18:50:14
【问题描述】:
我使用 node youtube-dl modul 和 Meteor.js 来获取有关 youtube 视频的信息,因为我正在学习使用 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)
编辑:
我终于解决了。而不是node-youtube-dl,我使用基本的python youtube-dl 和how to execute unix command with Meteor 上的本教程。
在服务器上获取视频描述的示例代码如下:
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);
}
});
【问题讨论】:
标签: javascript node.js meteor youtube-dl