1

カメラで撮影した映画の未加工のフッテージにアクセスして、未加工のフッテージを編集または変換するにはどうすればよいですか (例: 白黒にする)。

AVAsset を使用して mov をロードし、別の AVAsset を使用してコンポジションを作成し、それを新しいムービーにエクスポートできることは知っていますが、ムービーを編集できるようにアクセスするにはどうすればよいですか。

4

2 に答える 2

5

入力アセットからビデオ フレームを読み取り、フレームごとに CGContextRef を作成して描画を行い、フレームを新しいビデオ ファイルに書き出す必要があります。基本的な手順は次のとおりです。フィラー コードとエラー処理はすべて省略しているため、主な手順は読みやすくなっています。

// AVURLAsset to read input movie (i.e. mov recorded to local storage)
NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:inputURL options:inputOptions];

// Load the input asset tracks information
[inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{

    // Check status of "tracks", make sure they were loaded    
    AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
    if (!tracksStatus == AVKeyValueStatusLoaded)
        // failed to load
        return;

    // Fetch length of input video; might be handy
    NSTimeInterval videoDuration = CMTimeGetSeconds([inputAsset duration]);
    // Fetch dimensions of input video
    CGSize videoSize = [inputAsset naturalSize];


    /* Prepare output asset writer */
    self.assetWriter = [[[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:&error] autorelease];
    NSParameterAssert(assetWriter);
    assetWriter.shouldOptimizeForNetworkUse = NO;


    // Video output
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                       AVVideoCodecH264, AVVideoCodecKey,
                       [NSNumber numberWithInt:videoSize.width], AVVideoWidthKey,
                       [NSNumber numberWithInt:videoSize.height], AVVideoHeightKey,
                       nil];
    self.assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                            outputSettings:videoSettings];
    NSParameterAssert(assetWriterVideoInput);
    NSParameterAssert([assetWriter canAddInput:assetWriterVideoInput]);
    [assetWriter addInput:assetWriterVideoInput];


    // Start writing
    CMTime presentationTime = kCMTimeZero;

    [assetWriter startWriting];
    [assetWriter startSessionAtSourceTime:presentationTime];


    /* Read video samples from input asset video track */
    self.reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];

    NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
    [outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]  forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
    self.readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                        outputSettings:outputSettings];


    // Assign the tracks to the reader and start to read
    [reader addOutput:readerVideoTrackOutput];
    if ([reader startReading] == NO) {
        // Handle error
    }


    dispatch_queue_t dispatch_queue = dispatch_get_main_queue();

    [assetWriterVideoInput requestMediaDataWhenReadyOnQueue:dispatch_queue usingBlock:^{
        CMTime presentationTime = kCMTimeZero;

        while ([assetWriterVideoInput isReadyForMoreMediaData]) {
            CMSampleBufferRef sample = [readerVideoTrackOutput copyNextSampleBuffer];
            if (sample) {
                presentationTime = CMSampleBufferGetPresentationTimeStamp(sample);

                /* Composite over video frame */

                CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sample); 

                // Lock the image buffer
                CVPixelBufferLockBaseAddress(imageBuffer,0); 

                // Get information about the image
                uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
                size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
                size_t width = CVPixelBufferGetWidth(imageBuffer); 
                size_t height = CVPixelBufferGetHeight(imageBuffer); 

                // Create a CGImageRef from the CVImageBufferRef
                CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
                CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

                /*** Draw into context ref to draw over video frame ***/

                // We unlock the  image buffer
                CVPixelBufferUnlockBaseAddress(imageBuffer,0);

                // We release some components
                CGContextRelease(newContext); 
                CGColorSpaceRelease(colorSpace);

                /* End composite */

                [assetWriterVideoInput appendSampleBuffer:sample];
                CFRelease(sample);

            }
            else {
                [assetWriterVideoInput markAsFinished];

                /* Close output */

                [assetWriter endSessionAtSourceTime:presentationTime];
                if (![assetWriter finishWriting]) {
                    NSLog(@"[assetWriter finishWriting] failed, status=%@ error=%@", assetWriter.status, assetWriter.error);
                }

            }

        }
    }];

}];
于 2010-11-15T01:41:57.120 に答える
0

私はプロセス全体を知りませんが、いくつか知っています:

個々のフレームを処理するには、AV Foundation Framework とおそらく Core Video Framework を使用する必要がある場合があります。おそらく AVWriter を使用するでしょう:

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:path]
                                            fileType:AVFileTypeQuickTimeMovie
                                               error:&error];

AVFoundation または CV を使用してピクセル バッファーを維持し、次のように記述できます (この例は CV 用です)。

[pixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

フレームを取得するにAVAssetStillImageGeneratorは、実際には十分ではありません。

または、 AVVideoMutableComposition、AVMutableComposition、または AVAssetExportSession で使用できるフィルターまたは命令がある場合があります。

8 月に質問してから進展があれば、興味があるので投稿してください。

于 2010-10-06T21:41:18.120 に答える