Caffeine Coma Caffeine Coma - 1 year ago 77
iOS Question

iOS: get video duration and thumbnails without playing video

I need to get a (local) video's duration, and then get access to its individual frames as

s. So far I've been using
for this.

First I register for
events, and then call
. When the event is received I note the video's duration, and then I request frames via

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?

Answer Source

To get the duration:

NSURL *sourceMovieURL = [NSURL fileURLWithPath:somePath];
AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
CMTime duration = sourceAsset.duration;

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download