从我所读到的苹果不会公开api,允许开发人员使用当前的sdk获取电影的缩略图。
任何人都可以分享一些关于他们如何处理的代码吗? 我听说你可以访问相机选择器视图,并在ui图像选择器关闭之前找到图像视图。这看起来有点难看。
我也考虑过使用ffmpeg从电影中抓取一个帧,但不知道如何将其编译为iphone的库。任何指针都将非常感激
答案 0 :(得分:10)
希望我的代码可以帮助你们。它很丑。我认为苹果应该打开这种API。 当然,应删除所有NSLog()。这只是为了演示。
阿尔文
- (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] retain];
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:), 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);
// Your code ...
// Your code ...
// Your code ...
}
答案 1 :(得分:8)
在与缩略图的电影相同的文件夹中有一个jpg。我只用手机录制的视频测试过,但效果很好。它的名字与电影的名称不同,因此请获取电影所在的目录路径并迭代.jpg并离开。
答案 2 :(得分:5)
我发现的最佳方法... MPMoviePlayerController thumbnailImageAtTime:timeOption
答案 3 :(得分:5)
NSString *videoLink=[info objectForKey:@"UIImagePickerControllerMediaURL"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:(NSURL*)videoLink];
UIImage *imageSel = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[player stop];
[player release];
答案 4 :(得分:2)
这是一篇关于使用ffmpeg从电影中提取帧的博文:
http://www.codza.com/extracting-frames-from-movies-on-iphone
github上的相应项目:iFrameExtractor
答案 5 :(得分:1)
只想提供此更新。当您将选择器设置为UIImagePickerControllerSourceTypeCamera时,在获取缩略图方面似乎有用。但是,当您尝试从现有库中选择时(即UIImagePickerControllerSourceTypePhotoLibrary) 缩略图的.jpg永远不会被创建。我甚至尝试用UISaveVideoAtPathToSavedPhotosAlbum(...)重新保存,但仍然没有骰子。看起来像是在捕获视频时首先创建缩略图。这是你“抓住”它的唯一机会。之后它会被移动到/ var / mobile / Media / DCIM /的子目录中。虽然您实际上可以列出缩略图并看到它存在,但该文件是不可读和不可复制的,因为我认为目录受到保护。
如果有人有解决方法,我会非常感谢,因为在我的情况下,我需要从库中选择现有视频的缩略图。
答案 6 :(得分:0)
如果您使用UIImagePickerController获取图像,那么JPG将不会存储在tmp目录中。我不确定这个问题的答案,但是ffmpeg,更具体地说是它背后的库,可能是你的选择。