视频/电影持续时间

时间:2014-01-19 23:50:20

标签: ios iphone objective-c

我在应用文档目录中保存了一段视频。我需要做的是找出视频的持续时间。即它是多少分钟和几秒钟。我无法弄清楚的是,一旦我有了电影网址,我需要做些什么来获得它的持续时间?我使用ALAssets吗?但我不确定如何。

这是我到目前为止的代码

NSString *nameSelected = [NSString stringWithFormat:@"Movie-1.mov"];
    NSLog(@"nameSelected: %@ ...", nameSelected);



    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
    NSString* documentsPath = [paths objectAtIndex:0];
    NSString* movieFile1 = [documentsPath stringByAppendingPathComponent:nameSelected];

    NSURL *firstMovieURL1 = [NSURL fileURLWithPath:movieFile1];

//No sure how I can call this
    //NSString *durationStr = [alAsset valueForProperty:ALAssetPropertyDuration];

2 个答案:

答案 0 :(得分:4)

这对我有用。

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:firstMovieURL1
                                                 options:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [NSNumber numberWithBool:YES],
                                                          AVURLAssetPreferPreciseDurationAndTimingKey,
                                                          nil]];

    NSTimeInterval durationInSeconds = 0.0;
    if (asset) 
        durationInSeconds = CMTimeGetSeconds(asset.duration) ;
    NSLog(@"videoDuration: %.2f", durationInSeconds);

确保导入

#import <AssetsLibrary/AssetsLibrary.h>

答案 1 :(得分:0)

利用AVPlayerItemAVFoundation&amp; CoreMedia框架。

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:yourVideoUrl];
CMTime videoDuration = playerItem.duration;
float seconds = CMTimeGetSeconds(videoDuration);
NSLog(@"videoDuration: %.2f", seconds);