7

私の新しいアプリケーションでは、さまざまな Web サイトからビデオをダウンロードする必要があります。ビデオ ダウンローダー アプリケーションだとします。このために私が計画しているのは、html で .mp4 と .Flv url を検索してから、ビデオをダウンロードしようとすることです。すでに同じことをしている多くのアプリがあります

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

どうすればビデオをダウンロードできますか? コードやリンクなど。このアプリケーションはどのように機能しますか? どんな助けでも本当に感謝します。

私が必要としていたのは、UIWebview でページを開いたときです。たとえば、「www.youtube.com」を開いて、再生するビデオを選択すると、ダウンロードを求められます。ダウンロードには URL (埋め込み URL、Flv URL、mpv URL) が必要なので、これを機能させることができます。そのURLについて知りたい

4

2 に答える 2

1

AFNetworkingライブラリを使用できる場合は、非常に簡単です。HTTP 要求を作成し、そのoutputStreamプロパティを使用してファイルをデバイスにダウンロードできます。ダウンロードボタンを関数に接続するとします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];

    }
}
于 2012-09-07T14:20:12.403 に答える
1

本当にハッキングしたい場合は、Apple のプライベート ライブラリ "webkit" を取得します。UIWebview のサブビューを見つけようとすると、役立つかもしれません。試したことはありませんが、このロジックでテストできます。

于 2013-02-14T12:26:13.873 に答える