¿Cómo extraer imágenes de un video?

¿Alguien puede decirme cómo extraer imágenes de un video? Lo que he intentado hasta ahora es seguir estos:

  • Obtener miniaturas de videos de iPhone
  • Obtener una miniatura de una url de video o datos en el iPhone SDK
  • ¿Cómo extraigo una captura de pantalla de un video en el iPhone SDK?
  • iPhone Lee UIimage (frames) desde video con AVFoundation , iphone sdk> 3.0. ¿Miniatura de video?

etc. y después de eso he hecho esto:

Código fuente:

- (void)viewDidLoad { videoPicker = [[UIImagePickerController alloc] init]; [videoPicker setDelegate:self]; videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; videoPicker.mediaTypes = mediaTypesAllowed; videoPicker.view.hidden = YES; } -(IBAction)imgPickerController { [self presentModalViewController:videoPicker animated:YES]; videoPicker.view.hidden = NO; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // eg NSString *tempFilePath = [(NSURL *)[info valueForKey:UIImagePickerControllerMediaURL] absoluteString]; NSLog(@"didFinishPickingMediaWithInfo: %@",tempFilePath); // eg /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp/capture/captunetworkingvideo.MOV tempFilePath = [tempFilePath substringFromIndex:16]; NSLog(@"didFinishPickingMediaWithInfo: %@",tempFilePath); NSLog(@"===Try to save video to camera roll.==="); NSLog(@"UIVideoAtPathIsCompatibleWithSavedPhotosAlbum: %@",UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath)? @"YES":@"NO"); // Check if the video file can be saved to camera roll. if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath)) { // YES. Copy it to the camera roll. UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:),(__bridge_retained void *)tempFilePath ); } [self dismissModalViewControllerAnimated:YES]; } - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo { NSLog(@"didFinishSavingWithError--videoPath in camera roll:%@",videoPath); NSLog(@"didFinishSavingWithError--videoPath in temp directory:%@",contextInfo); // The thumbnail jpg should located in this directory. NSString *thumbnailDirectory = [[contextInfo stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]; // Debug info. list all files in the directory of the video file. // eg /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp/capture NSLog(@"%@",[contextInfo stringByDeletingLastPathComponent]); NSLog(@"%@",[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[contextInfo stringByDeletingLastPathComponent] error:nil] description]); // Debug info. list all files in the parent directory of the video file, ie the "~/tmp" directory. // eg /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp NSLog(@"%@",thumbnailDirectory); NSLog(@"%@",[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:thumbnailDirectory error:nil] description]); /////////////////// // Find the thumbnail for the video just recorded. NSString *file,*latestFile; NSDate *latestDate = [NSDate distantPast]; NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:[[contextInfo stringByDeletingLastPathComponent]stringByDeletingLastPathComponent]]; // Enumerate all files in the ~/tmp directory while (file = [dirEnum nextObject]) { // Only check files with jpg extension. if ([[file pathExtension] isEqualToString: @"jpg"]) { NSLog(@"***latestDate:%@",latestDate); NSLog(@"***file name:%@",file); NSLog(@"***NSFileSize:%@", [[dirEnum fileAttributes] valueForKey:@"NSFileSize"]); NSLog(@"***NSFileModificationDate:%@", [[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"]); // Check if current jpg file is the latest one. if ([(NSDate *)[[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"] compare:latestDate] == NSOrdenetworkingDescending) { latestDate = [[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"]; latestFile = file; NSLog(@"***latestFile changed:%@",latestFile); } } } // The thumbnail path. latestFile = [NSTemporaryDirectory() stringByAppendingPathComponent:latestFile]; NSLog(@"****** The thumbnail file should be this one:%@",latestFile); UIImage *img = [[UIImage alloc] initWithContentsOfFile:latestFile]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)]; [imgView setImage:img]; imgView.layer.borderWidth = 2.0; [self.view addSubview:imgView]; // Your code ... // Your code ... // Your code ... } 

Después de hacer todo esto, seguí llegando a donde quiero llegar. Si aún recibo imágenes, estoy atascado ahora. Pls, alguien me ayude !!! Gracias 🙂

Una manera simple de extraer miniaturas de una película es utilizar la class MPMoviePlayerController y su método - thumbnailImageAtTime:timeOption:

Por ejemplo, tienes una URL de ruta de file:

 NSURL *tempFilePathURL = ...; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: tempFilePathURL]; UIImage *thumbnail = [player thumbnailImageAtTime:THUMBNAIL_TIME timeOption:MPMovieTimeOptionExact]; [player release]; 

Lo usé en mi código y funcionó.

thumbnailImageAtTime: timeOption se aplica desde iOS 7.0. Aquí hay una alternativa

 + (UIImage *)extractFirstFrameFromFilepath:(NSString *)filepath { AVURLAsset *movieAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:filepath] options:nil]; AVAssetImageGenerator *assetImageGemerator = [[AVAssetImageGenerator alloc] initWithAsset:movieAsset]; assetImageGemerator.appliesPrefernetworkingTrackTransform = YES; CGImageRef frameRef = [assetImageGemerator copyCGImageAtTime:CMTimeMake(1, 2) actualTime:nil error:nil]; return [[UIImage alloc] initWithCGImage:frameRef]; }