根據(jù)優(yōu)酷URL砚婆,解析視頻 iOS

話(huà)不多說(shuō)直接狠鸳,上代碼

比如優(yōu)酷鏈接:http://v.youku.com/v_show/id_XNzA3NjE4MTEy.html

Model

@interface VideoModel : NSObject
@property (nonatomic, copy) NSString * srcUrl;
@property (nonatomic, copy) NSString * videoUrl;
@property (nonatomic, copy) NSString * streamType;
@property (nonatomic, copy) NSString * hdtv;
@property (nonatomic, assign) NSInteger size;
@property (nonatomic, assign) NSInteger videoMilliseconds;
@end

VC

typedef void (^completeBlock)(NSArray * array);
@property (nonatomic, strong) NSMutableDictionary * cookDic;
@property (nonatomic, strong) NSMutableArray * videoTSArr;
    self.videoTSArr = [NSMutableArray array];
    NSString * vid = [self getVid:self.self.model.url];
    NSString * utid = [self getUtid];
    NSTimeInterval client_ts = [NSDate date].timeIntervalSince1970;
    
    NSString * playApi = [NSString stringWithFormat:@"https://ups.youku.com/ups/get.json?vid=%@&ccode=0521&client_ip=192.168.1.1&utid=%@&client_ts=%ld",vid, utid, (long)client_ts];
    
    [self getVideoMessage:playApi complete:^(NSArray *array) {
        VideoModel * video = [array firstObject];
        playerManager.assetURL = [NSURL URLWithString:video.videoUrl];
    }];

- (NSString *)getVid:(NSString *)url
{
    NSString * string = @"";
    NSArray * strArr = [url componentsSeparatedByString:@"id_"];
    if (strArr.count > 1)
    {
        NSArray * neArray = [[strArr lastObject] componentsSeparatedByString:@".html"];
        if (neArray.count > 1)
        {
            string = [neArray firstObject];
        }
    }
    
    return string;
}

- (NSString *)getUtid
{
    NSString * etgm = @"";
    NSString * utidURLStr = @"https://log.mmstat.com/eg.js";
    NSURL * url = [NSURL URLWithString:utidURLStr];
    NSError * error;
    NSStringEncoding encoding;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&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;
        }
    }
    return etgm;
}

- (NSMutableDictionary *)cookDic
{
    if (!_cookDic)
    {
        _cookDic = [NSMutableDictionary dictionary];
        [_cookDic setObject:[self getCookieValue:3] forKey:NSHTTPCookieValue];
        [_cookDic setObject:[NSDate dateWithTimeIntervalSinceNow:60*60*24] forKey:NSHTTPCookieExpires];
        [_cookDic setObject:@"http://www.youku.com" forKey:NSHTTPCookieName];
        [_cookDic setObject:@"www.youku.com" forKey:NSHTTPCookieDomain];
        [_cookDic setObject:@"0" forKey:NSHTTPCookieVersion];
        [_cookDic setObject:@"www.youku.com" forKey:NSHTTPCookiePath];
    }
    return _cookDic;
}

- (NSString *)getCookieValue:(NSInteger)length
{
    NSString * source = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString * str = [[NSMutableString alloc] init];
    for (NSInteger index = 0; index < length; index++)
    {
        NSInteger number = arc4random() % 52;
        NSString * sourceStr = [source substringWithRange:NSMakeRange(number,1)];
        [str appendString:sourceStr];
    }
    
    NSInteger timeVal = [NSDate date].timeIntervalSince1970;
    return [NSString stringWithFormat:@"%ld%@",(long)timeVal,str];
}

- (void)getVideoMessage:(NSString *)api complete:(completeBlock)complete
{
    [self.videoTSArr removeAllObjects];
    
    NSHTTPCookie * cookie = [[NSHTTPCookie alloc] initWithProperties:self.cookDic];
    NSArray * cookies = [NSArray arrayWithObjects:cookie,nil];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
    
    AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [sessionManager.requestSerializer setValue:self.model.url forHTTPHeaderField:@"Referer"];
    [sessionManager.requestSerializer setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
    [sessionManager GET:api parameters:nil progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id 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"]);
            complete(@[]);
        }
        else
        {
            NSArray * streamArr = [videoDic objectForKey:@"stream"];
            for(NSDictionary * item in streamArr)
            {
                if(![[item objectForKey:@"audio_lang"]isEqualToString:@"guoyu"])continue;
                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];
            }
            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];
                }
            }
            complete([self.videoTSArr copy]);
        }
    }failure:^(NSURLSessionDataTask * _Nullable task,NSError * _Nonnull error) {
        complete(@[]);
    }];
}

- (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;
}

PS:目前ccode是0521【我試出來(lái)的】

注:侵權(quán)立刪

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末胡陪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子碍舍,更是在濱河造成了極大的恐慌柠座,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件片橡,死亡現(xiàn)場(chǎng)離奇詭異妈经,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)捧书,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)吹泡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人经瓷,你說(shuō)我怎么就攤上這事爆哑。” “怎么了舆吮?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵揭朝,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我色冀,道長(zhǎng)潭袱,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任锋恬,我火速辦了婚禮屯换,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘与学。我一直安慰自己彤悔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布癣防。 她就那樣靜靜地躺著蜗巧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蕾盯。 梳的紋絲不亂的頭發(fā)上幕屹,一...
    開(kāi)封第一講書(shū)人閱讀 49,111評(píng)論 1 285
  • 那天蓝丙,我揣著相機(jī)與錄音,去河邊找鬼望拖。 笑死渺尘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的说敏。 我是一名探鬼主播鸥跟,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼盔沫!你這毒婦竟也來(lái)了医咨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤架诞,失蹤者是張志新(化名)和其女友劉穎拟淮,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體谴忧,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡很泊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了沾谓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片委造。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖均驶,靈堂內(nèi)的尸體忽然破棺而出昏兆,到底是詐尸還是另有隱情,我是刑警寧澤辣恋,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布亮垫,位于F島的核電站,受9級(jí)特大地震影響伟骨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜燃异,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一携狭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧回俐,春花似錦逛腿、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至忘瓦,卻和暖如春搁廓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工境蜕, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蝙场,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓粱年,卻偏偏與公主長(zhǎng)得像售滤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子台诗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容