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?
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.
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.
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 ).
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.
I don't think MPMoviePlayerController
can help you.
Use an AVAssetReader
and count the number of CMSampleBuffer
s 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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With