如何获得最低质量的YouTube视频?

时间:2013-09-18 15:51:08

标签: php video youtube youtube-api

我有一个代码段,可将YouTube视频转换为.mp4并将其存储在我的服务器中。它正确地做了一切,但它得到了视频的高质量版本,而我的服务器基本上超时了。有没有办法可以检查视频是否超过20 MB并获得最低质量的视频?

这是我的代码:

<?php
$id = $_GET['id'];
$format = 'video/mp4';
parse_str(file_get_contents("http://www.youtube.com/get_video_info?video_id=" . $id), $info);
$streams = $info['url_encoded_fmt_stream_map'];

$streams = explode(',', $streams);

foreach($streams as $stream) {
    parse_str($stream, $data);
    if(stripos($data['type'], $format) !== false) {
        $video = fopen($data['url'] . '&signature=' . $data['sig'], 'r');
        $file = fopen($_GET['id'] . '.mp4', 'w');
        stream_copy_to_stream($video, $file);
        fclose($video);
        fclose($file);
        echo '<a href="./' . $_GET['id'] . '.mp4">Download</a>';
        die();
    }
}
?>

1 个答案:

答案 0 :(得分:3)

使用此代码,您可以获得资源的内容长度(大小):

$url = "http://some-adress/test.php";
$headers = get_headers($url, 1);
$content_length = $headers["Content-Length"];

如果您解析流地图,则必须查找视频质量“itag”。 这是一个获取itag的preg_match的例子:

preg_match('/itag=([0-9]+)/',$url,$tm);

这是所有代码的列表及其含义:


    $typeMap = array();
    $typeMap[13] = array("13", "3GP", "Low Quality - 176x144");
    $typeMap[17] = array("17", "3GP", "Medium Quality - 176x144");
    $typeMap[36] = array("36", "3GP", "High Quality - 320x240");
    $typeMap[5]  = array("5", "FLV", "Low Quality - 400x226");
    $typeMap[6]  = array("6", "FLV", "Medium Quality - 640x360");
    $typeMap[34] = array("34", "FLV", "Medium Quality - 640x360");
    $typeMap[35] = array("35", "FLV", "High Quality - 854x480");
    $typeMap[120] = array("120", "FLV", "High Quality - 1280x720");
    $typeMap[43] = array("43", "WEBM", "Low Quality - 640x360");
    $typeMap[44] = array("44", "WEBM", "Medium Quality - 854x480");
    $typeMap[45] = array("45", "WEBM", "High Quality - 1280x720");
    $typeMap[18] = array("18", "MP4", "Medium Quality - 480x360");
    $typeMap[22] = array("22", "MP4", "High Quality - 1280x720");
    $typeMap[37] = array("37", "MP4", "High Quality - 1920x1080");
    $typeMap[38] = array("38", "MP4", "High Quality - 4096x230");

来源:http://www.ngcoders.com/php-script/php-youtube-video-downloader-script/