客户端解析优酷等三方网站的真实的播放地址。
解析优酷真实的地址,只需要获取到优酷的网页的播放地址即可。
流程:1)获取到视频在网页上的地址
2)获取需要的参数拼接完整的API
3)获取真实的视频地址(需要手动拼接cookie,需要验证cokkie)
第一步:拼接需要的API:(后面用于获取真实的播放地址)
NSString * playApi = @"https://ups.youku.com/ups/get.json?vid=%@&ccode=0508&client_ip=192.168.1.1&utid=%@&client_ts=%d";
需要注意的是:ccode优酷会间隔一段时间去更新这个参数。如果不能解析,那么去网上找一下最新的ccode。
1)将网页链接地址传入下面的方法,获取到参数vid
-(NSString*)comIdFlagStr:(NSString*)url{
NSString* str =nil;
if([RemoveEmpt removeEmptWithString:url].length>0) {
NSArray* strArr = [url componentsSeparatedByString:@"id_"];
if(strArr.count>1) {
NSArray* neArry = [[strArr lastObject] componentsSeparatedByString:@".html"];
if(neArry.count>1) {
str = [neArry firstObject];
}
}
}
returnstr;
}
2)获取utid参数。
#define utidURL @"https://log.mmstat.com/eg.js";
-(NSString*)comETag{
NSString* etgm =nil;
NSString* utidURLStr =utidURL;
NSURL* url = [NSURL URLWithString:utidURLStr];
NSError*error;
NSStringEncoding encoding;
NSString * dataString = [[NSString alloc] initWithContentsOfURL:urlusedEncoding:&encoding error:&error];
NSArray * arr = [dataString componentsSeparatedByString:@";"];
for(NSString* str in arr) {
if([str hasPrefix:@"goldlog.Etag"]) {
NSString * etag = [[str componentsSeparatedByString:@"="] lastObject];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];
etag = [etag stringByTrimmingCharactersInSet:set];
etgm = etag;
break;
}
}
returnetgm;
}
3)拼接clients_ts参数
NSDate* date = [NSDatedate];
NSInteger index = (NSInteger)date.timeIntervalSince1970;
4)拼接完整的api
二:获取真实的地址
1)需要先拼接cookie,用于验证信息
-(NSMutableDictionary *)cookDic{
if(!_cookDic) {
_cookDic = [[NSMutableDictionary alloc] init];
[_cookDicsetObject:[self getYsuid:3] forKey:NSHTTPCookieValue];
[_cookDic setValue:[NSDate dateWithTimeIntervalSinceNow:60*60*24] forKey:NSHTTPCookieExpires];
[_cookDic setValue:@"http://www.youku.com" forKey:NSHTTPCookieName];
[_cookDic setValue:@"www.youku.com" forKey:NSHTTPCookieDomain];
[_cookDic setValue:@"0" forKey:NSHTTPCookieVersion];
[_cookDic setValue:@"www.youku.com" forKey:NSHTTPCookiePath];
}
return _cookDic;
}
2)获取ysuid参数
-(NSString*)getYsuid:(NSInteger)length{
NSString* ysuid =@"";
NSString * source = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString * str = [[NSMutableString alloc] init];
for(int i=0; i<length;i++) {
NSInteger number =arc4random()%52;
NSString* sourceStr = [source substringWithRange:NSMakeRange(number,1)];
[str appendString:sourceStr];
}
NSInteger timeVal = [NSDate date].timeIntervalSince1970;
NSString* yu = [NSString stringWithFormat:@"%ld%@",(long)timeVal,str];
ysuid = yu;
return ysuid;
}
3)baseUrl就是网页地址
-(void)getVideoMessage:(NSString*)api{
[self.videoTSArr removeAllObjects];
//获取cookie
NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:self.cookDic];
NSArray * cookies=[NSArray arrayWithObjects:cookie,nil];
NSDictionary * headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
AFHTTPSessionManager * afMn = [AFHTTPSessionManager manager];
afMn.responseSerializer = [AFHTTPResponseSerializer serializer];
[afMn.requestSerializer setValue:self.baseUrl forHTTPHeaderField:@"Referer"];
[afMn.requestSerializer setValue:[headersobjectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
[afMnGET:api parameters:nil progress:^(NSProgress*_Nonnull downloadProgress) {
}success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSDictionary* videoDic = [dic objectForKey:@"data"];
if([videoDic objectForKey:@"error"]!=NULL) {
NSLog(@"%@",[[videoDic objectForKey:@"error"]objectForKey:@"note"]);
self.anLyBlock([NSMutableArray array]);
}
else {
NSArray* streamArr = [videoDic objectForKey:@"stream"];
for(NSDictionary* item in streamArr)//视频.ts地址 {
if(![[item objectForKey:@"audio_lang"]isEqualToString:@"guoyu"])continue;
VideoModel* video = [VideoModelnew];
video.srcUrl= api;
video.videoUrl= [item objectForKey:@"m3u8_url"];
video.size= [[item objectForKey:@"size"]integerValue];
video.streamType= [item objectForKey:@"stream_type"];
video.videoMilliseconds= [[item objectForKey:@"milliseconds_video"]integerValue];
video.hdtv= [self hdtv:[item objectForKey:@"stream_type"]];
[self.videoTSArr addObject:video];
}
if(self.videoTSArr.count<=0){
for(NSDictionary* item in streamArr) {
VideoModel* video = [VideoModel new];
video.srcUrl= api;
video.videoUrl= [item objectForKey:@"m3u8_url"];
video.size= [[item objectForKey:@"size"]integerValue];
video.streamType= [item objectForKey:@"stream_type"];
video.videoMilliseconds= [[item objectForKey:@"milliseconds_video"]integerValue];
video.hdtv= [self hdtv:[item objectForKey:@"stream_type"]];
[self.videoTSArr addObject:video];
}
}
self.anLyBlock(self.videoTSArr);
}
}failure:^(NSURLSessionDataTask * _Nullable task,NSError * _Nonnull error) {
//失败
if(self.anLyBlock)
self.anLyBlock([NSMutableArray array]);
}];
}
判断清晰度
-(NSString*)hdtv:(NSString*)type{
NSString* ht =@"h1";
if([type isEqualToString:@"hd3"])
ht =@"1";
else if([type isEqualToString:@"hd2"])
ht =@"2";
else if([type isEqualToString:@"mp4hd"]||
[type isEqualToString:@"mp4"])
ht =@"3";
else if([type isEqualToString:@"mp4hd3"]||
[type isEqualToString:@"mp4hd2"]||
[type isEqualToString:@"flvhd"]||
[type isEqualToString:@"flv"])
ht =@"4";
else if([type isEqualToString:@"3gphd"])
ht =@"5";
else if([type isEqualToString:@"3gp"])
ht =@"6";
return ht;
}
这就完整的解析出来真正的播放地址
后面会补充解析乐视的地址
如有不合理的地方,请斧正。
申明:这只是自己的一些研究而已,请不要用于商业用途。如果影响到你的利益,请联系我。
网友评论