-1

i have made a script to download the youtube webm file of the url you copied in the application. then it will download but when i open it it says the file cant be played because its damaged. How do i fix this?

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $url = (isset($_POST['url']) && !empty($_POST['url'])) ? $_POST['url'] : false;

    if (!$url) {
        echo "Please enter a URL";
    } else {
        $source = file_get_contents($url);
        $source = urldecode($source);

        // Extract video title.
        $vTitle_results_1 = explode('<title>', $source);
        $vTitle_results_2 = explode('</title>', $vTitle_results_1[1]);

        $title = trim(str_replace(' – YouTube', '', trim($vTitle_results_2[0])));

        // Extract video download URL.
        $dURL_results_1 = explode('url_encoded_fmt_stream_map": "url=', $source);
        $dURL_results_2 = explode('\u0026quality', $dURL_results_1[1]);

        // Force download of video.
        $file = str_replace(' ', '_', strtolower($title)).'.webm';

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: video/webm");
        header("Content-Transfer-Encoding: binary");

        readfile($dURL_results_2[0]);

        exit;
    }
}
?>
<form method="post">
    <label for="url">URL:</label> <input type="text" name="url" value=""
        id="url"> <input type="submit" name="submit" value="Download">
</form>
mario
  • 144,265
  • 20
  • 237
  • 291
Rene
  • 13,299
  • 5
  • 19
  • 28
  • Look at the binary dump (with an hexeditor) and/or `diff` it against the original to find out. – mario Jan 26 '14 at 20:33

1 Answers1

0

You are not extracting the video resource url correctly.

For example: for video http://www.youtube.com/watch?v=x9g_8jy-Sw4 the value of $dURL_results_2[0] is

http://r1---sn-qxa7en7s.googlevideo.com/videoplayback?itag=22&mt=1392152819&upn=IjyEP3JHDpU&id=c7d83ff23cbe4b0e&source=youtube&sver=3&expire=1392179297&key=yt5&ip=59.89.130.203&mv=m&fexp=917000,912302,932260,914088,916626,937417,913434,936910,936913&ms=au&sparams=id,ip,ipbits,itag,ratebypass,source,upn,expire&ratebypass=yes&ipbits=0\u0026itag=22\u0026fallback_host=tc.v8.cache3.googlevideo.com\u0026sig=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA\u0026type=video/mp4; codecs="avc1.64001F, mp4a.40.2"

which is not the correct url.

It should be further processed for signature (also cipher signature if present) and following part:

\u0026itag=22\u0026fallback_host=tc.v8.cache3.googlevideo.com\u0026sig=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA\u0026type=video/mp4; codecs="avc1.64001F, mp4a.40.2"

should be replaced with

&signature=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA

So the final and correct download url is

http://r1---sn-qxa7en7s.googlevideo.com/videoplayback?itag=22&mt=1392152819&upn=IjyEP3JHDpU&id=c7d83ff23cbe4b0e&source=youtube&sver=3&expire=1392179297&key=yt5&ip=59.89.130.203&mv=m&fexp=917000,912302,932260,914088,916626,937417,913434,936910,936913&ms=au&sparams=id,ip,ipbits,itag,ratebypass,source,upn,expire&ratebypass=yes&ipbits=0&signature=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA

Also take care of the mime type, as you are expecting that file is .webm. but this may not be the case everytime. you can easily check this with the value of type in the url map.

Akhilesh
  • 1,064
  • 15
  • 28