2012-07-18 104 views
7

我需要下載並保存視頻網站(如YouTube)的視頻。我想將它們保存到iPhone的視頻庫中。如何檢測視頻並將它們保存到庫中?我已經檢查了一些可用的源代碼。這是我在下載操作中所做的,但不起作用。以目標c編程方式檢測youtube視頻鏈接

- (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]; 
    } 
} 
+0

我也需要同樣的事情,因爲YouTube包含的JavaScript,而不是HTTP或HTTPS reqeust – dark 2012-08-14 07:49:07

+1

當你說「它不工作」,你可以更具體一點嗎? – Abizern 2012-08-14 07:50:58

+0

@alpz有沒有更新? – 2012-08-30 04:36:37

回答

2

可以節省下載的視頻無論是照片的相冊或app目錄itself.For視頻下載u必須生成相應的video.In從網站YouTube下載視頻的情況下的downloadble鏈接,請參閱此源代碼https://github.com/comonitos/youtube_video。有些網站是可能無法下載的限制網站。在所有其他網站通過生成可下載網址的情況下,您可以使其工作

3

如果您始終期望它們位於<對象>格式。 正則表達式顯然需要在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]];   
}]; 
+1

爲什麼downvote? – endy 2012-08-17 15:02:36