在 iOS 上使用 HEVC 编码器输出巨大的视频

Output Video Size Huge Using HEVC Encoder on iOS

我有一个项目目前使用 H.264 编码器在 iOS 上录制视频。我想尝试在 iOS 11 中使用新的 HEVC 编码器来减小文件大小,但发现使用 HEVC 编码器会导致文件大小急剧膨胀。 Here's a project on GitHub that shows the issue - it simultaneously writes frames from the camera to files using the H.264 and H.265 (HEVC) encoders, and the resulting file sizes are printed to the console.

AVFoundation 类 是这样设置的:

class VideoWriter {
    var avAssetWriterInput: AVAssetWriterInput
    var avAssetWriter: AVassetWriter
    init() {
        if #available(iOS 11.0, *) {
            avAssetWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: [AVVideoCodecKey:AVVideoCodecType.hevc, AVVideoHeightKey:720, AVVideoWidthKey:1280])
        }
        avAssetWriterInput.expectsMediaDataInRealTime = true
        do {
            let url = directory.appendingPathComponent(UUID.init().uuidString.appending(".hevc"))
            avAssetWriter = try AVAssetWriter(url: url, fileType: AVFileType.mp4)
            avAssetWriter.add(avAssetWriterInput)
            avAssetWriter.movieFragmentInterval = kCMTimeInvalid
        } catch {
            fatalError("Could not initialize AVAssetWriter \(error)")
        }
    }
...

然后帧是这样写的:

    func write(sampleBuffer buffer: CMSampleBuffer) {
        if avAssetWriter.status == AVAssetWriterStatus.unknown {
            avAssetWriter.startWriting()
            avAssetWriter.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(buffer))
         }
        if avAssetWriterInput.isReadyForMoreMediaData {
            avAssetWriterInput.append(buffer)
        }
    }

当他们进入 AVCaptureVideoDataOutputSampleBufferDelegate 时。在我录制的质量(720p 或 1080p)下,HEVC 编码视频的文件大小应该是相同 H.264 编码视频的 40-60%,当我在iOS,但是当我如上所述(或在上面链接的项目中)使用 AVAssetWriter 时,我发现 HEVC 的文件大小大约是 H.264 的三倍。要么我做错了什么,要么 HEVC 编码器工作不正常。我是不是遗漏了什么或者是否有解决方法让 HEVC 通过 AVFoundation 工作?

您尝试过指定比特率等吗? 如下:

NSUInteger bitrate = 50 * 1024 * 1024;  // 50 Mbps
NSUInteger keyFrameInterval = 30;
NSString *videoProfile = AVVideoProfileLevelH264HighAutoLevel;
NSString *codec = AVVideoCodecH264;
if (@available(iOS 11, *)) {
    videoProfile = (NSString *)kVTProfileLevel_HEVC_Main_AutoLevel;
    codec = AVVideoCodecTypeHEVC;
}

NSDictionary *codecSettings = @{AVVideoAverageBitRateKey: @(bitrate),
                              AVVideoMaxKeyFrameIntervalKey: @(keyFrameInterval),
                              AVVideoProfileLevelKey: videoProfile};
NSDictionary *videoSettings = @{AVVideoCodecKey: codec,
                              AVVideoCompressionPropertiesKey: codecSettings,
                              AVVideoWidthKey: @((NSInteger)resolution.width),
                              AVVideoHeightKey: @((NSInteger)resolution.height)};

AVAssetWriterInput *videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
...

据我了解,在码率相同的情况下,H264 和 HEVC 的文件大小应该相同,但 HEVC 的质量应该更好。

使用exportSession.fileLengthLimit = 1048576 * 10 //10 MB

10MB 是硬编码数字。根据您需要的码率使用。