iOS:在不播放视频的情况下获取视频时长和缩略图

Geo*_*old 20 ios ios6

我需要获得(本地)视频的持续时间,然后以UIImages的形式访问其各个帧.到目前为止,我一直在使用MPMoviePlayerController它.

首先我注册MPMovieDurationAvailableNotification活动,然后打电话prepareToPlay.收到事件后,我会记录视频的持续时间,然后通过请求帧requestThumbnailImagesAtTimes.

这是有效的,但即使我没有以任何方式将视频添加到视图中,视频似乎也开始播放(我可以听到在后台播放的音频).

有没有办法在没有实际播放视频的情况下获得视频的持续时间和帧数?

Shi*_*zam 39

要获得持续时间:

NSURL *sourceMovieURL = [NSURL fileURLWithPath:somePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
CMTime duration = sourceAsset.duration;
Run Code Online (Sandbox Code Playgroud)

获得一个framegrab:

AVAssetImageGenerator* generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:destinationAsset];

//Get the 1st frame 3 seconds in
int frameTimeStart = 3;
int frameLocation = 1;

//Snatch a frame
CGImageRef frameRef = [generator copyCGImageAtTime:CMTimeMake(frameTimeStart,frameLocation) actualTime:nil error:nil];
Run Code Online (Sandbox Code Playgroud)


Dha*_*ngh 8

你可以像这样迅速进入

func getMediaDuration(url: NSURL!) -> Float64{
    let asset : AVURLAsset = AVURLAsset(URL: url) as AVURLAsset
    let duration : CMTime = asset.duration
    return CMTimeGetSeconds(duration)
}
Run Code Online (Sandbox Code Playgroud)


Geo*_*old 6

setShouldAutoPlay:NO使用MPMoviePlayerController 调用诀窍:

 moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[moviePlayerController setShouldAutoplay:NO];   
[moviePlayerController prepareToPlay];
Run Code Online (Sandbox Code Playgroud)

编辑:我在没有解释的情况下被投票,但我会坚持这个答案.如果您需要使用,MPMoviePlayerController那么这将阻止媒体的自动播放,但仍允许您根据我的原始问题获得持续时间和缩略图.