Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Determine Number of Frames in Video

If I have a MPMoviePlayerController in Swift:

MPMoviePlayerController mp = MPMoviePlayerController(contentURL: url)

Is there a way I can get the number of frames within the video located at url? If not, is there some other way to determine the frame count?

like image 540
Oliver Spryn Avatar asked Apr 08 '15 05:04

Oliver Spryn


People also ask

How to count the number of frames in a video file?

There are two methods to determine the total number of frames in a video file using OpenCV and Python: Method #1: The fast, efficient way using the built-in properties OpenCV provides us to access the video file meta information and return the total number of frames.

What is the difference between resolution and frame rate?

Video resolution is the dimension: width x height of a video file. The video resolution information allows you to confirm the quality of the video that you are watching. Frame rate tells us the number of frames per second the video is.

How do I get the total number of frames in OpenCV?

In OpenCV 3 the name of the frame count property is cv2.CAP_PROP_FRAME_COUNT while in OpenCV 2.4 the property is named cv2.cv.CV_CAP_PROP_FRAME_COUNT . Ideally, passing the respective property name into the .get method of the video pointer will allow us to obtain the total number of frames in the video ( Lines 25-30 ).

How do I find out what frame I am on?

Sometimes its handy to know exactly which frame you are on and to be able to step through the video frame by frame. Reply ashishsays: February 6, 2016 at 7:30 am The only frame information that I can find is Tools > Media informationand switching to the Statisticstag. It displays the frames decoded, displayed and lost.


2 Answers

I don't think MPMoviePlayerController can help you.

Use an AVAssetReader and count the number of CMSampleBuffers it returns to you. You can configure it to not even decode the frames, effectively parsing the file, so it should be fast and memory efficient.

Something like

    var asset = AVURLAsset(URL: url, options: nil)
    var reader = AVAssetReader(asset: asset, error: nil)
    var videoTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack

    var readerOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: nil) // NB: nil, should give you raw frames
    reader.addOutput(readerOutput)
    reader.startReading()

    var nFrames = 0

    while true {
        var sampleBuffer = readerOutput.copyNextSampleBuffer()
        if sampleBuffer == nil {
            break
        }

        nFrames++
    }

    println("Num frames: \(nFrames)")

Sorry if that's not idiomatic, I don't know swift.

like image 124
Rhythmic Fistman Avatar answered Sep 20 '22 11:09

Rhythmic Fistman


Swift 5

 func getNumberOfFrames(url: URL) -> Int {
        let asset = AVURLAsset(url: url, options: nil)
        do {
            let reader = try AVAssetReader(asset: asset)
        //AVAssetReader(asset: asset, error: nil)
            let videoTrack = asset.tracks(withMediaType: AVMediaType.video)[0]

            let readerOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: nil) // NB: nil, should give you raw frames
            reader.add(readerOutput)
        reader.startReading()

        var nFrames = 0

        while true {
            let sampleBuffer = readerOutput.copyNextSampleBuffer()
            if sampleBuffer == nil {
                break
            }

            nFrames = nFrames+1
        }

        print("Num frames: \(nFrames)")
            return nFrames
        }catch {
            print("Error: \(error)")
        }
        return 0
    }
like image 30
Amal T S Avatar answered Sep 21 '22 11:09

Amal T S