【问题标题】:Change the bitrate of recordings made with AVFoundation on Mac在 Mac 上更改使用 AVFoundation 录制的比特率
【发布时间】:2012-08-23 01:22:45
【问题描述】:

我正在使用 AVFoundation 在 OSX 10.8 上进行一些屏幕录制。我正在使用 sessionPreset "AVCaptureSessionPresetPhoto" 来记录整个屏幕。我想更改的一件事是创建的电影文件的质量。

似乎需要 AVCaptureSessionPresetPhoto 才能实际捕获全屏而不进行剪辑。

根据此处的文档:http://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVCaptureSession_Class/Reference/Reference.html

"You use this property to customize the quality level or bitrate of the output. For possible values of sessionPreset, see “Video Input Presets.” The default value is AVCaptureSessionPresetHigh."

但是,对于视频输入预设,唯一的选项是这些常量: http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVCaptureSession_Class/Reference/Reference.html#//apple_ref/occ/cl/AVCaptureSession

NSString *const AVCaptureSessionPresetPhoto;

NSString *const AVCaptureSessionPresetHigh;

NSString *const AVCaptureSessionPresetMedium;

NSString *const AVCaptureSessionPresetLow;

NSString *const AVCaptureSessionPreset320x240;

NSString *const AVCaptureSessionPreset352x288;

NSString *const AVCaptureSessionPreset640x480;

NSString *const AVCaptureSessionPreset960x540;

NSString *const AVCaptureSessionPreset1280x720;

AVCaptureSessionPresetPhoto 可以在不裁剪的情况下捕获全屏,但最终的质量有些令人失望。由于默认使用的比特率较低,存在可见的伪影。

如何提高最终录制的比特率?

以下是我当前代码的示例。

    mSession = [[AVCaptureSession alloc] init];

    mSession.sessionPreset = AVCaptureSessionPresetPhoto;

    CGDirectDisplayID displayId = kCGDirectMainDisplay;

    AVCaptureScreenInput *input = [[AVCaptureScreenInput alloc] initWithDisplayID:displayId];
    if (!input) {
        mSession = nil;
        return;
    }        

    if ([mSession canAddInput:input]){
        [mSession addInput:input];
    }

    mMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
    if ([mSession canAddOutput:mMovieFileOutput])
        [mSession addOutput:mMovieFileOutput];

    [mSession startRunning];

    if ([[NSFileManager defaultManager] fileExistsAtPath:[destPath path]])
    {
        NSError *err;
        if (![[NSFileManager defaultManager] removeItemAtPath:[destPath path] error:&err])
        {
            NSLog(@"Error deleting existing movie %@",[err localizedDescription]);
        }
    }

    [mMovieFileOutput startRecordingToOutputFileURL:destPath recordingDelegate:self];

    mTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(finishRecord:) userInfo:nil repeats:NO];

【问题讨论】:

    标签: objective-c avfoundation


    【解决方案1】:

    如果您需要对 MP4/MOV H.264 比特流的比特率进行精细控制,那么您将需要使用 AVFoundation 的 AVAssetWriter。对于视频输入,您可以设置类似post 中的属性。查看 AVVideoAverageBitRateKey。还要注意 AVVideoMaxKeyFrameIntervalKey 键。如果您打算对视频进行后期制作,那么我建议您使用 1 作为关键帧间隔。有关使用 AVAssetWriter 的示例,请查看 Apple 的 RosyWriter 或 AVCamDemo 示例应用程序。如果您需要更多解释,请告诉我。

    【讨论】:

    • 非常感谢史蒂夫的信息。肯定会有所帮助。根据我看到的各种代码示例,我很难弄清楚如何使用 AVCaptureScreenInput 作为输入源。我所看到的大部分内容涉及从相机源捕获视频,或将 NSArray 图像转换为电影。
    • 我不确定从屏幕上捕获。文档似乎表明您可以将其用作捕获输入来代替视频输入。
    【解决方案2】:

    这实际上可以在不使用 AVAssetWriter 的情况下完成。这是一些示例代码。

    for connection in movieFileOutput.connections {
         let settings = movieFileOutput.outputSettings(for: connection)
         var newSettings = settings
         var sub = newSettings[AVVideoCompressionPropertiesKey] as? [String: Any]
         sub![AVVideoAverageBitRateKey] = NSNumber(integerLiteral: 4000000)
         newSettings[AVVideoCompressionPropertiesKey] = sub!
         movieFileOutput.setOutputSettings(newSettings, for: connection)
      }
    

    当然,您仍然需要确定您的 AVCaptureConnection 是您的视频连接。 (与我的示例代码不同,您还应该安全地打开选项)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2016-12-05
      相关资源
      最近更新 更多