GithubHelp home page GithubHelp logo

sejas / psyoutubeextractor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from steipete/psyoutubeextractor

1.0 1.0 0.0 79 KB

Display YouTube URLs in a MPMoviePlayerController

Home Page: http://twitter.com/steipete

License: Other

psyoutubeextractor's Introduction

PSYouTubeExtractor ("BetterYouTube")

Displaying YouTube is a pain in the ass. This class makes it a lot more bearable by trying to extract the native mp4 when available. If that doesn't work, we fall back to the UIWebView YouTube plugin.

There are two classes available:

PSYouTubeExtractor

Does some crazy things behind the scenes and extracts the mp4 of a YouTube video. I use an UIWebView to get the data, as Google does a pretty good job of obfuscating their html content.

    [PSYouTubeExtractor extractorForYouTubeURL:self.youTubeURL success:^(NSURL *URL) {
        NSLog(@"Finished extracting: %@", URL);
		// show the movie!
    } failure:^(NSError *error) {
        NSLog(@"Failed to query mp4: %@", error);
    }];

Note that PSYouTubeExtractor is not a NSOperation, as there's some craziness behind the scenes that needs a RunLoop (and I don't wanna mess around with runloops in NSOperation). The class retains itself until either success or failure is called, or until you send cancel to it. The blocks are nullified afterwards, so don't worry about retain cycles. (You still have to worry about Xcode bit chin' about it.)

PSYouTubeView

Woohoo! That's where the awesomeness is. Just use this instead of your UIWebView and you're good.

NSURL *youTubeURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=Vo0Cazxj_yc"];  

PSYouTubeView *youTubeView = [[PSYouTubeView alloc] initWithYouTubeURL:youTubeURL
                                                                 frame:CGRectMake(0,0,200,200)
                                                       showNativeFirst:YES];
[self.view addSubview:youTubeView];

Note that you should set the correct frame right away. If we need to fallback to UIWebView, the YouTube plugin can't resize. (You can recreate it, but that would kill a running video). However, in most cases it should extract the mp4 successfully and you don't need to worry about that crap.

The setting 'showNativeFirst' decides if you want to start with a MPMoviePlayerController or a UIWebView. As we are optimistic, I suggest you set this to YES per default.

I am using this class for PSPDFKit, my pdf framework where you can add interactive elements, and YouTube just sucked too much, so I wrote this helper. That's also why there is a block for "setupNativeView" and "setupWebView", you can override those and do your own custom stuff with it.

ARC

โ€ฆ what else? Also should work with iOS 4.0 upwards. Tested with iOS 4.3 and 5.0.

Help wanted!

If anyone has a better way of extracting the final YouTube mp4 url (maybe some crazy regex magic), it would make the class a lot faster (we could get rid of the UIWebView). I am kinda ok with the UIWebView solution though, as this one will be pretty robust. I look for a

Also, we could add support for Reachability to re-try the extracting in case we didn't had network when the view was created. Feel free to send a pull request!

PS: If you like this stuff, you should follow me on Twitter!

License

MIT! See LICENSE file for the legal stuff.

psyoutubeextractor's People

Contributors

steipete avatar

Stargazers

Antonio Sejas avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.