Last active
December 10, 2015 14:09
-
-
Save awojnowski/4445875 to your computer and use it in GitHub Desktop.
YouTube MP4 Grabber 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(NSString *)mp4URLFromYoutubeContents:(NSString *)string error:(NSError **)error { | |
@try { | |
NSString * (^getGETVariable)(NSString *url, NSString *val) = ^(NSString *url, NSString *val){ | |
NSArray *array = [url componentsSeparatedByString:[NSString stringWithFormat:@"%@=",val]]; | |
if ([array count] <= 1) return (NSString *)nil; | |
val = [array[1] componentsSeparatedByString:@"&"][0]; | |
val = [val stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
return val; | |
}; | |
// first we shall find if the video is available and under 18 | |
if ([string rangeOfString:@"<p>This video may be inappropriate for some users.</p>"].location != NSNotFound) { | |
*error = [NSError errorWithDomain:@"MusiParser" code:1 userInfo:@{ NSLocalizedDescriptionKey : @"This song requires you to be over the age of 18. Musi can't stream it at this time." }]; | |
return nil; | |
} | |
// now that the video is there, we can parse the urls | |
NSArray *array = [string componentsSeparatedByString:@"url_encoded_fmt_stream_map="]; | |
NSString *urlStreamMap = [array[1] componentsSeparatedByString:@"\""][0]; | |
urlStreamMap = [urlStreamMap stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSArray *streams = [urlStreamMap componentsSeparatedByString:@","]; | |
// for Musi, I like to get the lowest quality video for maximum streaming potential | |
NSArray *qualities = @[@"small", @"medium", @"hd720", @"hd1080"]; | |
NSString *bestURL = nil; | |
int lastIndex = INT_MAX; | |
for (NSString __strong *stream in streams) { | |
NSString *type = getGETVariable(stream, @"type"); | |
NSString *quality = getGETVariable(stream, @"quality"); | |
NSString *signature = getGETVariable(stream, @"sig"); | |
NSString *url = getGETVariable(stream, @"url"); | |
url = [url stringByAppendingString:[NSString stringWithFormat:@"&signature=%@",signature]]; | |
if ([type rangeOfString:@"video/mp4"].location != NSNotFound) { | |
int index = [qualities indexOfObject:quality]; | |
if (index < lastIndex) { | |
bestURL = url; | |
} | |
lastIndex = index; | |
} | |
} | |
NSLog(@"[MusiParser] Returning Best URL to stream as: %@",bestURL); | |
if (!bestURL) { | |
*error = [NSError errorWithDomain:@"MusiParser" code:1 userInfo:@{ NSLocalizedDescriptionKey : @"No URL was found for streaming. Are you sure this song can be played?" }]; | |
} | |
return bestURL; | |
} | |
@catch (NSException *e) { | |
*error = [NSError errorWithDomain:@"MusiParser" code:1 userInfo:@{ NSLocalizedDescriptionKey : @"No URL was found for streaming. Are you sure this song can be played?" }]; | |
return nil; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment