php - 是否可以通过可恢复的PHP通过PHP下载YouTube视频链接?

标签 php youtube download youtube-dl resume-download

是否有可能通过可恢复的PHP下载youtube视频链接。我可以通过youtube-dl轻松获取下载链接。

在可恢复的PHP中,需要知道视频的大小。但是从链接中我无法获取大小,因此可以通过PHP恢复。

假设链接是:[mp4]

https://r10---sn-hp576n7z.googlevideo.com/videoplayback?mt=1415174349&itag=22&fexp=915516%2C930666%2C932404%2C934601%2C947209%2C947215%2C948124%2C952302%2C952901%2C953912%2C957103%2C957201&key=yt5&upn=xe1em66NCSc&sparams=gcr%2Cid%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&ipbits=0&ratebypass=yes&gcr=us&mv=u&id=o-ANmao-OacohTVjp7yijSePI5a3vvlERcSs6uL3WLfOA7&ms=au&expire=1415196179&sver=3&ip=176.576.184.114&mime=video%2Fmp4&requiressl=yes&source=youtube&mm=31&signature=29046D03C1EC013A78BD20E4CDD362BBA62ADF4B.A0E2E8E104769DD664202597A91F0888F9E554B3

最佳答案

试试这个来获取文件大小:

<?php
 /**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
function curl_get_file_size( $url ) {
    // Assume failure.
    $result = -1;

    $curl = curl_init( $url );

    // Issue a HEAD request and follow any redirects.
    curl_setopt( $curl, CURLOPT_NOBODY, true );
    curl_setopt( $curl, CURLOPT_HEADER, true );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'user_agent_string' );

    $data = curl_exec( $curl );
    curl_close( $curl );

    if( $data ) {
        $content_length = "unknown";
        $status = "unknown";

        if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
            $status = (int)$matches[1];
        }

        if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
            $content_length = (int)$matches[1];
        }

        // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
        if( $status == 200 || ($status > 300 && $status <= 308) ) {
            $result = $content_length;
        }
    }

    return $result;
}

用法:
$file_size = curl_get_file_size($url);

关于php - 是否可以通过可恢复的PHP通过PHP下载YouTube视频链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26752301/

相关文章:

javascript - 让 jQuery 函数通过 mySQL 查询使用来自外部 php 文件的数据

php - 如何处理 AJAX 请求中的 session 超时

php - 为什么我收到 max_user_connections SQL 错误?

javascript - YouTube iframe 嵌入在 Internet Explorer 8 中抛出 JavaScript 错误

youtube - Youtube Player API-选择了“仅播放浏览器”标签

php - Youtube channel 嵌入尺寸

android - 当cordova构建android --angular时点击javax.net.ssl.SSLHandshakeException

php - 如何创建虚拟滚动条?

python - 使用 Python Selenium 下载文件

javascript - 如何知道 JavaScript 的下载何时开始?