7

我需要从 youtube 等视频网站下载和保存视频。我想将它们保存到 iPhone 的视频库中。如何检测视频并将其保存到库中?我已经检查了一些可用的源代码。这是我在下载操作中所做的,但不起作用。

- (IBAction)download {
    [downloadButton setEnabled:NO];

    [webView setUserInteractionEnabled:NO];

    UIUserInterfaceIdiom userInterfaceIdiom = [UIDevice currentDevice].userInterfaceIdiom;

    NSString *getURL = @"";

    if (userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        getURL = [webView stringByEvaluatingJavaScriptFromString:@"function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();"];
    } else {
        getURL = [webView stringByEvaluatingJavaScriptFromString:@"function getURL() {var bh = document.getElementsByClassName('bh'); if (bh.length) {return bh[0].getAttribute('src');} else {var zq = document.getElementsByClassName('zq')[0]; return zq.getAttribute('src');}} getURL();"];
    }

    NSString *getTitle = [webView stringByEvaluatingJavaScriptFromString:@"function getTitle() {var jm = document.getElementsByClassName('jm'); if (jm.length) {return jm[0].innerHTML;} else {var lp = document.getElementsByClassName('lp')[0]; return lp.childNodes[0].innerHTML;}} getTitle();"];

    NSString *getTitleFromChannel = [webView stringByEvaluatingJavaScriptFromString:@"function getTitleFromChannel() {var video_title = document.getElementById('video_title'); return video_title.childNodes[0].innerHTML;} getTitleFromChannel();"];

    NSLog(@"%@, %@, %@", getURL, getTitle, getTitleFromChannel);

    [webView setUserInteractionEnabled:YES];

    NSArray *components = [getTitle componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];
    getTitle = [components componentsJoinedByString:@" "];

    if ([getURL length] > 0) {
        if ([getTitle length] > 0) {
            videoTitle = [getTitle retain];

            bar = [[UIDownloadBar alloc] initWithURL:[NSURL URLWithString:getURL]
                                    progressBarFrame:CGRectMake(85.0, 17.0, 150.0, 11.0)
                                             timeout:15
                                            delegate:self];

            [bar setProgressViewStyle:UIProgressViewStyleBar];

            [toolbar addSubview:bar];
        } else {
            NSArray *components = [getTitleFromChannel componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]];
            getTitleFromChannel = [components componentsJoinedByString:@" "];

            if ([getTitleFromChannel length] > 0) {
                videoTitle = [getTitleFromChannel retain];

                bar = [[UIDownloadBar alloc] initWithURL:[NSURL URLWithString:getURL]
                                        progressBarFrame:CGRectMake(85.0, 17.0, 150.0, 11.0)
                                                 timeout:15
                                                delegate:self];

                [bar setProgressViewStyle:UIProgressViewStyleBar];

                [toolbar addSubview:bar];
            } else {
                //NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyTube" message:@"Couldn't get video title." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

                [alertView show];
                [alertView release];

                [downloadButton setEnabled:YES];
            }
        }
    } else {
        //NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyTube" message:@"Couldn't get MP4 URL." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];
        [alertView release];

        [downloadButton setEnabled:YES];
    }
}
4

2 回答 2

3

如果您一直希望它们采用 <object> 格式,这将获取视频 ID。如果正则表达式在 iframe 中,显然需要更改它。

NSError *error = nil;
NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:@"<object .*name=\"movie\" value=\"[a-zA-z:\\/]*.com\\/\\w*\\/([A-Za-z0-9\\_-]*).*<\\/object>"
                                          options:NSRegularExpressionCaseInsensitive
                                            error:&error];

[regex enumerateMatchesInString:pageSource options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    NSString *videoID = [pageSource substringWithRange:[match rangeAtIndex:1]];        
}];
于 2012-08-16T03:11:03.770 回答
2

您可以将下载的视频保存到相册或应用程序目录本身。要下载视频,您必须生成相应视频的下载链接。如果从网站 youtube 下载视频,请参阅此源代码 https:// github.com/comonitos/youtube_video。有些网站是下载受限的,可能无法下载。在所有其他网站的情况下,通过生成可下载的 url,您可以使其工作

于 2012-08-14T11:34:55.103 回答