iOS:在不播放视频的情况下获取视频时长和缩略图 [英] iOS: get video duration and thumbnails without playing video
问题描述
我需要获取(本地)视频的持续时间,然后以 UIImage
s的形式访问其各个帧。到目前为止,我一直在使用 MPMoviePlayerController
。
I need to get a (local) video's duration, and then get access to its individual frames as UIImage
s. So far I've been using MPMoviePlayerController
for this.
首先我注册 MPMovieDurationAvailableNotification
事件,然后调用 prepareToPlay
。收到事件后,我会记录视频的持续时间,然后通过 requestThumbnailImagesAtTimes
请求帧。
First I register for MPMovieDurationAvailableNotification
events, and then call prepareToPlay
. When the event is received I note the video's duration, and then I request frames via requestThumbnailImagesAtTimes
.
这是有效的,但即使我没有以任何方式将视频添加到视图中,视频似乎也开始播放(我可以听到后台播放的音频) 。
This works, however the video seems to start playing even if I have not added the it to the view in any way (I can hear the audio playing in the background).
有没有办法在没有实际播放视频的情况下获得视频的持续时间和帧数?
Is there any way to get a video's duration and frames without actually playing the video?
推荐答案
获取持续时间:
NSURL *sourceMovieURL = [NSURL fileURLWithPath:somePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
CMTime duration = sourceAsset.duration;
获取framegrab:
To get a 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];
这篇关于iOS:在不播放视频的情况下获取视频时长和缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!