Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video download from HTML + UIWebView

In my new application i need to download video from different-different web sites, Say it is video downloader application. For this what i am planing is Searching html for .mp4 and .Flv url and then trying to downloading videos. There are many app already doing same

http://itunes.apple.com/in/app/video-downloader-super-lite/id481701140?mt=8

What i am asking is, how can we download video? Any code or link or something. how this application work ? Any Help will really appreciate.

what i needed is when you open a page in UIWebview Say you open "www.youtube.com" and select a video to play then it ask to download. For download i need URL (Embeded url, Flv url, mpv url) so i can paas this to function. I need to know about That URL

like image 683
Mangesh Avatar asked Sep 07 '12 13:09

Mangesh


2 Answers

If you are able to use the AFNetworking library, it's pretty simple. You can make an HTTP request and use its outputStream property to download the file to your device. Say you hook up a download button to a function downloadVideoFromURL:withName:

- (void)downloadVideoFromURL:(NSURL*)url withName:(NSString*)videoName
{
    //filepath to your app's documents directory
    NSString *appDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *videosPath = [appDocPath stringByAppendingPathComponent:@"Videos"];
    NSString *filePath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", videoName]];

    //check to make sure video hasn't been downloaded already
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        //file was already downloaded
    }

    //video wasn't downloaded, so continue
    else
    {

        //enable the network activity indicator
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

        //create a temporary filepath while downloading
        NSString *tmpPath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-tmp.mp4", videoName]];

        //the outputStream property is the key to downloading the file
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:tmpPath append:NO];

        //if operation is completed successfully, do following
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            //rename the downloaded video to its proper name
            [[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:filePath error:nil];

            //disable network activity indicator
            [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;

            //optionally, post a notification to anyone listening that the download was successful
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadedVideo" object:nil];

        //if the operation fails, do the following:
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"error: %@", error);

            //delete the downloaded file (it is probably partially downloaded or corrupt)
            [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];

            //disable network activity indicator
            [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
        }];

        //start the operation
        [operation start];

    }
}
like image 175
lobianco Avatar answered Sep 29 '22 05:09

lobianco


if you really want to go for hacking , you you will get apple's private library, "webkit" , if you try to find the subviews of UIWebview, it might help you, i never tried it, but you can test with this logic.

like image 28
Jenis Avatar answered Sep 29 '22 05:09

Jenis