ios - 将 AVAsset 读入帧并编译回视频

标签 ios objective-c avassetwriter avassetreader

各位!我项目中的想法是收集视频,将其分成帧,然后逐帧应用特定效果(不是主题),然后将所有内容编译回来。

使用以下代码,我能够将视频读入帧并将它们传递到数组中:

//initialize avassetreader
AVAsset *avAsset = [AVAsset assetWithURL:url];
NSError *error = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&error];

//get video track
NSArray *videoTracks = [avAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0];

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];

AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options];

[reader addOutput:asset_reader_output];
[reader startReading];

接下来,正如我上面所说,我逐帧阅读轨道

//while there is something to read
while ( [reader status]==AVAssetReaderStatusReading ) { 


    CMSampleBufferRef sampleBuffer = [asset_reader_output copyNextSampleBuffer];
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0);

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    //Generate image to edit
    unsigned char* pixel = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    CGContextRef context=CGBitmapContextCreate(pixel, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little|kCGImageAlphaPremultipliedFirst);

    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* myImage = [[UIImage alloc] initWithCGImage:image];
    [photos addObject:myImage];

}

我可以将所有内容放入数组中,但我需要将所有内容编译回视频轨道,使用原始轨道的音频进行编译,然后保存。但是,我在网上找不到任何有用的信息。请帮忙!

提前致谢!

最佳答案

一旦你有了图像数组,AVAssetWriter 就是你的 friend 。

关于ios - 将 AVAsset 读入帧并编译回视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31272799/

相关文章:

ios - 使用 iOS Stringsdict 文件的显式复数字符串

iphone - 如何在iOS中取消推送通知

avfoundation - AVAssetWriter 无法使用 AVVideoProfileLevelKey 对视频进行编码

video - iPhone RGBA 到 ARGB

iphone - 为什么 UITableViewController self.tableView 属性在 UIPopoverController 中呈现时为 Nil?

ios - 如何将字符串保存到数组并在标签中显示字符串?

ios - 当 View Controller 被关闭时,它应该清空内存吗?

iphone - iOS 5 中的 Twitter 用户 ID

ios - 如何使用 AVAssetWriter 保留视频的纵横比

ios - 如何更改正在使用 iOS11+ 拖放操作拖动的单元格的背景?