Posted By

codespartan on 06/18/12


Tagged

php script video download YouTube


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

fbifido
firstpass
Some0ne
catrinho


Youtube Video Download Script


 / Published in: PHP
 

Extracts the video title & download URL from a youtube page and starts a download.

  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  3. {
  4. $url = (isset($_POST['url']) && !empty($_POST['url'])) ? $_POST['url'] : false;
  5. if (!$url) {
  6. echo "Please enter a URL.";
  7. } else {
  8. $source = file_get_contents($url);
  9. $source = urldecode($source);
  10.  
  11. // Extract video title.
  12. $vTitle_results_1 = explode('<title>', $source);
  13. $vTitle_results_2 = explode('</title>', $vTitle_results_1[1]);
  14.  
  15. $title = trim(str_replace(' - YouTube', '', trim($vTitle_results_2[0])));
  16.  
  17. // Extract video download URL.
  18. $dURL_results_1 = explode('url_encoded_fmt_stream_map": "url=', $source);
  19. $dURL_results_2 = explode('\u0026quality', $dURL_results_1[1]);
  20.  
  21. // Force download of video.
  22. $file = str_replace(' ', '_', strtolower($title)).'.webm';
  23.  
  24. header("Cache-Control: public");
  25. header("Content-Description: File Transfer");
  26. header("Content-Disposition: attachment; filename=$file");
  27. header("Content-Type: video/webm");
  28. header("Content-Transfer-Encoding: binary");
  29.  
  30. readfile($dURL_results_2[0]);
  31.  
  32. }
  33. }
  34. ?>
  35. <form method="post">
  36. <label for="url">URL:</label>
  37. <input type="text" name="url" value="" id="url">
  38. <input type="submit" name="submit" value="Download">
  39. </form>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fbifido on July 5, 2012

how do we do the mp4 file?

Posted By: fbifido on July 5, 2012

can you show how to download the diff. type-format(MP4/FLV/WEBM) and type-stream (SD/HD/Full-HD) or type-size (360/480/720).

You need to login to post a comment.