46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers;
 | |
| 
 | |
| use Illuminate\Http\Request;
 | |
| use YoutubeDl\Metadata\DefaultMetadataReader;
 | |
| use YoutubeDl\Options;
 | |
| use YoutubeDl\YoutubeDl;
 | |
| 
 | |
| class YoutubeDownloadController extends Controller
 | |
| {
 | |
|     public function index(Request $request)
 | |
|     {
 | |
|         ini_set('max_execution_time', env('MAX_EXECUTION_TIME'));
 | |
|         $url = $request->get('videourl');
 | |
|         $yt = new YoutubeDl();
 | |
|         $collection = $yt->download(
 | |
|             Options::create()
 | |
|                 ->downloadPath(env('PATH_TO_DOWNLOAD'))
 | |
|                 ->url($url)
 | |
|                 ->format('bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio')
 | |
|                 ->mergeOutputFormat('mp4')
 | |
|         );
 | |
|         $videotitle = '';
 | |
|         foreach ($collection->getVideos() as $video) {
 | |
|             if ($video->getError() !== null) {
 | |
|                 $error = "Error downloading video: {$video->getError()}.";
 | |
|                 $response = new \stdClass();
 | |
|                 $response->error = $error;
 | |
|                 return response(json_encode($response));
 | |
|             } else {
 | |
|                 $videotitle = $video->getFile()->getFilename();
 | |
|                 $thumbnails = $video->getThumbnails();
 | |
|                 $thumbnail = $thumbnails[0]?->getUrl();
 | |
|                 $response = new \stdClass();
 | |
|                 $response->link = $videotitle;
 | |
|                 $response->thumbnail = $thumbnail;
 | |
|                 return response(json_encode($response));
 | |
|             }
 | |
|         }
 | |
|         $response = new \stdClass();
 | |
|         $response->error = 'No video';
 | |
|         return response(json_encode($response));
 | |
|     }
 | |
| }
 |