2014-10-28 32 views
2

我正試圖穿過AVAsset中的每個幀,並將每個幀視爲圖像。我無法從我的搜索中找到任何內容。處理AVAsset中的所有幀

我試圖完成應該是這樣的僞代碼的任務

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 

在僞代碼,我不知道怎麼寫的唯一的部分是,雖然每幀的持續和捕捉它在圖像中。

任何人都可以幫忙嗎?

回答

4

一個答案是使用AVAssetImageGenerator

1)將電影文件加載到一個AVAsset對象中。
2)創建一個AVAssetImageGenerator對象。
3)通過幀的估計時間,你想從電影中獲取圖像。

將2個屬性requestedTimeToleranceBeforerequestedTimeToleranceAfterAVAssetImageGenerator對象設置爲kCMTimeZero將增加獲取單個幀的能力,但會增加處理時間。

但是這種方法很慢,我還沒找到更快的方法。

//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);  
}