Skip to content

Instantly share code, notes, and snippets.

@awojnowski
Created March 18, 2012 05:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awojnowski/2069100 to your computer and use it in GitHub Desktop.
Save awojnowski/2069100 to your computer and use it in GitHub Desktop.
YouTube URL .MP4 Grabber
-(void)viewDidLoad {
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor whiteColor]];
NSString *url = @"http://www.youtube.com/watch?v=8UVNT4wvIGY";
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] queue:[[[NSOperationQueue alloc] init] autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
// result now contains the HTML code for the YouTube video
result = [self mp4URLFromYoutubeContents:result];
NSLog(@"The MP4 URL: %@",result);
}];
}
-(NSString *)mp4URLFromYoutubeContents:(NSString *)string {
// used throughout the function to store the components of a string
// we store the components to check the length and optionally error handle
NSArray *arr;
// find where "var swf = " is declared
arr = [string componentsSeparatedByString:@"var swf = "];
if ([arr count] < 2) return nil;
string = [arr lastObject];
// copy before the "document.getElementById(..."
arr = [string componentsSeparatedByString:@"document.getElementById("];
if ([arr count] < 2) return nil;
string = [arr objectAtIndex:0];
// decode the url three times
string = [[[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// delete before "url_encoded_fmt_stream_map="
arr = [string componentsSeparatedByString:@"url_encoded_fmt_stream_map="];
if ([arr count] < 2) return nil;
string = [arr lastObject];
// get all of the URLs and clean them
NSMutableArray *urls = [[[string componentsSeparatedByString:@"url="] mutableCopy] autorelease];
[urls removeObjectAtIndex:0]; // first object is just an empty string
NSMutableArray *cleanURLS = [NSMutableArray array];
for (NSString *url in urls) {
url = [[url componentsSeparatedByString:@";+codecs="] objectAtIndex:0];
NSString *lastTag = [[url componentsSeparatedByString:@"&itag="] lastObject];
url = [[url componentsSeparatedByString:[NSString stringWithFormat:@"&itag=%@,",lastTag]] objectAtIndex:0];
[cleanURLS addObject:url];
}
// cleanURLS now contains all of our nice, shiny URLs. We want to return the .mp4 video
for (NSString *url in cleanURLS) {
if ([url rangeOfString:@"&type=video/mp4"].location != NSNotFound) return url;
}
return nil;
}
@danieleratti
Copy link

It doesn't works anymore :(
Tested both on iOS and Mac (with mobile and desktop user agent)

@simplyi
Copy link

simplyi commented Nov 30, 2013

Hi! Where you able to fix the project and make it work again?

@abrar006
Copy link

abrar006 commented Jan 6, 2014

I found it very simple, but it is so simple
Not working :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment