Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将AVAsset读入帧并编译回视频_Ios_Objective C_Avassetwriter_Avassetreader - Fatal编程技术网

Ios 将AVAsset读入帧并编译回视频

Ios 将AVAsset读入帧并编译回视频,ios,objective-c,avassetwriter,avassetreader,Ios,Objective C,Avassetwriter,Avassetreader,各位!!我项目中的想法是收集一段视频,将其分割成帧,然后逐帧应用特定效果(不是主题),然后将所有内容编译回来 使用以下代码,我能够将视频读入帧并将其传递到阵列中: //initialize avassetreader AVAsset *avAsset = [AVAsset assetWithURL:url]; NSError *error = nil; AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset err

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

使用以下代码,我能够将视频读入帧并将其传递到阵列中:

//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就是你的朋友。

只是想指出,在你的逐帧代码中有一些内存泄漏。具体来说,您需要释放CGImageRef、颜色空间、上下文和示例缓冲区。因此,如果您仍在使用此功能(或者如果其他人来使用此功能),请确保检查内存管理。