youtube-dl-php icon indicating copy to clipboard operation
youtube-dl-php copied to clipboard

Add support for async download

Open jaspervdm opened this issue 7 years ago • 6 comments

I added functionality to download youtube videos asynchronized, using react event loop and promises. Example code:

$loop = \React\EventLoop\Factory::create();
$timer = $loop->addPeriodicTimer(1, function () {
  static $i = 0;
  echo "Loop ".++$i."\n";
});
$loop->addTimer(1, function () use ($timer, $loop) {
  $dl = new \YoutubeDl\YoutubeDl([
    "extract-audio" => true,
    "audio-format" => "mp3",
    "audio-quality" => 0,
    "output" => "%(id)s.%(ext)s"
  ]);
  $dl->downloadAsync("URL", $loop)->then(function (\YoutubeDl\Entity\Video $video) use ($timer) {
    echo "Done, ".$video->getFilename()."\n";
    $timer->cancel();
  }, function (Exception $e) use ($timer) {
    echo get_class($e).": ".$e->getMessage()."\n";
    $timer->cancel();
  });
});
$loop->run();

jaspervdm avatar Aug 04 '17 16:08 jaspervdm

I don't think that this should be in core because I want this lib to be lightweight :thinking:

norkunas avatar Sep 05 '17 05:09 norkunas

Alright, thats fine by me, I am closing the PR. If anyone in the future is reading this, you can use this feature by using these lines in your composer.json:

{
  "require": {
    "norkunas/youtube-dl-php": "dev-async",
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/jaspervdm/youtube-dl-php"
    }
  ]
}

jaspervdm avatar Sep 05 '17 07:09 jaspervdm

@jaspervdm actually we could provide this method but just adding these dependencies to the composer suggest and if people would try async method and the dependencies wouldn't be installed then we could just throw exception

norkunas avatar Sep 06 '17 06:09 norkunas

Okay, I will modify the code to check for the dependencies.

jaspervdm avatar Sep 08 '17 13:09 jaspervdm

I made the async functionality optional now, what do you think?

jaspervdm avatar Sep 18 '17 17:09 jaspervdm

@jaspervdm are you still interested to finish this?

norkunas avatar Sep 07 '20 11:09 norkunas