<?php require '../autoloader.php'; $url = 'https://www.youtube.com/watch?v=oeCihv9A3ac'; $track = 'Eminem - Phenomenal'; $download = new YTDownloader\Service\Download($url); YTDownloader\Helper\Convert::$quality = "256K"; $file = $download->convert(); $file = YTDownloader::getDownloadPath() . $file; header('Content-type: audio/mpeg'); header('Content-length: ' . filesize($file)); header('Content-Disposition: attachment; filename="' . $track . '.mp3"'); header("Content-Transfer-Encoding: binary"); header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"); readfile($file);
public function downloadVideo($ytId, $coverUrl, $tagsData, $folder, $category) { $this->log("---- Start Convert Video {$ytId} to MP3"); if (empty($ytId)) { $this->log("---- Youtube Id is not null"); return false; } $videoLink = sprintf('https://www.youtube.com/watch?v=%s', $ytId); $crawInfo = isset(Yii::app()->params['prefixmp3'][$category]) ? Yii::app()->params['prefixmp3'][$category] : array('file' => '/var/www/phatammp3/prefixmp3/sachnoi.mp3', 'bitrate' => '320K'); $processInfo = array('url' => $videoLink, 'timeDownloadMp3' => 0, 'timeDownloadImage' => 0, 'timeMerge' => 0, 'timeWriteTag' => 0, 'imageAttach' => $coverUrl, 'fileMp3' => $tagsData->FileName); $startTime = time(); $prefixFile = $crawInfo['file']; $mp3Engine = new MP3Process(); /** * Download video file and convert to mp3 */ try { $this->log("-- Start Init Tool"); $converter = new YTDownloader($videoLink); $converter->setAudioFormat('mp3'); $converter->setFfmpegLogsActive(false); $converter->setDeleteVideo(false); $response = $converter->downloadAudio(); // $this->log($response); // $videoFile = new DownloadFile($response['videoPath']); // $mp3File = new DownloadFile($response['audioPath']); // $this->log("------ Checking duration mp3 file converted ". $mp3File->getDurationEstimate() ." and compare with video duration ".$videoFile->getDurationEstimate()); // if ($mp3File->getDurationEstimate() < $videoFile->getDurationEstimate()) { // $this->log("------ Download mp3 form video: " . $videoLink . " fail!"); // return false; // } if ($response == false) { $this->log("------ Download mp3 form video: " . $videoLink . " fail!"); return false; } $mp3TmpPath = $response['audioPath']; $videoTmpPath = $response['videoPath']; $this->log("------ Download MP3 Response " . $mp3TmpPath); $processInfo['timeDownloadMp3'] = time() - $startTime; /** * Download file img */ $responseImg = $converter->getVideoThumb(); if ($responseImg == false) { $this->log("------ Download Image: " . $coverUrl . " fail!"); return false; } $startTime = time(); $this->log("------ MP3 Response file size: " . FileUtils::getSize($mp3TmpPath)); if (FileUtils::getSize($mp3TmpPath) > 0) { /** * Process merge file mp3 */ $desFile = $folder . '/' . $tagsData->FileName; $fileIns = array($prefixFile, $mp3TmpPath); $this->log("------ Merge MP3 with prefix"); if ($this->mergeFile($desFile, $fileIns)) { //Delete file download unlink($mp3TmpPath); $processInfo['timeMerge'] = time() - $startTime; $startTime = time(); /** * Process write IDTag to mp3 */ $this->log("Image response " . $responseImg); $imageThumb = $converter->getDownloadsDir() . $converter->getThumb(); $this->log("Image Thumbs file size " . FileUtils::getSize($imageThumb)); if (FileUtils::getSize($imageThumb) > 0) { $this->log("load attach image " . $tagsData->loadAttachedPicture($imageThumb)); if ($tagsData->loadAttachedPicture($imageThumb)) { $processInfo['attachImage'] = 'success'; } else { $processInfo['attachImage'] = 'false'; } } if ($mp3Engine->WrigeIDTag($desFile, $tagsData)) { //Delete file image if (FileUtils::getSize($imageThumb) > 0) { unlink($imageThumb); } $processInfo['writeTag'] = 'success'; $processInfo['timeWriteTag'] = time() - $startTime; $this->log(json_encode($processInfo)); unlink($videoTmpPath); return $tagsData->FileName; } else { $processInfo['writeTag'] = 'fail'; $processInfo['timeWriteTag'] = time() - $startTime; } $this->log("------ Merge MP3 with prefix: DONE"); } else { $processInfo['merge'] = 'fail'; } } else { $processInfo['downloadmp3'] = 'fail'; } $this->log(json_encode($processInfo)); return false; } catch (Exception $e) { $this->log("------ Convert to MP3 Failed: " . $e->getMessage()); return false; } }