Search code examples
phphtmlyoutube-dl

YouTube Videos In HTML5 Player With YouTube-dl and PHP


I found PHP code for YouTube-DL like this

<?php
 
 define('YOUTUBE_DL', '/usr/local/bin/youtube-dl'); // find your youtube-dl path and replace with it
 $youtube_video = 'https://www.youtube.com/watch?v=e4Uq8O5ZhUA'; // replace with any youtube video
/**
 * Fetches direct URL of given youtube video
 */
function getDirectUrl($youtube_video) {
  // lets build command to get direct url via youtube-dl
  $video_json_command = YOUTUBE_DL.' -g '.$youtube_video;
  // get url
  $direct_url = shell_exec($video_json_command);
// remove any possible white spaces
  $direct_url = str_replace(array(' ',"\n"), '', $direct_url);
return $direct_url;
 }
function buildPlayer($direct_url) {
  echo '<video width="400" controls>
    <source src="'.$direct_url.'" type="video/mp4">
    Your browser does not support HTML5 video.
  </video>';
 }
function playVideo($youtube_video) {
  $url = getDirectUrl($youtube_video);
  buildPlayer($url);
 }
// call below function and play any video
 playVideo($youtube_video);
?>

I want to change the url https://www.youtube.com/watch?v=e4Uq8O5ZhUA outside php code

example: site.com/youtube-dl.php?link=https://www.youtube.com/watch?v=e4Uq8O5ZhUA

which exists in $youtube_video = 'https://www.youtube.com/watch?v=e4Uq8O5ZhUA'

replace to $youtube_video = '$_GET['link']' but error

how can it work?


Solution

  • Secure it

    if (ereg("^[0-9A-z_-]+$",$_GET['link'])) $youtube_video = "https://www.youtube.com/watch?v=".$_GET['link'];
    

    http://example.com/youtube-dl.php?link=e4Uq8O5ZhUA