can anyone tell me how to extract images from a video? what i ve tried till now is i ve follow these:
有人能告诉我如何从视频中提取图像吗?到目前为止,我所做的努力是:
- Getting iPhone video thumbnails
- 让iPhone视频缩略图
- Getting a thumbnail from a video url or data in iPhone SDK
- 从iPhone SDK中的视频url或数据获取缩略图
- How do I extract a screenshot from a video in the iPhone SDK?
- 如何从iPhone SDK中的视频中提取截图?
- iPhone Read UIimage (frames) from video with AVFoundation , iphone sdk > 3.0 . Video Thumbnail?
- iPhone通过AVFoundation, iPhone sdk > 3.0从视频中读取UIimage(框架)。视频缩略图吗?
etc. and after that i ve done this:
在那之后,我做了:
Source Code:
源代码:
- (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
{
// e.g.
NSString *tempFilePath = [(NSURL *)[info valueForKey:UIImagePickerControllerMediaURL] absoluteString];
NSLog(@"didFinishPickingMediaWithInfo: %@",tempFilePath);
// e.g. /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp/capture/capturedvideo.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.
// e.g. /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, i.e. the "~/tmp" directory.
// e.g. /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] == NSOrderedDescending)
{
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 ...
}
After doing all this still i dint reached where i want to reach.i dint get images still.I'm stuck now.Pls anyone help me out!!!Thanks :)
做了这一切之后,我仍然没有到达我想要到达的地方。我还没有得到图像。我现在困了。谁来帮帮我!!谢谢:)
2 个解决方案
#1
12
Simple way to extract thumbnails from a movie is to use MPMoviePlayerController
class and its - thumbnailImageAtTime:timeOption:
method.
从电影中提取缩略图的简单方法是使用MPMoviePlayerController类及其- thumbnailimattime:timeOption:方法。
For example you've got a filepath url:
例如,你有一个文件路径url:
NSURL *tempFilePathURL = ...;
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: tempFilePathURL];
UIImage *thumbnail = [player thumbnailImageAtTime:THUMBNAIL_TIME timeOption:MPMovieTimeOptionExact];
[player release];
I used it in my code and it worked.
我在我的代码中使用了它,它起作用了。
#2
4
thumbnailImageAtTime:timeOption is depricated since iOS 7.0. Here is an alternative
thumbnailImageAtTime:timeOption从ios7.0开始被取消。这是一个替代
+ (UIImage *)extractFirstFrameFromFilepath:(NSString *)filepath
{
AVURLAsset *movieAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:filepath] options:nil];
AVAssetImageGenerator *assetImageGemerator = [[AVAssetImageGenerator alloc] initWithAsset:movieAsset];
assetImageGemerator.appliesPreferredTrackTransform = YES;
CGImageRef frameRef = [assetImageGemerator copyCGImageAtTime:CMTimeMake(1, 2) actualTime:nil error:nil];
return [[UIImage alloc] initWithCGImage:frameRef];
}
#1
12
Simple way to extract thumbnails from a movie is to use MPMoviePlayerController
class and its - thumbnailImageAtTime:timeOption:
method.
从电影中提取缩略图的简单方法是使用MPMoviePlayerController类及其- thumbnailimattime:timeOption:方法。
For example you've got a filepath url:
例如,你有一个文件路径url:
NSURL *tempFilePathURL = ...;
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: tempFilePathURL];
UIImage *thumbnail = [player thumbnailImageAtTime:THUMBNAIL_TIME timeOption:MPMovieTimeOptionExact];
[player release];
I used it in my code and it worked.
我在我的代码中使用了它,它起作用了。
#2
4
thumbnailImageAtTime:timeOption is depricated since iOS 7.0. Here is an alternative
thumbnailImageAtTime:timeOption从ios7.0开始被取消。这是一个替代
+ (UIImage *)extractFirstFrameFromFilepath:(NSString *)filepath
{
AVURLAsset *movieAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:filepath] options:nil];
AVAssetImageGenerator *assetImageGemerator = [[AVAssetImageGenerator alloc] initWithAsset:movieAsset];
assetImageGemerator.appliesPreferredTrackTransform = YES;
CGImageRef frameRef = [assetImageGemerator copyCGImageAtTime:CMTimeMake(1, 2) actualTime:nil error:nil];
return [[UIImage alloc] initWithCGImage:frameRef];
}