使用GPUImage时的AVCaptureSession图像尺寸问题

3

我正在尝试在iOS上使用GPUImage实现视频录制。

我有一个GPUImageVideoCamera,我正在尝试像这样设置录制:

-(void)toggleVideoRecording {
if (!self.recording) {
    self.recording = true;
    GPUImageMovieWriter * mw = [self createMovieWriter: self.filter1];

    [self.filter1 addTarget:mw];
    mw.shouldPassthroughAudio = YES;
    [mw startRecording];
}
else {
    self.recording = false;
    [self finishWritingMovie];
}

}

-(GPUImageMovieWriter *) createMovieWriter:(GPUImageFilter *) forFilter {
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/CurrentMovie.m4v"];
    unlink([pathToMovie UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    GPUImageMovieWriter * mw = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:forFilter->currentFilterSize];
    self.movieWriter = mw;
    self.movieURL = movieURL;

    return mw;
}

这个方案基本可以实现。但是,MoviewWriter需要输入媒体的CGSize,而我不知道如何从VideoCamera中获取它。首先,尺寸会因为相机使用sessionPreset:AVCaptureSessionPresetHigh而有所变化,这意味着它会根据设备的功能而异。其次,方向也会影响相关的CGSize。如果用户在横向模式下开始录制,我希望转置维度。

对于任何知道如何使用原始CoreVideo实现此操作的人,我可以从我的GPUImageVideoCamera中取出AVCaptureSession。(但是我已经阅读了AVCaptureSession的API,没有找到什么有希望的方法)。

引导我参考一个处理此问题的示例项目也将非常有帮助。

1个回答

7

确实可以从捕获会话中提取尺寸。以下是方法:

// self.videoCamera is a GPUImageVideoCamera

AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
NSDictionary* outputSettings = [output videoSettings];

long width  = [[outputSettings objectForKey:@"Width"]  longValue]; 
long height = [[outputSettings objectForKey:@"Height"] longValue];

if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
    long buf = width;
    width = height;
    height = buf;
}

GPUImageMovieWriter * mw = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(width, height)];

谢谢你的答案,帮了我很多。我在使用Xamarin,所以代码有点不同,因为绑定包装了AVCaptureVideoOutput.videoSettings。参考示例代码:AVCaptureVideoDataOutput output = camera.CaptureSession.Outputs.OfType<AVCaptureVideoDataOutput>().Last(); int h = output.UncompressedVideoSetting.Height ?? 0; int w = output.UncompressedVideoSetting.Width ?? 0; if (h == 0 || w == 0) throw new InvalidOperationException("Failed to get video output size from capture session"); - Johannes Rudolph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接