简体   繁体   English

iphone sdk> 3.0。 视频缩略图?

[英]iphone sdk > 3.0 . Video Thumbnail?

From what i have read apple doesnt expose the api to allow developers to get a thumbnail of a movie using the current sdk. 从我所读到的苹果不暴露api允许开发人员使用当前的sdk获取电影的缩略图。

Can any one share some code as to how they are going about this? 任何人都可以分享一些关于他们如何处理这个问题的代码吗? I have heard you can access the camera picker view and find the image view just before the ui image picker is closed. 我听说你可以访问相机选择器视图,并在ui图像选择器关闭之前找到图像视图。 This seems kinda ugly. 这看起来有点难看。

Also ive considered using ffmpeg to grab a frame out of the movie but wouldnt hava a clue as to how to compile it as a library for the iphone. 我还考虑过使用ffmpeg从电影中抓取一个帧但是不知道如何将其编译为iphone的库。 Any pointers would be greatly appreciated 任何指针都将非常感激

Hope my code could help you guys. 希望我的代码可以帮助你们。 It's ugly. 它很丑。 I think apple should open this kind of APIs. 我认为苹果应该打开这种API。 Of course, all NSLog() should be removed. 当然,应删除所有NSLog()。 It's just for demonstration. 这只是为了演示。

alvin 阿尔文

- (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 ...
}

There is a jpg in the same folder as the the movie that is the thumbnail. 在与缩略图的电影相同的文件夹中有一个jpg。 I have only tested it with video recorded from the phone, but it works fine. 我只用手机录制的视频测试过,但效果很好。 Its not named the same as the movie, so get the directory path the movie is in and iterate for a .jpg and away you go. 它的名字与电影的名称不同,因此请获取电影所在的目录路径并迭代.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];

Here's a blog post about extracting frames from movies using ffmpeg: 这是一篇关于使用ffmpeg从电影中提取帧的博客文章:

http://www.codza.com/extracting-frames-from-movies-on-iphone http://www.codza.com/extracting-frames-from-movies-on-iphone

The corresponding project on github: iFrameExtractor github上的相应项目: iFrameExtractor

Just wanted to provide an update on this. 只是想提供一个更新。 Things seem to work in terms of getting the thumbnail when you've set your picker to UIImagePickerControllerSourceTypeCamera. 当您将选择器设置为UIImagePickerControllerSourceTypeCamera时,在获取缩略图方面似乎有用。 However when you try to pick from the existing library (ie UIImagePickerControllerSourceTypePhotoLibrary) the .jpg for the thumbnail never gets created. 但是,当您尝试从现有库(即UIImagePickerControllerSourceTypePhotoLibrary)中进行选择时,将永远不会创建缩略图的.jpg。 I even tried re-saving with UISaveVideoAtPathToSavedPhotosAlbum(... ) and still no dice. 我甚至尝试用UISaveVideoAtPathToSavedPhotosAlbum(...)重新保存,但仍然没有骰子。 It seems like the thumbnail is first created when the video is captured. 看起来像是在捕获视频时首先创建缩略图。 That is your only opportunity to "grab" it. 这是你“抓住”它的唯一机会。 Afterwards it gets moved into a subdirectory of /var/mobile/Media/DCIM/ . 之后它会被移动到/ var / mobile / Media / DCIM /的子目录中。 While you can actually list the thumbnail image and see that is there, that file is unreadable and uncopyable as I believe that directory is protected. 虽然您实际上可以列出缩略图并看到它存在,但该文件是不可读和不可复制的,因为我认为目录受到保护。

If anyone has a workaround for this I would greatly appreciate it as in my case I need to grab the thumbnail of existing video after choosing it from the library. 如果有人有解决方法,我会非常感激,因为在我的情况下,我需要从库中选择现有视频的缩略图。

If your using UIImagePickerController to get the image, then a JPG would not be stored in the tmp directory. 如果您使用UIImagePickerController来获取图像,那么JPG将不会存储在tmp目录中。 I'm not sure of the answer for this, but ffmpeg, more specifically the libraries behind it, may be your option. 我不确定这个问题的答案,但是ffmpeg,更具体地说是它背后的库,可能是你的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM