цель c - AvAssetReader и Writer для наложения видео

Я пытаюсь наложить на записанное видео с помощью AvAssetReader и AvAssetWriter некоторые изображения. После этого руководство, я могу скопировать видео (и аудио) в новый файл. Теперь моя цель - наложить некоторые из начальных видеокадров на изображения с помощью этого кода:

while ([assetWriterVideoInput isReadyForMoreMediaData] && !completedOrFailed)
            {
                // Get the next video sample buffer, and append it to the output file.
                CMSampleBufferRef sampleBuffer = [assetReaderVideoOutput copyNextSampleBuffer];

                CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
                CVPixelBufferLockBaseAddress(pixelBuffer, 0);
                EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
                CIContext *ciContext = [CIContext contextWithEAGLContext:eaglContext options:@{kCIContextWorkingColorSpace : [NSNull null]}];
                UIFont *font = [UIFont fontWithName:@"Helvetica" size:40];
                NSDictionary *attributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:[UIColor lightTextColor]};
                UIImage *img = [self imageFromText:@"test" :attributes];

                CIImage *filteredImage = [[CIImage alloc] initWithCGImage:img.CGImage];

                [ciContext render:filteredImage toCVPixelBuffer:pixelBuffer bounds:[filteredImage extent] colorSpace:CGColorSpaceCreateDeviceRGB()];


                CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

                if (sampleBuffer != NULL)
                {
                    BOOL success = [assetWriterVideoInput appendSampleBuffer:sampleBuffer];
                    CFRelease(sampleBuffer);
                    sampleBuffer = NULL;
                    completedOrFailed = !success;
                }
                else
                {
                    completedOrFailed = YES;
                }
            }

И чтобы создать изображение из текста:

-(UIImage *)imageFromText:(NSString *)text :(NSDictionary *)attributes{
CGSize size = [text sizeWithAttributes:attributes];
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[text drawAtPoint:CGPointMake(0.0, 0.0) withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Видео и аудио скопированы, но на моем видео нет текста.

Вопрос 1. Почему этот код не работает?

Более того, я хочу иметь возможность проверить тайм-код текущего кадра чтения. Например, я хотел бы вставить в видео текст с текущим тайм-кодом.

Я пробую этот код, следуя этому руководство:

        AVAsset *localAsset = [AVAsset assetWithURL:mURL];
    NSError *localError;
    AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:localAsset error:&localError];
    BOOL success = (assetReader != nil);

    // Create asset reader output for the first timecode track of the asset
    if (success) {
        AVAssetTrack *timecodeTrack = nil;

        // Grab first timecode track, if the asset has them
        NSArray *timecodeTracks = [localAsset tracksWithMediaType:AVMediaTypeTimecode];
        if ([timecodeTracks count] > 0)
            timecodeTrack = [timecodeTracks objectAtIndex:0];

        if (timecodeTrack) {
            AVAssetReaderTrackOutput *timecodeOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:timecodeTrack outputSettings:nil];
            [assetReader addOutput:timecodeOutput];
        } else {
            NSLog(@"%@ has no timecode tracks", localAsset);
        }
    }

Но я получаю журнал:

[...] не имеет дорожек с временным кодом

Вопрос 2. Почему в моем видео нет кода AVMediaTypeTimecode? Объявление, так как я могу получить текущий тайм-код кадра?

Спасибо за вашу помощь


person Oier Etchelecou    schedule 08.02.2016    source источник


Ответы (1)


Я нашел решения:

Для наложения видеокадров нужно поправить настройки декомпрессии:

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* decompressionVideoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
// If there is a video track to read, set the decompression settings for YUV and create the asset reader output.
assetReaderVideoOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:assetVideoTrack outputSettings:decompressionVideoSettings];

Чтобы получить метку времени кадра, вы должны прочитать видеоинформацию, а затем использовать счетчик для увеличения текущей метки времени:

durationSeconds = CMTimeGetSeconds(asset.duration);
timePerFrame = 1.0 / (Float64)assetVideoTrack.nominalFrameRate;
totalFrames = durationSeconds * assetVideoTrack.nominalFrameRate;

Затем в этом цикле

while ([assetWriterVideoInput isReadyForMoreMediaData] &&! completedOrFailed)

Вы можете найти метку времени:

CMSampleBufferRef sampleBuffer = [assetReaderVideoOutput copyNextSampleBuffer];
if (sampleBuffer != NULL){
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if (pixelBuffer) {
Float64 secondsIn = ((float)counter/totalFrames)*durationSeconds;
CMTime imageTimeEstimate = CMTimeMakeWithSeconds(secondsIn, 600);
mergeTime = CMTimeGetSeconds(imageTimeEstimate);
                                    counter++;
}
}

Надеюсь, это поможет!

person Oier Etchelecou    schedule 08.02.2016