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