从Youtube下载视频

时间:2014-04-23 09:35:56

标签: iphone objective-c ios7

我要使用PSYouTubeExtractor从Youtube下载视频。一切都很好,但保存的文件不包含任何视频。它只有283字节大小。我不知道我会出错什么。请帮助我对目标C不熟悉。任何回应都将受到赞赏。

这是我用于下载的代码

- (void)viewDidLoad {
[super viewDidLoad];

self.title = NSLocalizedString(@"YouTube", nil);

[self.navigationController.navigationBar setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  HELVETICA_NEUE_LIGHT_24,
  NSFontAttributeName, nil]];

self.youtubeUrl_Label.text = NSLocalizedString(@"Enter YouTube URL:", nil);
self.youtubeUrl_Label.font = HELVETICA_NEUE_LIGHT_15;

self.go_button.titleLabel.text = NSLocalizedString(@"GO", nil);
self.go_button.titleLabel.font = HELVETICA_NEUE_LIGHT_15;


extractor = [[PSYouTubeExtractor alloc] init]; }

/////////////////////////////////////////////// ///////

-(IBAction)go_button:(id)sender {

NSString* textFieldString = [NSString stringWithFormat:@"%@",self.textField.text];

NSURL *youTubeURL = [NSURL URLWithString:textFieldString];

[extractor getVideoUrlForUrl:youTubeURL notificatoinName:@"URLDidFinishExtractingFromYouTubeURL"];

//NSURLConnection will download the video
con = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:youTubeURL] delegate:self];}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
videoData = [[NSMutableData alloc] init];}

////////////////////////////////////

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[videoData appendData:data]; }

////////////////

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

NSString* mediaPath = [documentsDirectory stringByAppendingPathComponent:@"Media"];

if(![[NSFileManager defaultManager] fileExistsAtPath:mediaPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:mediaPath withIntermediateDirectories:NO attributes:nil error:nil];

NSString *filePath = [NSString stringWithFormat:@"%f.mp4",[[NSDate date] timeIntervalSince1970]];

NSString* finalPath = [mediaPath stringByAppendingPathComponent:filePath];

[videoData writeToFile:finalPath atomically:YES];

NSLog(@"LOCAL VIDEO LINK %@",filePath);}

1 个答案:

答案 0 :(得分:0)

XCDYouTubeVideoPlayerViewController是PSYouTubeExtractor的新版本。 PSYouTubeExtractor 2年前停产。 Youtube API可能会发生变化,从而使PSYouTubeExtractor失效。

如果您真的想调试问题,请查看NSURLResponse的用途:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

并打印您获得的数据(在NSString UTF8中)