iPhone:如何获得从图书馆select的video的持续时间?

我正在使用UIImagePickerController从库中selectvideo文件。 用户可以上传video。

此外,我正在使用videoMaximumDuration属性,而用户想要捕捉video并上传它。

我想知道如何获得选定的video文件的持续时间? 这样我就可以限制用户上传持续时间超过20秒的video。

我能够通过此代码获得有关所选video的一些基本信息:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; NSError *error; NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:selectedVideoUrl.path error:&error]; NSNumber * size = [properties objectForKey: NSFileSize]; NSLog(@"Vide info :- %@",properties); } 

但所选video的时长没有任何关系。

谢谢…

得到的解决scheme:我使用AVPlayerItem类和AVFoundationCoreMedia框架。

 #import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVAsset.h> - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl]; CMTime duration = playerItem.duration; float seconds = CMTimeGetSeconds(duration); NSLog(@"duration: %.2f", seconds); } 

使用AVPlayerItem这个问题的其他答案不适合我,但是这使用AVURLAsset:

 #import <AVFoundation/AVFoundation.h> -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; NSTimeInterval durationInSeconds = 0.0; if (asset) durationInSeconds = CMTimeGetSeconds(asset.duration); } 

迅速

无论使用didFinishPickingMediaWithInfo还是didFinishRecordingToOutputFileAtURL您都可以:

 // needed only for didFinishPickingMediaWithInfo let outputFileURL = info[UIImagePickerControllerMediaURL] as! NSURL // get the asset let asset = AVURLAsset(URL: outputFileURL) // get the time in seconds let durationInSeconds = asset.duration.seconds 

如果您使用AVFoundationAVFoundation框架,您可以枚举所有资产,为video应用filter,并使用方法- (id)valueForProperty:(NSString *)property获取每个video的持续时间。 传递属性的ALAssetPropertyDuration 。 下面的代码在控制台上打印出以下内容。

video剪辑编号0是66.80833333333334秒

video剪辑编号1是190.06秒

video剪辑编号2是13.74秒

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. if (!assetItems) { assetItems = [[NSMutableArray alloc] init]; } else { [assetItems removeAllObjects]; } if (!assetLibrary) { assetLibrary = [[ALAssetsLibrary alloc] init]; } ALAssetsLibraryGroupsEnumerationResultsBlock listBlock = ^(ALAssetsGroup *group, BOOL *stop) { if (group) { [group setAssetsFilter:[ALAssetsFilter allVideos]]; [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result) { [assetItems addObject:result]; NSString *duration = [result valueForProperty:ALAssetPropertyDuration]; NSLog(@"video clip number %d is %@ seconds\n",index, duration); } }]; } }; ALAssetsLibraryAccessFailureBlock failBlock = ^(NSError *error) { // error handler block NSString *errorTitle = [error localizedDescription]; NSString *errorMessage = [error localizedRecoverySuggestion]; NSLog(@"%@...\n %@\n",errorTitle,errorMessage); }; [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:listBlock failureBlock:failBlock]; } 

Swift 3.0和Swift 4

 let outputFileURL = info[UIImagePickerControllerMediaURL] as! URL // get the asset let asset = AVURLAsset.init(url: outputFileURL) // AVURLAsset.init(url: outputFileURL as URL) in swift 3 // get the time in seconds let durationInSeconds = asset.duration.seconds print("==== Duration is ",durationInSeconds) 

是的,您可以使用由MPMediaPlayerController定义的属性“持续时间”。 请尝试一下并检查输出。 你可以参考这里持续时间的属性

尝试使用MPMediaPlayerController播放video,然后使用属性“duration”。 U将能够清楚地了解video的持续时间。