我已经创建了一个脚本来下载您在应用程序中复制的url的youtube webm文件。然后它会下载但是当我打开它时它说文件不能播放因为它已损坏。我该如何解决这个问题?
<?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>
答案 0 :(得分:0)
您没有正确提取视频资源网址。
例如:for video http://www.youtube.com/watch?v=x9g_8jy-Sw4
$dURL_results_2[0]
的值是
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"
不是正确的网址。
应进一步处理签名(如果存在密码签名)和以下部分:
\u0026itag=22\u0026fallback_host=tc.v8.cache3.googlevideo.com\u0026sig=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA\u0026type=video/mp4; codecs="avc1.64001F, mp4a.40.2"
应替换为
&signature=1F94BC0A23F1708BCAABF48B95F250698656ADB6.92D86F92762DB485F4E2EB770CAD6E95C5706DBA
所以最终正确的下载网址是
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
还要注意mime类型,因为您希望该文件是.webm。但每次都可能不是这种情况。您可以使用网址映射中的type
值轻松检查此内容。