public function run() { $this->output->writeln("Downloading video: " . $this->video->getId()); $this->logger->addInfo("START Downloading video: " . $this->video->getId()); if ($this->video->getDownloaded()) { $this->output->writeln("<info>Video already downloaded... Continue...</info>"); $this->logger->addInfo("END Skipping video: " . $this->video->getId() . " -> Already exists!"); return; } $builder = new ProcessBuilder(array('youtube-dl', '-x', '--audio-format', 'mp3', '--output', $this->webDir . "/media/" . "%(id)s.%(ext)s", '--cache-dir', $this->appDir . "/ytdl_cache/", 'https://youtube.com/watch?v=' . $this->video->getId())); $process = $builder->getProcess(); $process->setTimeout(0); $logger = $this->logger; $output = $this->output; $process->run(function ($type, $buffer) use($logger, $output) { if (Process::ERR === $type) { $output->writeln("<error>{$buffer}</error>"); $logger->addError("YOUTUBE-DL: " . $buffer); } else { $logger->addDebug("YOUTUBE-DL: " . $buffer); } }); $this->logger->addDebug("Video " . $this->video->getId() . ": Exitcode " . $process->getExitCode()); if ($process->getExitCode() === 0) { $this->video->setDownloaded(true); $output->writeln("<info>Video successfully downloaded!</info>"); $this->logger->addInfo("Video " . $this->video->getId() . ": Downloaded successfull"); } elseif ($process->getExitCode() === 127) { $this->video->setDownloaded(false); $output->writeln("<error>youtube-dl isn't installed!</error>"); $this->logger->addError("youtube-dl isn't available!"); } else { $this->video->setDownloaded(false); $output->writeln("<error>Video downloading error! See logs.</error>"); $this->logger->addError("Video " . $this->video->getId() . ": Unknown error!"); } $this->videoWrapper->saveVideo($this->video); }
public function saveVideo(Video $video) { $v = $this->getVideoById($video->getId()); if ($v === null) { $stmt = $this->db->prepare("INSERT INTO videos (id, title, description, published, downloaded) VALUES (:id, :title, :desc, :published, :downloaded)"); } else { $stmt = $this->db->prepare("UPDATE videos SET title = :title, description = :desc, published = :published, downloaded = :downloaded WHERE id IS :id"); } $stmt->bindValue("id", $video->getId(), \PDO::PARAM_STR); $stmt->bindValue("title", $video->getTitle(), \PDO::PARAM_STR); $stmt->bindValue("desc", $video->getDescription(), \PDO::PARAM_STR); $stmt->bindValue("published", $video->getPublished()->format("Y-m-d H:i:s"), \PDO::PARAM_STR); $downloaded = $video->getDownloaded() == true ? 1 : 0; $stmt->bindValue("downloaded", $downloaded, \PDO::PARAM_INT); $stmt->execute(); }