Save Youtube video to iPhone in the app

24.1k views Asked by At

Playing Youtube video in the app is easy and well documented around.

There are two problems with that:

  1. after closing Youtube player, if user wants to play it again it has to wait for online streaming again
  2. can't play offline (load video at home to watch on the road)

Does anyone have code to:

  1. download Youtube video to documents folder and show progress of download
  2. play downloaded video by loading file from documents folder (meaning even when not connected to the internet)
6

There are 6 answers

2
Sean On

I would play the video and figure out where the temp file is being stored. If you can get access to it, copy it into some document folder for offline viewing.

3
Petr On

Ive used classes from this project: https://github.com/larcus94/LBYouTubeView It works fine for me. I can download youtube videos.

I used this code:

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
    if(!error) {
        NSLog(@"Did extract video URL using completion block: %@", videoURL);

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL: videoURL];
            NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ];
            [data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
            NSLog(@"File %@ successfully saved", filename);
        });
    } else {
        NSLog(@"Failed extracting video URL using block due to error:%@", error);
    }
}];

You can show progress of downloading using technique described in the posts above.

6
Anomie On

To download the video from YouTube:

  1. Get the URL to download from, via the YouTube API or whatever other method.
  2. Create an NSOutputStream or NSFileHandle opened on a temporary file (in NSTemporaryDirectory() or a temp-named file in your Documents directory).
  3. Set up your progress bar and whatever else you need to do.
  4. Allocate and start an NSURLConnection to fetch the file from the URL. Do not use sendSynchronousRequest:returningResponse:error:, of course.
  5. In the connection:didReceiveResponse: delegate method, read out the length of data to be downloaded for proper updating of the progress bar.
  6. In the connection:didReceiveData: delegate method, write the data to the output stream/file handle and update the progress bar as necessary.
  7. In connectionDidFinishLoading: or connection:didFailWithError:, close the output stream/file handle and rename or delete the temporary file as appropriate.

To play it back, just use NSURL's fileURLWithPath: to create a URL pointing to the local file in the Documents directory and play it as you would any remote video.

1
Kyle Howells On

I don't personally know how to download youtube videos (and the code is too big to put in an answer here).

However, here's a complete youtube downloading example here. It's an open source youtube downloader called LoadTube: here's the a link to the source code.

1
Saurabh On

check these projects -

https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube

these will definitely help you!!

0
Tim Kozak On

Here is my example: https://github.com/comonitos/youtube_video

I used PSYouTubeExtractor.h class by Peter Steinberger It can get youtube mp4 video url and than downloading and viewing is not a problem

NSURLConnection + NSNotificationCenter + PSYouTubeExtractor + NSMutableData