Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:如何获取从库中选择的视频的持续时间?_Iphone_Ios_Uiimagepickercontroller - Fatal编程技术网

iPhone:如何获取从库中选择的视频的持续时间?

iPhone:如何获取从库中选择的视频的持续时间?,iphone,ios,uiimagepickercontroller,Iphone,Ios,Uiimagepickercontroller,我正在使用UIImagePickerController从库中选择视频文件。用户可以上传视频 当用户想要捕获视频并上传时,我也在使用videoMaximumDuration属性 我想知道如何获得所选视频文件的持续时间?所以我可以限制用户上传持续时间超过20秒的视频 我可以通过以下代码获得有关所选视频的一些基本信息: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithIn

我正在使用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);
}
但并没有关于所选视频的持续时间


谢谢…

得到了解决方案:我使用
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);
}
#导入
#进口
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
selectedVideoUrl=[info-objectForKey:UIImagePickerController-DiaUrl];
AVPlayerItem*playerItem=[AVPlayerItem playerItemWithURL:selectedVideoUrl];
CMTime duration=playerItem.duration;
浮动秒数=CMTimeGetSeconds(持续时间);
NSLog(@“持续时间:%.2f”,秒);
}

是的,您可以使用MPMediaPlayerController定义的属性“duration”。请尝试一下并检查输出。你可以在这里查阅资料


尝试使用MPMediaPlayerController播放视频,然后使用属性“duration”。U将能够清楚地获得视频持续时间的详细信息。

如果使用
AVFoundation
和AssetLibrary框架,则可以枚举所有资产,仅对视频应用筛选器,并使用方法
-(id)valueForProperty:(NSString*)property
获取每个视频的持续时间。为属性传入
ALAssetPropertyDuration
。下面的代码在控制台上打印出以下内容

视频剪辑编号0为66.80833334秒

1号视频剪辑为190.06秒

第二个视频剪辑是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];
}

使用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);
}
#导入
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
NSURL*videoURL=[info objectForKey:@“UIImagePickerControllerMediaURL”];
AVURLAsset*asset=[[AVURLAsset alloc]initWithURL:videoURL选项:nil];
NSTimeInterval durationInSeconds=0.0;
如果(资产)
durationInSeconds=CMTimeGetSeconds(资产持续时间);
}

Swift 4

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

// 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
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)

您还需要链接CoreMedia framework for CMTimeGetSeconds才能正常工作。我们可以裁剪特定持续时间的视频吗?@Maulik-我已经看到了这一点,但没有帮助。将nan恢复为mp4文件…我可以通过AVURLAsset获取视频帧,所以我知道文件在那里,可以用后一种方式处理…滚动到本页底部以获得有效的答案。谢谢你的回答。