1

这是一个让我很困惑的相当具体的问题。我正在编写一个视频录制软件,它将视频数据从网络摄像头捕获到 MP4。我的软件将替换已经存在的脚本,该脚本触发 QuickTime Player 执行相同操作但输出到 MOV。

我正在使用 AVFoundation 并进行了捕获和保存,但是经过反复调整和测试,我发现已经存在的脚本始终可以创建具有比我的软件更高的视频质量和更小的文件大小的 MOV 文件。

这是两个示例的链接,一个由现场脚本创建的 MOV 和一个由我的软件创建的 MP4:https ://www.dropbox.com/sh/1qnn5afmrquwfcr/AADQvDMWkbYJwVNlio9_vbeNa?dl=0

MOV 是由一位同事创建的,它是我试图与我的软件匹配的质量和文件大小。MP4 正在我的办公室录制,显然是不同的照明情况,但使用与现场使用的相机相同的相机。

比较这两个视频,我可以看到它们具有相同的尺寸、持续时间和视频编解码器,但文件大小和质量不同。

这是我设置 AVAssetWriter 和 AVAssetWriter 输入的代码:

NSDictionary *settings = nil;

settings = [NSDictionary dictionaryWithObjectsAndKeys:
            // Specify H264 (MPEG4) as the codec
            AVVideoCodecH264, AVVideoCodecKey,

            // Specify the video width and height
            [NSNumber numberWithInt:self.recordSize.width], AVVideoWidthKey,
            [NSNumber numberWithInt:self.recordSize.height], AVVideoHeightKey,

            // Specify the video compression
            [NSDictionary dictionaryWithObjectsAndKeys:
             [NSNumber numberWithInteger:2500000], AVVideoAverageBitRateKey,
             [NSNumber numberWithInt:1], AVVideoMaxKeyFrameIntervalKey,
             //AVVideoProfileLevelH264Baseline30, AVVideoProfileLevelKey, Not available on 10.7
              nil], AVVideoCompressionPropertiesKey,

            // Specify the HD output color
            [NSDictionary dictionaryWithObjectsAndKeys:
             AVVideoColorPrimaries_ITU_R_709_2, AVVideoColorPrimariesKey,
             AVVideoTransferFunction_ITU_R_709_2, AVVideoTransferFunctionKey,
             AVVideoYCbCrMatrix_ITU_R_709_2, AVVideoYCbCrMatrixKey, nil], AVVideoColorPropertiesKey,
            nil];

self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];// sourceFormatHint:self.formatHint];

/*self.pixelBufferAdaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc]
 initWithAssetWriterInput:self.assetWriterInput
 sourcePixelBufferAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange],//kCVPixelFormatType_32BGRA],
 kCVPixelBufferPixelFormatTypeKey,
 nil]];*/
NSError *error;

self.videoFile = [NSURL fileURLWithPath:file];

//[self.movieFileOutput startRecordingToOutputFileURL:self.videoFile recordingDelegate:self];

self.assetWriter = [[AVAssetWriter alloc]
                    initWithURL:self.videoFile
                    fileType:AVFileTypeMPEG4//AVFileTypeQuickTimeMovie//
                    error:&error];

if (self.assetWriter){
  [self.assetWriter addInput:self.assetWriterInput];

  self.assetWriterInput.expectsMediaDataInRealTime = YES;

以及我设置 AVCaptureVideoDataOutput 并将其添加到我的捕获会话的代码:

AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

output.alwaysDiscardsLateVideoFrames = NO;

  output.videoSettings = nil;

  self.videoQueue = dispatch_queue_create("ca.blackboxsoftware.avcapturequeue", NULL);

  [output setSampleBufferDelegate:self queue:self.videoQueue];

  [self.session addOutput:output];

这个质量问题是我软件的一大绊脚石,我迫切需要你的帮助。我很乐意发布您可能需要查看的任何其他代码,并测试您认为会有所作为的更改。

感谢您的时间。

4

0 回答 0