Example #1
0
 public function run(Message $message, Discord $discord, WebSocket $webSocket, Logger $log, &$audioStreams, Channel $channel, cURL $curl)
 {
     $exp = explode(" ", $message->content);
     unset($exp[0]);
     $youtubeLink = implode(" ", $exp);
     // URL Checker
     $parts = parse_url($youtubeLink);
     if (!stristr($parts["host"], "youtube.com")) {
         return $message->reply("Error, you can only use youtube links!");
     }
     // Generate song md5
     $md5 = md5($youtubeLink);
     // Now get the mp3 from the cache
     $songFile = __DIR__ . "/../../../../../cache/songs/{$md5}.mp3";
     $dl = new YoutubeDl(["extract-audio" => true, "audio-format" => "mp3", "audio-quality" => 0, "output" => $songFile]);
     $title = "";
     try {
         $video = $dl->download($youtubeLink);
         $title = $video->getTitle();
         $log->addNotice("Downloading {$title} from YouTube");
     } catch (NotFoundException $e) {
         $log->addError("Error: the song was not found: {$e->getMessage()}");
         $message->reply("Error: the song was not found: {$e->getMessage()}");
     } catch (PrivateVideoException $e) {
         $log->addError("Error: song has been made private: {$e->getMessage()}");
         $message->reply("Error: song has been made private: {$e->getMessage()}");
     } catch (CopyrightException $e) {
         $log->addError("Error: song is under copyright: {$e->getMessage()}");
         $message->reply("Error: song is under copyright: {$e->getMessage()}");
     } catch (\Exception $e) {
         $log->addError("Error: {$e->getMessage()}");
         $message->reply("Error: {$e->getMessage()}");
     }
     $webSocket->joinVoiceChannel($channel)->then(function (VoiceClient $vc) use($message, $discord, $webSocket, $log, &$audioStreams, $channel, $curl, $songFile, $title) {
         $guildID = $message->getChannelAttribute()->guild_id;
         if (file_exists($songFile)) {
             // Add this audio stream to the array of audio streams
             $audioStreams[$guildID] = $vc;
             $vc->setFrameSize(40)->then(function () use($vc, &$audioStreams, $guildID, $songFile, $log, $message, $title, $channel) {
                 $vc->setBitrate(128000);
                 $message->reply("Now playing **{$title}** in {$channel->name}");
                 $vc->playFile($songFile, 2)->done(function () use($vc, &$audioStreams, $guildID) {
                     unset($audioStreams[$guildID]);
                     $vc->close();
                 });
             });
         }
     });
 }
 /**
  * @expectedException \Symfony\Component\Process\Exception\ProcessFailedException
  */
 public function testBadUrlVideoDownload()
 {
     $yt = new YoutubeDl(['skip-download' => true]);
     $yt->setDownloadPath('/');
     $yt->download('https://www.example.com');
 }
Example #3
0
<?php

include 'vendor/autoload.php';
use YoutubeDl\YoutubeDl;
use YoutubeDl\Exception\CopyrightException;
use YoutubeDl\Exception\NotFoundException;
use YoutubeDl\Exception\PrivateVideoException;
// $a = new SimpleXMLElement;
// exit;
$dl = new YoutubeDl(['format' => 'best', 'simulate' => true]);
// For more options go to https://github.com/rg3/youtube-dl#user-content-options
// $dl->setDownloadPath('/home/user/downloads');
// $dl->debug(function ($type, $buffer) {
//     if (\Symfony\Component\Process\Process::ERR === $type) {
//         echo 'ERR > ' . $buffer;
//     } else {
//         echo 'OUT > ' . $buffer;
//     }
// });
try {
    $video = $dl->download('https://www.youtube.com/watch?v=oDAw7vW7H0c');
    echo $video->getUrl();
    echo $video->getTitle();
    // var_dump($video);
    // $dl->getFile(); // \SplFileInfo instance of downloaded file
} catch (NotFoundException $e) {
    // Video not found
} catch (PrivateVideoException $e) {
    // Video is private
} catch (CopyrightException $e) {
    // The YouTube account associated with this video has been terminated due to multiple third-party notifications of copyright infringement
 /**
  * @expectedException \YoutubeDl\Exception\CopyrightException
  */
 public function testYoutubeRemovedVideoDownload()
 {
     $obj = new YoutubeDl(['skip-download' => true]);
     $obj->download('https://www.youtube.com/watch?v=AYeiLa_F8fk');
 }