iOS: get video duration and thumbnails without playing video

后端 未结 3 2092
北荒
北荒 2021-01-31 04:30

I need to get a (local) video\'s duration, and then get access to its individual frames as UIImages. So far I\'ve been using MPMoviePlayerController fo

相关标签:
3条回答
  • 2021-01-31 05:05

    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];
    
    0 讨论(0)
  • 2021-01-31 05:12

    You can get in swift like this

    func getMediaDuration(url: NSURL!) -> Float64{
        let asset : AVURLAsset = AVURLAsset(URL: url) as AVURLAsset
        let duration : CMTime = asset.duration
        return CMTimeGetSeconds(duration)
    }
    
    0 讨论(0)
  • 2021-01-31 05:12

    Calling setShouldAutoPlay:NO does the trick with MPMoviePlayerController:

     moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [moviePlayerController setShouldAutoplay:NO];   
    [moviePlayerController prepareToPlay];
    

    Edit: I'm getting downvoted without explanation, but I'll stand by this answer. If you need to use MPMoviePlayerController then this will prevent autoplay of the media, yet still allow you to get duration and thumbnails as per my original question.

    0 讨论(0)
提交回复
热议问题