使用 PHP 从 youtube 下载视频 - 仍然可以吗?

发布于 2024-09-12 13:00:08 字数 80 浏览 3 评论 0原文

我想知道在过去几个月 youtube 发生了所有变化之后,是否有办法编写一个能够下载视频的脚本?

我知道一年前有效的方法不再适用。

I was wondering if there is a way, after all the changes in youtube in the last months, to write a script that enables downloading videos?

I'm aware of the fact that the methods that worked a year ago, are no longer relevant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-19 13:00:08

是的,还有办法。我不会为您编写整个脚本,但至少会为您编写提供 YouTube 下载链接的函数。

好吧,您需要安装 CURL 才能执行此操作。

/* Developed by User - WebEntrepreneur @ StackOverlow.com */

function ___get_youtube_video($youtube_url)
{
    if(eregi('youtube.com', $youtube_url))
    {
        preg_match('/http:\/\/(.+)youtube.com\/watch(.+)?v=(.+)/', $youtube_url, $youtube_id_regex);
        $youtube_id = ($youtube_id_regex[3]) ? $youtube_id_regex[3] : '';

        if(!$youtube_id)
        {
            return INVALID_YOUTUBE_ID;
        }

        if(eregi('\&', $youtube_id))
        {
            $youtube_id_m = explode('&', $youtube_id);
            foreach($youtube_id_m as $slices)
            {
                $youtube_id = $slices;
                break;
            }
        }

    } else {
        $youtube_id = ($youtube_url);
    }

    $ping = ___get_curl("http://www.youtube.com/watch?v={$youtube_id}&feature=youtu.be");

    if(!$ping)
    {
        return YOUTUBE_UNAVAILABLE;
    }

    $ping_scan = nl2br($ping);
    $ping_scan = explode('<br />', $ping_scan);

    if(eregi('= null', $ping_scan[36]) or !$ping_scan[36])
    {
        return YOUTUBE_TOO_MANY_REQUESTS;
    }

    $ping_scan = str_replace("\n", "", $ping_scan[36]);
    $ping_scan = str_replace("              img.src = '", "", $ping_scan);
    $out[1] = str_replace("';", "", $ping_scan);

    $sub_ping = ___get_curl("http://gdata.youtube.com/feeds/api/videos/{$youtube_id}");

    preg_match('/<title type=\'text\'>(.+)<\/title>/', $sub_ping, $inout);

    $inout = $inout[1];

    if(!$out[1])
    {
        return VERSION_EXPIRED;
    }

    $out[1] = str_replace("generate_204", "videoplayback", $out[1]);
    $out[1] = str_replace("\\/", "/", $out[1]);
    $out[1] = rawurldecode($out[1]);

    if($inout)
    {
        $out[1] .= "&file=".urlencode($inout).".mp4";
        $filename = urlencode($inout).".mp4";
    }

    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    flush();
    return ___get_curl($out[1]);
}

function ___get_curl($url)
{

    if(!function_exists("curl_setopt"))
    {
        return CURL_NOT_INSTALLED;
    }

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_USERAGENT, "YouTube Video Downloader");
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,100000);

    $curl_output=curl_exec($ch);
    $curlstatus=curl_getinfo($ch);
    curl_close($ch);

    return $curl_output;

}

现在,让我谈谈代码。经过大约 30 分钟的研究,我成功破解了他们的算法,但他们现在已经设定了非常严格的界限。

由于每个视频下载请求仅与 IP 相关联,因此您需要将其从带宽中传输出来,这就是上面脚本的作用。然而,像 keepvid.com 这样的网站使用 Java 来获取下载 URL 并将其流式传输给用户。我还安装了自己的 YouTube ID 抓取器,对于此类工具来说非常方便。

请注意,如上所述,YouTube 总是会更改其算法,使用此工具的风险由您自行承担。对于对 YouTube 造成的任何损害,我不承担任何责任。

希望它为你设定了一个良好的界限,花了一段时间才做到。

Yes there is still a way. I'm not going to write the whole script for you, but I'll at least write you the function that provides the download link from YouTube.

Alright, you're going to need CURL installed to do this.

/* Developed by User - WebEntrepreneur @ StackOverlow.com */

function ___get_youtube_video($youtube_url)
{
    if(eregi('youtube.com', $youtube_url))
    {
        preg_match('/http:\/\/(.+)youtube.com\/watch(.+)?v=(.+)/', $youtube_url, $youtube_id_regex);
        $youtube_id = ($youtube_id_regex[3]) ? $youtube_id_regex[3] : '';

        if(!$youtube_id)
        {
            return INVALID_YOUTUBE_ID;
        }

        if(eregi('\&', $youtube_id))
        {
            $youtube_id_m = explode('&', $youtube_id);
            foreach($youtube_id_m as $slices)
            {
                $youtube_id = $slices;
                break;
            }
        }

    } else {
        $youtube_id = ($youtube_url);
    }

    $ping = ___get_curl("http://www.youtube.com/watch?v={$youtube_id}&feature=youtu.be");

    if(!$ping)
    {
        return YOUTUBE_UNAVAILABLE;
    }

    $ping_scan = nl2br($ping);
    $ping_scan = explode('<br />', $ping_scan);

    if(eregi('= null', $ping_scan[36]) or !$ping_scan[36])
    {
        return YOUTUBE_TOO_MANY_REQUESTS;
    }

    $ping_scan = str_replace("\n", "", $ping_scan[36]);
    $ping_scan = str_replace("              img.src = '", "", $ping_scan);
    $out[1] = str_replace("';", "", $ping_scan);

    $sub_ping = ___get_curl("http://gdata.youtube.com/feeds/api/videos/{$youtube_id}");

    preg_match('/<title type=\'text\'>(.+)<\/title>/', $sub_ping, $inout);

    $inout = $inout[1];

    if(!$out[1])
    {
        return VERSION_EXPIRED;
    }

    $out[1] = str_replace("generate_204", "videoplayback", $out[1]);
    $out[1] = str_replace("\\/", "/", $out[1]);
    $out[1] = rawurldecode($out[1]);

    if($inout)
    {
        $out[1] .= "&file=".urlencode($inout).".mp4";
        $filename = urlencode($inout).".mp4";
    }

    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    flush();
    return ___get_curl($out[1]);
}

function ___get_curl($url)
{

    if(!function_exists("curl_setopt"))
    {
        return CURL_NOT_INSTALLED;
    }

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_USERAGENT, "YouTube Video Downloader");
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,100000);

    $curl_output=curl_exec($ch);
    $curlstatus=curl_getinfo($ch);
    curl_close($ch);

    return $curl_output;

}

Now, let me talk about the code. After around 30 minutes of research, I managed to hack their algorithm however it does have very strict boundaries which they've set in now.

As each request made for a video download is only tied to the IP, you would need to stream it out of your bandwidth which is what the script above does. However, sites like keepvid.com use Java to grab the download url and stream it to the user. I've also put in my own YouTube ID grabber which is very handy for these kind of tools.

Please acknowledge that as stated, YouTube does always change their algorithms and usage of this tool is at your own risk. I am not held liable for any damages made to YouTube.

Hope it sets a good boundary for you, spent a while making it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文