本文介绍了如何使用python youtube-dl和meteorjs执行客户端视频下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的流星应用程序中,我使用 youtube-dl 从 youtube 和其他网站下载视频;实际上,我可以使用下面的代码从服务器端获取有关视频的信息

In my meteor app, i use youtube-dl to download video from youtube and others websites;actually, i can get information about the video from server side using code below

 var exec = Meteor.npmRequire('child_process').exec;
  var Future = Meteor.npmRequire("fibers/future");

Meteor.methods({
    'comman': function(url){
      this.unblock();
      var future = new Future();

      exec("youtube-dl -F " + url, function(error, stdout, stderr) {

        future.return({stdout: stdout, stderr: stderr});
      });

      return future.wait();
    }
});

以及对客户端的调用

Meteor.call('comman', url, function(error, result){
        if (result.stdout) {
          console.log('output:' + result.stdout);

       } else {
          console.log('error:'+ result.stderr);
       }
      });

下一步是用户可以看到视频的缩略图,可用格式列表,可以从客户端下载和保存他想要的格式;

The next step is that the user can see thumbnail of the video, the list of formats available, be able to download and save the format he wants from the client side;

我该怎么做??有什么想法吗?

How can I perform that??Any ideas?

感谢您的帮助

推荐答案

你必须添加这个包 https://大气js.com/meteorhacks/npm 到您的 Meteor 应用程序,以便能够使用 Npm 包.

You have to add this package https://atmospherejs.com/meteorhacks/npm to your Meteor app to be able to use Npm packages.

然后使用 https://www.npmjs.com/package/ytdl-core 使用 youtube-dl 代替 exec 命令,它更容易.为了使用它,你必须添加到你的主文件夹中的packages.json:

Then use https://www.npmjs.com/package/ytdl-core instead of exec command with youtube-dl, it's easier. In order to use it, you have to add to packages.json in your main folder:

{
  "ytdl-core":"0.7.9",
}

现在在 server.js 上使用 ydtl-core 示例创建一个方法.您需要使用 Futures 等待响应并将其返回给客户端:

Now on server.js create a method using a ydtl-core example. You need to use Futures to wait for a response and return it back to client:

// load future from fibers
var Future = Meteor.npmRequire("fibers/future");

// load ytdl-core
var ytdl = Meteor.npmRequire('ytdl-core');

Meteor.methods({
  // Get info from Youtube video
  'getVideoInfo':function(videoUrl) {
    this.unblock();
    var future = new Future();

    ytdl.getInfo(videoUrl, function(err, result) {
      future.return(result)
    });
    return future.wait();
  },
});

在 client.js 上:

On client.js:

var videoUrl = "http://youtube.com/watch?v=I8qtzxpDM4k";

Meteor.call('getVideoInfo', videoUrl, function(err, result) {
  console.log(result);
  console.log (result.thumbnail_url);
}

要下载视频,您可以创建一个链接并使用 javascript 单击它.您必须选择要下载的格式.在这个例子中,我下载第一个可用的格式.

To download video you can create a link and click it using javascript. You have to pick format which you want to download. In this example I'm downloading first available format.

在 client.js 上

on client.js

var videoUrl = "http://youtube.com/watch?v=I8qtzxpDM4k";

Meteor.call('getVideoInfo', videoUrl, function(err, result) {
  console.log(result);
  console.log (result.thumbnail_url);
  downloadLink(result.formats[0].url);
}

downloadLink = function(link) {
  //Create url to download from
  var url = document.createElement('a');
  // Add direct video link to a clickable link
  url.setAttribute('href', link);
  // Add download attribute to initiate download
  url.setAttribute('download', 'filename.mp4');
  // Click to start download
  url.click();
}

这篇关于如何使用python youtube-dl和meteorjs执行客户端视频下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 08:52