[AS3]as3.0优酷地址解析方案及实例下载(2)
假设返回”03000201004D8858360BD1047C4F5FF471CDD7-C742-8D74-3EED-90A9EF54EEC1″。
生成key
利用上面得到的key1和key2
- private String genKey(String key1,String key2){
- int key = Long.valueOf(key1,16).intValue();
- key ^= 0xA55AA5A5;
- return key2 + Long.toHexString(key);
- }
假设返回”de1515a31372faac182698bc”。
好了,把sid,fileid,key合并起来,就可以得到一开始的下载地址了。
接下来就是分段视频的问题了,如果一个视频分成几段,在返回的json对象中可以找到类似的内容:
- "segs":{
- "mp4":[{"no":"0","size":"39095085","seconds":"426"},{"no":"1","size":"22114342","seconds":"426"},
{"no":"2","size":"23296715","seconds":"424"},{"no":"3","size":"18003234","seconds":"426"},
{"no":"4","size":"31867294","seconds":"423"},{"no":"5","size":"14818514","seconds":"248"}],- "flv":[{"no":"0","size":"19739080","seconds":"425"},{"no":"1","size":"11506385","seconds":"426"},
{"no":"2","size":"11821267","seconds":"426"},{"no":"3","size":"8988612","seconds":"426"},
{"no":"4","size":"16078739","seconds":"425"},{"no":"5","size":"7634043","seconds":"245"}]}
很明显,该视频分成了6段,而且有mp4和flv两种格式的视频。还记得一开始的视频地址中的蓝色部分吗,我们只要修改那一部分的数字就可以了,比如第二段,就把蓝色部分换成01(两个都要换),注意这是16进制的。
如果想下载mp4格式的,只要把下载地址中的/flv/换成/mp4/,当然你要确定该视频有mp4格式。
update:
php版代码如下:
- function getSid(){
- $sid = time().(rand(0,9000)+10000);
- return $sid;
- }
- function getkey($key1,$key2){
- $a = hexdec($key1);
- $b = $a ^0xA55AA5A5;
- $b = dechex($b);
- return $key2.$b;
- }
- function getfileid($fileId,$seed){
- $mixed = getMixString($seed);
- $ids = explode("*",$fileId);
- unset($ids[count($ids)-1]);
- $realId = "";
- for ($i=0;$i<count($ids);++$i){
- $idx = $ids[$i];
- $realId .= substr($mixed,$idx,1);
- }
- return $realId;
- }
- function getMixString($seed){
- $mixed = "";
- $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
- $len = strlen($source);
- for($i=0;$i<$len;++$i){
- $seed = ($seed * 211 + 30031)%65536;
- $index = ($seed / 65536 * strlen($source));
- $c = substr($source,$index,1);
- $mixed .= $c;
- $source = str_replace($c,"",$source);
- }
- return $mixed;
- }
另一个要注意的地方,上面提到的分段视频,从第11段开始是16进制的0A,注意是大写的,后面依次类推。
update2:
c#版代码如下:
- private String genSid()
- {
- int i1 = (int)(1000+ Math.Floor((double)(new Random().Next(999))));
- int i2 = (int)(1000+ Math.Floor((double)(new Random().Next(9000))));
- TimeSpan ts = new TimeSpan(System.DateTime.UtcNow.Ticks-new DateTime(1970,1,1, 0,0,0).Ticks);
- return Convert.ToInt64(ts.TotalMilliseconds).ToString()+"" + i1+"" + i2;
- }
- private String getFileID(String fileid,double seed)
- {
- String mixed = getFileIDMixString(seed);
- String[] ids= fileid.Split('*');
- StringBuilder realId = new StringBuilder();
- int idx;
- for (int i=0; i< ids.Length-1; i++)
- {
- idx = int.Parse(ids[i]);
- realId.Append(mixed[idx]);
- }
- return realId.ToString();
- }
- private String getFileIDMixString(double seed)
- {
- StringBuilder mixed = new StringBuilder();
- StringBuilder source = new StringBuilder("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890");
- int index, len = source.Length;
- for (int i=0; i< len;++i)
- {
- seed = (seed * 211 + 30031) % 65536;
- index = (int)Math.Floor(seed/65536 * source.Length);
- mixed.Append(source[index]);
- source.Remove(index,1);
- }
- return mixed.ToString();
- }
- private String genKey(String key1,String key2)
- {
- int key = Convert.ToInt32(key1,16);
- var tempkey = key ^ 0xA55AA5A5;
- return key2 + Convert.ToString(tempkey,16).Substring(8);
update3:
python版代码如下:
- import time
- import random
- import math
- def createSid():
- nowTime = int(time.time() *1000)
- randomrandom1 = random.randint(1000,1998)
- randomrandom2 = random.randint(1000,9999)
- return "%d%d%d" %(nowTime,random1,random2)
- def getFileIDMixString(seed):
- mixed=[]
- source=list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890")
- seed=float(seed)
- for i in range(len(source)):
- seed = (seed * 211 + 30031 ) % 65536
- index = math.floor(seed /65536 *len(source))
- mixed.append(source[int(index)])
- source.remove(source[int(index)])
- #return ''.join(mixed)
- return mixed
- def getFileId(fileId,seed):
- mixed=getFileIDMixString(seed)
- ids=fileId.split('*')
- realId=[]
- for ch in ids:
- realId.append(mixed[int(ch)])
- return ''.join(realId)
- if __name__ == '__main__':
- #print createSid()
- #print getFileIDMixString(4528)
- fileId='3*31*3*3*3*61*3*13*3*3*36*17*48*21*17*55*31*17*61*31*14*14*3*3*36*13*67*31*31*10*21*32*58*31*13*14*3*48*15*13*10*48*55*15*55*10*36*31*15*31*61*10*67*15*3*61*17*13*13*14*11*36*48*21*36*10'
- seed=4528
- print getFileId(fileId,seed)
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- [AS3]as3.0的sound类常用技巧整理
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例