处理AVAsset中的所有帧 [英] Processing all frames in an AVAsset
问题描述
我试图遍历AVAsset
中的每一帧,并像对待图像一样处理每一帧.我无法从搜索中找到任何内容.
I am trying to go through each frame in an AVAsset
and process each frame as if it were an image. I have not been able to find anything from my searches.
我要完成的任务在伪代码中看起来像这样
The task I am trying to accomplish would look like this in pseudo-code
for each frame in asset
take the frame as an image and convert to a cvMat
Process and store data of center points
Store center points in array
该伪代码中我唯一不知道如何写的是遍历每一帧并将其捕获到图像中.
The only part in that pseudo-code I do not know how to write is the going though each frame and capturing it in an image.
任何人都可以帮忙吗?
推荐答案
一个答案是使用 1)将影片文件加载到AVAsset
对象中.
2)创建一个AVAssetImageGenerator
对象.
3)输入您想从电影中获取图像的帧的估计时间.
1) Load the movie file into an AVAsset
object.
2) Create an AVAssetImageGenerator
object.
3) Pass in an estimated time of the frame where you want to get an image back from the movie.
将AVAssetImageGenerator
对象上的2个属性requestedTimeToleranceBefore
和requestedTimeToleranceAfter
设置为kCMTimeZero
将增加获取单个帧的能力,但会增加处理时间.
Setting the 2 properties requestedTimeToleranceBefore
and requestedTimeToleranceAfter
on the AVAssetImageGenerator
object to kCMTimeZero
will increase the ability to get individual frames, but increases the processing time.
但是这种方法很慢,我还没有找到更快的方法.
However this method is slow and I have not found a faster way.
//Load the Movie from a URL
self.movieAsset = [AVAsset assetWithURL:self.movieURL];
NSArray *movieTracks = [self.movieAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *movieTrack = [movieTracks objectAtIndex:0];
//Make the image Generator
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:self.movieAsset];
//Create a variables for the time estimation
Float64 durationSeconds = CMTimeGetSeconds(self.movieAsset.duration);
Float64 timePerFrame = 1.0 / (Float64)movieTrack.nominalFrameRate;
Float64 totalFrames = durationSeconds * movieTrack.nominalFrameRate;
//Step through the frames
for (int counter = 0; counter <= totalFrames; counter++){
CMTime actualTime;
Float64 secondsIn = ((float)counter/totalFrames)*durationSeconds;
CMTime imageTimeEstimate = CMTimeMakeWithSeconds(secondsIn, 600);
NSError *error;
CGImageRef image = [imageGenerator copyCGImageAtTime:imageTimeEstimate actualTime:&actualTime error:&error];
...Do some processing on the image
CGImageRelease(image);
}
这篇关于处理AVAsset中的所有帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!