异步预热在线视频实现

来源:互联网 发布:淘宝旺铺有必要开通吗 编辑:程序博客网 时间:2024/04/27 12:54

转载自https://blog.zymlinux.net/index.php/archives/100

毕业之际给学校搭建了基于ATS的正向代理缓存服务器,专门用来处理优酷土豆等在线视频流量。通过改写一个浏览器做成在线视频专用浏览器,内置了ATS的代理设置。

用php配合memcacheq和小脚本实现了简单的异步预热功能,在实际运用中安全性请自行考虑完善。

工作方式:PHP页面提交URL——>memcacheq<——shell脚本cron定时取队列url预热(wget)

php提交

php

PHP代码(海洋之心指导修改了部分):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<head>
<title>广师ATS缓存预热后台</title>
</head>
<?php
header("Content-type: text/html; charset=utf-8");
$myurl=$_GET['url'];
if(${myurl}!=""){
echo"${myurl}已经提交到下载队列,服务器会尽快下载";}
 
echo"";
echo"<form name=\"Form\" method=\"post\" action=\"mem.php\">";
echo"请输入要预热的视频URL:<input style=\"height:30px\" type=\"text\" name=\"url\" size=\"100\"/>";
     echo"<input style=\"height:30px\" type=\"submit\" value=\"提交\" />";
     echo"</form>";
?>

mem.php:

1
2
3
4
5
6
7
8
9
10
11
<?php
header("Content-type: text/html; charset=utf-8");
 
$url=$_POST['url'];
//if(${url}!=""){
$memcache_obj= memcache_connect('127.0.0.1', 21201);
 
memcache_set($memcache_obj,'url',$url, 0, 80);
Header("Location: autofetch.php?url=$url");//}
//Header("Location: autofetch.php");
?>

memcacheq的配置参考:http://blog.163.com/song_0803/blog/static/46097597201131510533947/

Shell脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
jinchengshu=`ps aux |grep -c "wget"`
if[ $jinchengshu-gt "3"]; then
echo"wget进程太多,我先退出了"
exit0
fi
shumu=1
time=`date"+%Y-%m-%d %H:%M:%S"`
memurl=`printf"get url\r\n" | nc 127.0.0.1 21201 | sed -n 2p`
ok=`echo$memurl | grep http`
if[ -z $ok]
then
    echo"队列为空!"
    exit0
else
echo"正在联网查询,请稍后..."
flvurl=`echo"$memurl" | sed -e "s@:@%3A@g"-e "s@/@%2F@g"`
echo"${time}要处理的URL为 $memurl" >>/data/ats/autofetch.log
url="http://www.flvcd.com/parse.php?format=&kw=${flvurl}"
urls=`curl -s "$url"|sed -r '/<a href="http:\/\/f.youku.com\/player/s/<a href="([^"]*)(.*)/\1/'
 
foru in $urls
do
    echo"找到的视频链接${shumu} $u" >>/data/ats/autofetch.log
    shumu=$((shumu + 1))
    trueurl=`curl -s -I $u| awk '/Location/{print $2}'`
    echo"真实视频URL $trueurl" >> /data/ats/autofetch.log
done
 
thead_num=15 ###并发数
tmp_fifo_file="/tmp/$$.fifo"
mkfifo$tmp_fifo_file
exec6<>$tmp_fifo_file
rm -f $tmp_fifo_file
 
for((i=0;i<$thead_num;i++))
do
        echo""
done >&6
 
fori in $urls
do
        echo"URL为$i"
        read -u6
                {
                        nowtime=`date"+%Y-%m-%d %H:%M:%S"`
                        wget -Y on --header="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"--header="Accept-Encoding:gzip, deflate" --header="Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"--header="Referer:http://static.youku.com/v1.0.0313/v/swf/loader.swf"--header="User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1" $i && echo"${nowtime} 下载完成 $i" >> /data/ats/autofetch.log
                        sleep 1
                        echo"" >&6
                }&
done
wait
exec6>&-
echo" " >> /data/ats/autofetch.log
fi

【注:要配置wget的代理】

创建文件 ~/.wgetrc 内容为:http_proxy = http://atsip:端口/,然后可以设定crontab让shell脚本定时取memq队列预热。

喜欢用curl下载的话可以自行修改下wget的部分代码即可。

后话

经过上面的简单运用完善一下就可以出来了一个比较完整的预热功能如下图。

yure


0 0