2014-04-11 153 views
0

我正在嘗試從iPhone管中下載視頻。我採用gdata api提供feed,但我認爲feed中的url不是視頻的ur,而是html頁面的url。這是我的代碼:NSURLConnection從YouTube上下載視頻

-(void)downloadSongWithURL:(NSURL*)songURL andDelagate:(PlayViewController*)dataReceiver{ 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:songURL  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15]; 
toSendData = dataReceiver; 
connectionYouTube = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
[connectionYouTube start]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    dataReceived = [[NSMutableData alloc] init]; 
    NSLog(@"receive response function: %@", response); 
} 

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error{ 
    connection = nil; 
    NSLog(@"Connessione fallita"); 
} 

-(NSCachedURLResponse *)connection:(NSURLConnection *)connection 
        willCacheResponse:(NSCachedURLResponse*)cachedResponse{ 
    return nil; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    [dataReceived appendData:data]; 
    NSLog(@"receiv data function: %@ Connection: %@", data, connection); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    [toSendData receivedData:dataReceived]; 
    NSLog(@"dataaaaaaaaaaaa: %@", [NSString stringWithUTF8String:[dataReceived bytes]]); 
    NSLog(@"download finish connection: %@", connection); 
} 
用來記錄我這

2014-04-11 16:55:23.039 AudioTube[5572:70b] receive response function: <NSHTTPURLResponse:  0xf780d70> { URL: https://www.youtube.com/v/Ek0SgwWmF9w?version=3&f=videos&app=youtube_gdata } {  status code: 200, headers { 
    "Alternate-Protocol" = "443:quic"; 
"Cache-Control" = "no-store"; 
"Content-Length" = 4411; 
"Content-Type" = "application/x-shockwave-flash"; 
Date = "Fri, 11 Apr 2014 14:55:22 GMT"; 
Expires = "Tue, 27 Apr 1971 19:44:06 EST"; 
P3P = "CP=\"This is not a P3P policy! See http://support.google.com/accounts/bin/answer.py?answer=151657&hl=en for more info.\""; 
Server = "gwiseguy/2.0"; 
"Set-Cookie" = "YSC=uT7YqaOWRV8; path=/; domain=.youtube.com; httponly, GEUP=8386b307efd185ab8f2e9d0d7d782d69aTAAAAA=; path=/; domain=.youtube.com; expires=Sun, 10-Apr-2016 14:55:22 GMT"; 
"X-Content-Type-Options" = nosniff; 
"X-Frame-Options" = ALLOWALL; 
"X-XSS-Protection" = "1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube"; 
} } 
     2014-04-11 16:55:23.041 AudioTube[5572:70b] receiv data function: <43575311 471e0000 78dab539  69731bc9 75dd3303 f40c4010 07499002 49093c24 1e3b0440 52074951 5a5114c1 6329820b 50ab6305 01436000  421a1c8b 1990e2ae 3796956c 761d3b1b 3b4e9838 8e633a87 e364936c 8ecae77c cb67502a 4b1f53e5 aaa4fc2f  98d73d03 1294bc2a 6faa42a1 a7e7bdee 7ef77b3d dd7a8c84 5f22d4fa 33845a30 bae19510 426ff325 0ea1bd6e  8494ed7c aaaa1885 521ea1f3 a1a98b93 93172f9c 9f9c9ebe 847af71c 0869d9ca 4384c628 d4899051 303415a1  9b355d0d 8e056f2a d992aaeb 300424f5 1d84c643 11005a80 6876bb90 55cb8001 d88fd076 41dd4965 cab59201  4ca6ce8f 4f4f4d4d c2880f66 6a5a79c7 124037e7 b722a46a d08a9b6a 36ab6601 2302795d 3347e711 2ad4aa5a 51795c55 61c19661 54f49970 b8301eda 350ac57c 28532e86 b70be185 4791447e e776313a bd1336e7 66d59c52 d38cd0c3 4ade5486 89982a64 116a9a6c 6a5d30b5 9e02e82a 552d77cc 486fe2b3 6be86118 dc2c94c2 154dd955 ab63db39 6d455bcb 4c3c0a67 2a7a882e ecddeb00 2baaa5bc b195d2d5 4cb99405 a92727a8 a13c0df5 99aaa67a ef239453 1f57109a 8e5c8a5c 88c8d313 972e46ce d36e3a72 419e3e7f 2132c1ba a9f17179 7afce245 da4d5e3a 3f7e09a0 c9f39374 e6e4e4c4 14202fc2 3074e723 13130c9a a0169f36 adf79bd9 6deb8393 36bb6c2e d6b3bfd9 723d7b62 39fa4f09 ad63883d 7f2b3cd7 53a97525 f348c9ab 7a2893df ad25d6d1 61eb191b 0caf7f96 c443c8c2 a5f25a79 53d150a5 5a36cac6 6e454545 a550425a 59c9ce6b 850acab0 4755550c 75a15831 766f9621 d4d8c8d1 db2acc55 ab1092d9 79b0f626 30456a49 d9d4d487 ba522980 d1739aa2 6f21f5b1 a1564bc0 6ac17a59 2e419f53 322aa488 52d0e80a a454f3b5 a25a3274 942b2155 cf282050 56d58d6a 7917d54a 542ac616 55d56279 5b3d16c6 8c0e2649 1615f4f5 66900649 18e915ad 6030b3ce 58c6454d 91866860 a14229ab 3e8ee510 8b2b10da 58b61010 6b48af6d 821c348d 53d4c1e1 306a8426 9a4b2454 63bd5aae 44350592 6c8f4322 eaf1f97e cca12f79 0ae01e86 fa1cd20c 21fe143c 3e050fc5 f6042420 915b0300 779a00ff 39242746 2d7dc8b4 fc3d1e82 58140875 2b741ced 6c8c9a3d 3ebb4710 a1144572 0d48709d 146d0e8a 9fb75032 3c90013b be44c723 d21a7d3a 9657a974 a7105be3 64232d6c c4c59ead 6b74f827 96c45c1f 8a3112ee 9e3d1b22 3ca0d68e 41ee2488 4f828881 20a8702c baa7277e d5149820 816353e6 4d3c9dc8 54352de6 8d5304e5 f76b6414 e39db163 bd7c96a8 23e8223a b28b600e c53b5fc1 b4bd8669 3f26065e e8b0a126 d2fe23d2 ae264376 ac5d6d9a d3499dc8 77429d16 bb4c0d1a 78fa3c15 875181a9 2b981601 d8c65c6e 1303cb3e e60c07b2 10ddcb3e 13a64b03 e64cb692 b356dad9 44bbd863 4e0c62ca b6f775b6 a7295bc2 843af315 a3e25789 1c8c379c b908d324 16277df0 e660b210 72087f79 cafd2cda 73024290 9a83d3a4 d11f670b 7ec8410d a0abc4e3 5541b64a 7cc3aa26 b94c170d 58a63c8a 3998e6b4 4c36b808 6382933e c5b3ecdd c1decf2c 9a6ee118 11e0c8b3 a502270a d4e16e53 434ec066 909dbbd2 091bf70d 78fdec34 64e3cd8d b9c47cea e6dcf21a 32b60a7a a364b202 c9129a66 71a795e0 8dc8e598 b07c9c52 fa774a69 113cb955 d60d3403 55c82a43 bbe59a51 db5443f9 7239afa9 50257556 84465162 5737d422 827dac56 2d18bbe6 f675a36c b2ec0496 22b6d898 1d4fdcd0 f53305e3 27b21140 09d991f5 b7366b16 2516b1cb d4030144 2d6c37c9 10b36ab0 8512887e c3f32b7e 16cd5362 f71cd434 9410cf6d bbd189bf 5f3940ea 94a944ca d229a554 40c50c7c 6f944ba9 c44e0eca e2e35df4 1fedffdd 4e2b12d4 a3aa5254 c7910785 28857f85 bcd82e17 b22d6c8f 08a9dbb4 f6db1668 e758a856 cb55f66a 4fb0c22b 466ba50c a54bae97 cb60b692 d35c5533 0a9a2e5d df35d4b9 6a55d9b5 68e9cc94 2e73779a 2fc366f3 d8b0c736 1faa19e3 34886dd9 3ed47045 93d86243 6c974929 5bd0e9fe 624f54c0 27ead09b 17cf3416 dbcd3da9 0bf6c5c6 8ec79459 2d805c25 b5ea2a97 e68eb7cc 9672c994 74b95430 9ce6feb9 a2cf550a f67289a2 dce5d209 2a6d4d64 1b34ec1a a3e06912 9099c691 5072aa49 ddd560c3 8cdb562e c5d50f6a b0bfae2b d42d404e b79be3dd af129939 2622513e d4dcfae9 d7f6f204 8d5cf576 152ca256 c3af1179 f3fc53e0 5fa596df 328e5dbf a494b29a 5af5af2e 2cceada6 5612a9e8 adb5f98d e5d85a6a 6deee682 3daee617 1e579c4d df1ed2d1 c7849857 8df714ad a676ea6a 29bb0e01 74bb606c 81c7e26a 4ead56d5 aa033430 54b652ca 6ca99947 6b6003a7 ae80b90b 1f422ce5 bd2cfd12 996aa162 cc6532f0 29ee6a8c b2558e0c 3ca2c04e cdb66be5 529ee298 e896d402 1d171356 32e35167 533ab7b1 f7e5124b 75d5c439 342b0472 6547e5c8 23cea6af a93ef601 130e2bd9 32841dfd 269c4b4c 86272291 8be1cd5a 41838ffc f613313b 63c6ece0 49e40db3 379381a6 06f056ab 674e4e62 2e5220df b6557362 f71b88f4 34e7ef0c f31c1d57 0cb06b15 8ff3f0a5 e4a111db 9c00c2f2 daf2868d e58a3743 f313868e f46f598e 1d0781b8 1c4b2dc4 e3b1b8af 61cbe3b1 d6c4c2fc adf8f2c6 5d73468b bea55455 9391de32 77e3466a 7e6e75f5 fadcfc3b e2c29d8d 85f8dadc aa2fbef0 eead85c4 466a7d2e 0e3104c8 84648a5f 520dc7ad f8aa9513 02f586eb 0447acda 988a2d39 ab12d188 2119adac 830f5d59 4b63c65c c82a86e2 38668165 1babfd22 8dd5ad82 96f56c29 fa097b10 4871caa4 1506623b 25fa3da9 568d5d89 71dc804f 73079c0a e7958a01 bcdcaf> Connection: <NSURLConnection: 0xf76a380> { request: <NSMutableURLRequest: 0xf77b830> { URL: https://www.youtube.com/v/Ek0SgwWmF9w?version=3&f=videos&app=youtube_gdata } } 
2014-04-11 16:55:23.056 AudioTube[5572:70b] receiv data function: <98d875c2 1bded77c e7fff52e 6f7b3de1 f45702c8 aa1b2726 7a1fbcaf 8c7d3837 762f3236 9d0a0d26 df1a6cb5 fc6fa5b7 f7b53cb7 15f43565 cda6d2e5 76f3b466 a35573b7 a361cae0 f048f0a3 605505f5 4a41fe72 f063d770 63888e84 b6956ab0 c0768fe0 9560b69c 61e78490 7534d154 0a0d0f99 1386462e 9f36df42 bab10bc5 c7520616 0e95ca25 75e8f254 63b89ab9 32f4104a 86ce927c a69f7a6d 866e15e1 2da3a8c9 b39be5ec eed55938 2504e9d4 fb4376f3 50e2ba3f 747536cc 06fb872e f71f8943 31743780 82c39c3c 6cf219b9 cc7d3c42 ebd146a1 a8566dcc 8881d76c 14a4e522 6894833e 78941f05 95202d2a 4138a3a8 213b1cb0 e1a427d2 2994085f 801083d3 926e637b 9ea41b4a 9551e772 25e9a848 092560ce ddbf8ff3 a4aa8205 322affcb 273fc7c1 0e10e5dd 9aa21572 05a864e0 3a9d953e c84238e2 d43206f8 68ad0667 e6aaad48 e36b6459 d3d4bca2 05bf42e4 c6a92d38 c40d851c c7171eae 13f71d9c aa898d3b 07815e7b 7063976c ecc2a3f5 e47d07a7 eff0e3a1 08afeb9a e3f82ae2 c2ffe926 426c5c42 389ba609 05c67d8a 58d728ad 27af0ff8 c98988b3 e9e240a0 170689ff 87fb0281 6a37feb5 6f0a1cc7 b73d62e3 b2e7cad7 bd3ed9a1 8e4d29fa 243decda cd6b87f1 af7debe0 3a71bdf4 6d7cec7b 59d564ea 64997958 d6776470 a77cec4e b9e11899 3a43b63c 219ff484 dce40599 7a811190 8ff507ba 39d9145e 3e214ad0 8ffdbc5f f23bfdad 7e4f97cf dfd3d5d7 351818b4 21faafb3 55bc1cf0 05660357 0257036f 07ae055c fe18c3f7 04fa0279 ff43786f 75e1569b db43bcbe b6f60e7f 67eb29ec 0988dd52 4f2f03b1 28b538f1 19b1dbe3 e8fc06c1 1ce1e074 6623183e a709c122 e11d4468 21d8456c 6e62f712 d246483b 211d84f8 09e924a4 8b905384 0408e926 706e23a7 093943ec 4182fb89 3840f059 e23c47f0 10c1c304 8f103c4a f05bc429 133c4670 88e03069 8910e738 c113044f 12e13cc1 1709be44 f014c1d3 d20cee86 f3c10d82 e709bf40 7094708b 042f116e 99e01582 df215deb 04bf4b70 9ce00471 6f48b7b0 f41eccbf 2dddc1d2 fb589a86 f7fb0427 89fb3de2 be453c29 e24e13b7 42b84d62 cf107b96 d85529c7 49f39cd4 cb93d38f 082e760b 049788a7 4cf036e1 3f24f823 d2fd5ba4 fb9b043f 0152df82 f61493ee df86f63b d03e81f6 bb9878be 0da7adef 62e9f7b1 403c7f80 a5019e78 ff1013ef 1f41db83 f6a7d0fe 0cda9f43 fb31b49f 40fb29b4 bf82f6d7 d07e06ed 6fa1fd1d b42fa05d 27de7f82 ee9fa1dd ed65c712 386660cc 218e3ed0 6b0f1ece 502d2d1c 166cd60c 1e0b821d 39b0c3d1 7cb6e130 11d9cc23 8c9d3576 7e84530b 25255878 09618b9e 24015e10 9ae8f00e a7481076 b6d065d8 0547db56 3892bae1 14ef81a5 0e871723 ce07f4da e0300e47 a40ee4a7 677638f4 77740131 173ed587 5a0392e4 40dd08b5 15ec48f8 01c60fa1 fb638c1f 41f72718 6bd0fd10 e322743f c2b804dd 1c2ec3f3 2f30ae40 b78ff107 d0fd25c6 5536a4c3 f36f3036 185083e7 cf31de86 eeef31de 81ee1f30 7e0cdd3f 62bc0bdd 97187fc8 267e04cf 7fc1b805 77236c43 1e578f1d f7a2d35e a9eb8c8f f706bdf6 f63e2fe9 e847bc1d 0d789d81 41afe3d4 599fe03b e715fd43 08ac3b8c 381b1af1 dada7827 7680f9dc b84340b8 070704c4 f5822545 89af4716 c1763c27 39eed623 f565941e 952fc4de c2ac9371 3d3d960b e5c2f2dc 17a3f47e 7d24ab61 792617d9 def2d4e9 ebf55c64 aba52ecf d773ae58 1b971e4b 73b9f17a ce1b9be0 92932b93 e8c9e42f 82d016e9 595f70b8 7f84eb91 dba32f22 2a3e28e2 7ab263a5 036db5d7 73edb9f3 e63a37ac 33c10bb9 8bf59cff 08bc949b 6a0273d3 f26a3dd7 da04deac e77c4de0 5a3dd769 82c9997a ae656506 27c5a115 11c72e73 9e0e84ea 9167917b 68f4e568 e4ad224e fa637ed4 2bf28bb8 f82dfc09 9ebd82af 4254215e 00838c82 4192d241 54c2b9b7 adfe9af2 3a0a3407 0d05baa2 a31e49ce 25396be4 7a94c3b1 390c8336 c1263936 ea91e027 2fa8d9ee e622be79 8406d14b 0b92ef45 e7f1d38c e0e94748 da149e0f b079cf60 9a17218b da8d6726 4a894c08 bcf7e5e1 a12880d8 10cc1c30 7602e305 5038b640 d911eace 00a0a2f2 836814fb 2009402c 5e7eb0c2 5b0289bc cde19ea1 ee7869ba 43c5cf8a 60318f0f 24480754 ac0146e3 62a7398f 975aec39 b5584660 26eb1585 45dc3938 3c7b05f4 86f85a82 d44392c0 391c7de9 407ae940 be135dc6 3ed791e0 d79f7a20 7b834f63 23d44e0e e0dcd24f 39678426 d62a7e5e e4931394 f135099c b788f825 881ab442 89bf0301 ee7038a1 6e8803f5 888ad252 24bd1a49 df8ca4d7 22e95824 ed84df1d d4cbfeb6 1c8bf4ff 838e42db 65c39223 6c867677 f08bbd51 1f8de3e4 b29c0329 b77d3e2a 669d8ab9 2e6fa5bb 87060f63 ef0ab0ac 15f39243 0413ceae cc221e21 b7c53c19 7f957917 fcee608b 79072cf5 f098175c f5c8839e 74af5cb8 8eb77a00 e9e500e9 ad47d289 dc99ed51 1f94aa91 f4d0de12 423e9e17 a44bd41e bf88a4fb 7dc02a7d 66294de7 30394764 2d3a82b7 973c1eea 8603ea86 179617b8 a125ecee 3b3bbb81 da6c4064 1e889ff1 524b2f1d 0ca2e400 3c72b7a2 0358b935 886b2f3c a0a874f0 2c3978f0 0cb08380 7df6fdda 8b6707b9 f73afee7 f0307d2e 7810bdcd 8138ed02 c70beda0 e739b902 3460fdbe fcc17ef4 1c86b10e 1b8c0db3 b1aaacef cbc6be5c db4f0e26 ef1c44ef 60a0b92f efeccb8f f7e5ddfd d8391a64 7e9b2048 3f384afa bea745c1 7278f26e f42ed2a9 d7c11e23 e904753b f087099e c02b09fa 60e89c32 a4721ae6 214b1ff4 9f53faad e5343e7f aa1779e8 f9b3df5b fb72d467 a7461d06 1ffbbac0 abc3c97b f2372861 6a877df9 e3fd957b 38364cc3 5a58c2dd 63e3b31b c2fb743f b98ff824 7a80700a a5119744 9d02d42a 577230ad 3093805a a079170f 7bd5c4ab e902e686 3d6f51c5 d73a9fd8 8a7c3a91 1c608a0c 50263ce4 0e04b142 336413f1 3694a191 7cca66e7 854f39a8 044fa417 c14f5e1e 04db99c7 3c1f51f7 a453ffe6 13117a0f 3d913c20 d92de861 da20ca08 074f3336 6a1a69d3 b6299cfe e4e5b3e4 c073e003 ee7c6eb9 73c22678 ff0b8a82 4d149e2d 49074b92 94140ea2 028618fc 14cb9fe1 eb1cf4d1 2c07f0ef 59f07728 bce4d906 63a907b9 5c54c519 417e8a37 85809d16 27cf0242 b2cb82b0 e712409f 370639cf 3080a205 f15434d9 6e418207 ea8d87be d968e20f 8244ddc6 e1a1edec e121fc2a eca71f1e 020aa100 0f21d50b 819dee89 e6b10fa6 1f2c2567 e5efe183 7df9fb78 7f6516be 1b50376c f9363b4b 6188aa1e c844de0d 51b84533 51bac3f5 6e05002d b7d16bce 375f1286 8e2e46af c1dcff05 9255a543> Connection: <NSURLConnection: 0xf76a380> { request: <NSMutableURLRequest: 0xf77b830> { URL: https://www.youtube.com/v/Ek0SgwWmF9w?version=3&f=videos&app=youtube_gdata } } 
2014-04-11 16:55:23.058 AudioTube[5572:70b] dataaaaaaaaaaaa: CWSG 
2014-04-11 16:55:23.059 AudioTube[5572:70b] download finish connection: <NSURLConnection: 0xf76a380> { request: <NSMutableURLRequest: 0xf77b830> { URL: https://www.youtube.com/v/Ek0SgwWmF9w?  version=3&f=videos&app=youtube_gdata } } 

我不認爲這個數據日誌視頻的數據字節。 因此,這是我從YouTube上的進料方式:

GDataEntryBase *entry = (GDataFeedYouTubeVideo *)aFeed; //aFeed sarebbe un feed restituitomi da youtube 
GDataMediaContent *flashContent = [LDConnecting mediaContentOfEntry:entry]; 
NSString *tempURL = [flashContent URLString]; 

我這樣做是正確的方式? Feed的網址類型(https://www.youtube.com/v/Ek0SgwWmF9w?version = 3 & f = videos & app = youtube_gdata)是正確的嗎? 我錯在哪裏?

回答

0

我對這個問題看起來這麼久,最後我解決了它! html youtube頁面將鏈接編碼爲url_encoded_fmt_stream_map中歌曲文件的鏈接,因此您必須決定是否擁有該鏈接。在我的情況下,我正在尋找最小的videofile和我的代碼這樣做。所以我張貼它幫助其他人...

-(NSString*)directLinkToVodeoOfYouTubePage:(NSString*)html{ 
    if(!html) return nil; 
    html = [html stringByReplacingOccurrencesOfString:@"\\u0026" withString:@"&"]; 
    NSArray *split = [html componentsSeparatedByString:@"\""]; 
    int i; 
    for(i=0;i<[split count];i++){ 
     NSString *current = [split objectAtIndex:i]; 
     if([current isEqualToString:@"url_encoded_fmt_stream_map"]){ 
      html = [split objectAtIndex:i+2]; 
     } 
    } 
    split = [html componentsSeparatedByString:@","]; 
    for(NSString *current in split){ 
     if([current rangeOfString:@"quality=small"].location != NSNotFound) html = current; //here get my quality video 
    } 
    html = [[[html stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
       stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
       stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    html = [[html componentsSeparatedByString:@"url="] objectAtIndex:1]; 
    html = [[html componentsSeparatedByString:@";+codecs="] objectAtIndex:0]; 
    split = [html componentsSeparatedByString:@"&itag="]; 
    if([split count] > 2){ 
     html = [NSString stringWithFormat:@"%@&itag=%@", [split objectAtIndex:0], [split objectAtIndex:1]]; 
    } 
    return html; 
} 

當然這個代碼是因爲谷歌的變化deprecabile,像VEVO一些視頻有不同的encoded_stream_maps。但這是我能夠做到的。