将过滤器应用于AVVideoComposition时崩溃

时间:2017-10-27 09:58:48

标签: ios video filter

我想通过函数将过滤器应用于AVVideoComposition:

init(asset: AVAsset, applyingCIFiltersWithHandler: (AVAsynchronousCIImageFilteringRequest) -> Void)

其中assetAVComposition。当AVPlayerItem使用videoComposition播放此合成时,应用程序崩溃并显示错误:

reason: '*** -[AVCoreImageFilterCustomVideoCompositor startVideoCompositionRequest:] Expecting video composition to contain only AVCoreImageFilterVideoCompositionInstruction'

我想知道如何修复崩溃。

PS:我有两个videoTracks,每个timeRange都有它的指令

3 个答案:

答案 0 :(得分:1)

我猜你试图将AVVideoCompositionLayerInstruction添加到AVVideoComposition。

首先尝试简单方法,看看是否需要进行任何更改:

AVURLAsset *asset = [AVURLAsset assetWithURL:videoURL];
CIFilter *filter = [CIFilter filterWithName:@"CIHueAdjust"]; // the filter you want to add: https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoCompositionWithAsset:asset applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) {
    // set filter input image
[filter setDefaults];
    [filter setValue:sourceImage forKey:kCIInputImageKey];

    // hue
    NSNumber *angle = [NSNumber numberWithFloat:0.8];
    [filter setValue:angle forKey:kCIInputAngleKey];

    CIImage *outputImage = filter.outputImage;
    [request finishWithImage:outputImage context:nil];
}];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset1920x1080];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.outputURL = outputURL;
exportSession.videoComposition = videoComposition;

// export the session async
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch (exportSession.status) {
        case AVAssetExportSessionStatusCompleted:
            NSLog(@"Yeah!");
            break;

        default:
            NSLog(@"Nooo!");
            break;
    }
}];

答案 1 :(得分:1)

您不能同时使用CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 RECORD_SECONDS = 2 p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) stream.stop_stream() stream.close() p.terminate() AVVideoCompositionLayerInstruction

Intead,您需要直接在过滤器中应用转换。

这可以通过将其应用于源图像来完成。

applyingCIFiltersWithHandler

答案 2 :(得分:0)

AVMutableVideoComposition:videoCompositionWithAsset:applyCIFiltersWithHandler仅支持iOS 9+,如果您只需要使用CIFilter导出视频,那就没问题。

配置更多videoComposition指令将为现金,例如将类型的roateLayerInstruction添加到AVAssetTrack。

我在这种情况下有同样的问题,自定义AVVideoCompositing可能是更好的解决方案。关于如何自定义videoCompositor demo1 demo2的一些好演示。

本周我正在解决这个问题。