最近公司需要存在阿里云对象存储(oss)里的视频文件,而且需要18套课程的视频源文件,这周就要,我想了一下这要是一个一个找那可就麻烦了。要想想一套课程有n个章节,每个章节有n个视频文件。所以我下定决心要写一个自动下载的程序。
废话不多说,先看效果
下面是代码
<?php
class Request{
public static function post($url, $post_data = '', $timeout = 5){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
if($post_data != ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
public static function post2($url, $data=array()){
$postdata = http_build_query(
$data
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
public static function post3($host,$path,$query,$others=''){
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return $r;
}
}
ini_set('max_execution_time', '0');
@ini_set('memory_limit', '4048M');
$data = Request::post2('https://www.xxxx.cn/index/details_data',array('id'=>77));
echo '<pre>';
$data = json_decode($data,true);
foreach ($data as $k => &$v) {
if(is_array($v)){
$dir = iconv("UTF-8", "GBK", "Public/".$v['name']);
mkdir ($dir,0777,true);
$Catalogdata = $v['Catalogdata'];
foreach ($Catalogdata as $kk => &$vv) {
$hd = opendir($dir);
$i = 0;
while($f=readdir($hd)){
$dir = iconv("UTF-8", "GBK","Public/".$v['name'].'/'.$vv['id'].$vv['name']);
mkdir ($dir,0777,true);
$hd = opendir($dir);
while($f=readdir($hd)){
if(!empty($vv['video_url'])){
$mov = file_get_contents('http:'.$vv['video_url']);
file_put_contents($dir.'/'.$vv['name'].'.mov',$mov);
}
$chapter = $vv['chapter'];
if(is_array($chapter)){
foreach ($chapter as $key => $value) {
$mov = file_get_contents('http:'.$value['vedio']);
file_put_contents($dir.'/'.$value['title'].'.mov',$mov);
echo ++$i;
}
}
}
}
closedir($hd);
}
}
}
?>
由于有些特别大的文件下载还有点问题,所以我没有遍历循环,方便下载一套课程检查一套课程。
要是我一个一个手动下载那估计得需要两天多,写这个程序用了三个小时,下载文件大约两个小时,极大的缩短了工作时间,提高了工作效率。感谢大家观看,我们下次见。
|