Search code examples
iphoneobjective-cios7

Download video from Youtube


I'm going to download video from Youtube using PSYouTubeExtractor. Every thing is fine but the saved file does not contain any video. It has 283 byte size only. I don't know what thing I'm going to wrong. Please help I'm new to objective C. Any kind of response will be appreciated.

this is code that I've used for downloading

- (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);}

Solution

  • XCDYouTubeVideoPlayerViewController Is the new version of PSYouTubeExtractor. PSYouTubeExtractor was discontinued 2 years ago. It is possible that the Youtube API changed, thus making the PSYouTubeExtractor useless.

    If you really want to debug the problem, check out what the NSURLResponse is in:

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

    And print the data you get (in NSString UTF8)