Skip to content

Commit fe5d72c

Browse files
Simone Manganellirentzsch
Simone Manganelli
authored andcommitted
H.264 now works with embedded YouTube videos; we have to HTML scrape for this, though, and that adds ~100 KiB of extra data to download for each embedded YouTube video; I think that's acceptable
Signed-off-by: Jonathan 'Wolf' Rentzsch <[email protected]>
1 parent 2c68335 commit fe5d72c

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

Plugin/Plugin.m

+57-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ - (void) _loadContent: (NSNotification*) notification;
6969
- (void) _loadContentForWindow: (NSNotification*) notification;
7070

7171
- (NSDictionary*) _flashVarDictionary: (NSString*) flashvarString;
72+
- (NSDictionary*) _flashVarDictionaryFromYouTubePageHTML: (NSString*) youTubePageHTML;
7273
- (NSString*) flashvarWithName: (NSString*) argName;
7374
- (BOOL) _hasH264Version;
7475
- (BOOL) _useH264Version;
@@ -169,7 +170,9 @@ - (id) initWithArguments:(NSDictionary *)arguments
169170
if (videoId != nil) {
170171
[self setVideoId:videoId];
171172
} else {
172-
// scrub the URL to determine the video_id
173+
// it's an embedded YouTube flash view; scrub the URL to
174+
// determine the video_id, then get the source of the YouTube
175+
// page to get the Flash vars
173176

174177
NSString *videoIdFromURL = nil;
175178
NSScanner *URLScanner = [[NSScanner alloc] initWithString:[self src]];
@@ -181,6 +184,16 @@ - (id) initWithArguments:(NSDictionary *)arguments
181184
if (videoIdFromURL) [self setVideoId:videoIdFromURL];
182185
}
183186
[URLScanner release];
187+
188+
if (videoIdFromURL) {
189+
NSString *URLString = [NSString stringWithFormat:@"http://youtube.com/watch?v=%@",videoIdFromURL];
190+
NSURL *YouTubePageURL = [NSURL URLWithString:URLString];
191+
NSError *pageSourceError = nil;
192+
NSString *pageSourceString = [NSString stringWithContentsOfURL:YouTubePageURL
193+
usedEncoding:nil
194+
error:&pageSourceError];
195+
if (! pageSourceError) _flashVars = [[self _flashVarDictionaryFromYouTubePageHTML:pageSourceString] retain];
196+
}
184197
}
185198
}
186199

@@ -925,6 +938,49 @@ - (NSDictionary*) _flashVarDictionary: (NSString*) flashvarString
925938
return flashVarsDictionary;
926939
}
927940

941+
- (NSDictionary*) _flashVarDictionaryFromYouTubePageHTML: (NSString*) youTubePageHTML
942+
{
943+
NSMutableDictionary* flashVarsDictionary = [ NSMutableDictionary dictionary ];
944+
NSScanner *HTMLScanner = [[NSScanner alloc] initWithString:youTubePageHTML];
945+
946+
[HTMLScanner scanUpToString:@"var swfArgs = {" intoString:nil];
947+
BOOL swfArgsFound = [HTMLScanner scanString:@"var swfArgs = {" intoString:nil];
948+
949+
if (swfArgsFound) {
950+
NSString *swfArgsString = nil;
951+
[HTMLScanner scanUpToString:@"}" intoString:&swfArgsString];
952+
NSArray *arrayOfSWFArgs = [swfArgsString componentsSeparatedByString:@", "];
953+
CTFForEachObject( NSString, currentArgPairString, arrayOfSWFArgs ) {
954+
NSRange sepRange = [ currentArgPairString rangeOfString:@": "];
955+
if (sepRange.location != NSNotFound) {
956+
NSString *potentialKey = [currentArgPairString substringToIndex:sepRange.location];
957+
NSString *potentialVal = [currentArgPairString substringFromIndex:NSMaxRange(sepRange)];
958+
959+
// we might need to strip the surrounding quotes from the keys and values
960+
// (but not always)
961+
NSString *key = nil;
962+
if ([[potentialKey substringToIndex:1] isEqualToString:@"\""]) {
963+
key = [potentialKey substringWithRange:NSMakeRange(1,[potentialKey length] - 2)];
964+
} else {
965+
key = potentialKey;
966+
}
967+
968+
NSString *val = nil;
969+
if ([[potentialVal substringToIndex:1] isEqualToString:@"\""]) {
970+
val = [potentialVal substringWithRange:NSMakeRange(1,[potentialVal length] - 2)];
971+
} else {
972+
val = potentialVal;
973+
}
974+
975+
[flashVarsDictionary setObject:val forKey:key];
976+
}
977+
}
978+
}
979+
980+
[HTMLScanner release];
981+
return flashVarsDictionary;
982+
}
983+
928984
- (NSString*) flashvarWithName: (NSString*) argName
929985
{
930986
return [ _flashVars objectForKey: argName ];
@@ -1073,7 +1129,6 @@ - (IBAction)openFullscreenInQTPlayer:(id)sender;
10731129

10741130
NSString *scriptSource = [NSString stringWithFormat:
10751131
@"tell application \"QuickTime Player\"\nactivate\ngetURL \"%@\"\nrepeat while (display state of front document is not presentation)\ndelay 1\npresent front document scale screen\nend repeat\nrepeat while (playing of front document is false)\ndelay 1\nplay front document\nend repeat\nend tell",src];
1076-
NSLog(@"%@",scriptSource);
10771132
NSAppleScript *openInQTPlayerScript = [[NSAppleScript alloc] initWithSource:scriptSource];
10781133
[openInQTPlayerScript executeAndReturnError:nil];
10791134
[openInQTPlayerScript release];

0 commit comments

Comments
 (0)