我正在使用UIImagePickerController
从库中选择视频文件。用户可以上传视频。
我也在使用videoMaximumDuration
属性,而用户想要捕获视频并上传它。
我想知道如何获取所选视频文件的持续时间?这样我就可以限制用户上传持续时间超过20秒的视频。
我可以通过以下代码获得有关所选视频的一些基本信息:
- (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);
}
但是所选视频的持续时间并不存在。
...谢谢
答案 0 :(得分:30)
获得解决方案:我使用AVPlayerItem
类和AVFoundation
以及CoreMedia
框架。
#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);
}
答案 1 :(得分:29)
使用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);
}
答案 2 :(得分:8)
Swift 4
无论使用didFinishPickingMediaWithInfo
还是didFinishRecordingToOutputFileAtURL
,您都可以:
// needed only for didFinishPickingMediaWithInfo
let outputFileURL = info[UIImagePickerControllerMediaURL] as! URL
// get the asset
let asset = AVURLAsset(url: outputFileURL)
// get the time in seconds
let durationInSeconds = asset.duration.seconds
答案 3 :(得分:2)
如果您使用AVFoundation
和AssetLibrary框架,则可以枚举所有资源,仅为视频应用过滤器,并使用方法- (id)valueForProperty:(NSString *)property
获取每个视频的持续时间。通过ALAssetPropertyDuration
获取财产。下面的代码在控制台上打印出以下内容。
视频剪辑编号0为66.80833333333334秒
视频剪辑编号1为190.06秒
视频剪辑编号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];
}
答案 4 :(得分:2)
Swift 3.0和Swift 4
const wrapper = getWrapper();
答案 5 :(得分:1)
是的,您可以使用MPMediaPlayerController定义的属性“duration”。请试一试并检查输出。你可以参考这里duration property
尝试使用MPMediaPlayerController播放视频,然后使用属性“duration”。你将能够清楚地了解视频的持续时间。