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

标签 ios swift avfoundation avassetwriter hevc

我有一个项目目前使用 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 的质量应该更好。

关于ios - 在 iOS 上使用 HEVC 编码器输出巨大的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46778436/

相关文章:

ios - 如何使用 Swift 2 从 xib 加载自定义 UIView?

ios - 如何使用 AVURLAsset 流式传输视频并将缓存数据保存到磁盘

ios - 如何依赖 MFMailComposeViewController 及其类方法进行单元测试?

ios - UICollectionViewCell 叠加 View

ios - 从 super View 转换点时委托(delegate)不工作

swift - 如何快速裁剪所选区域的视频帧大小?

objective-c - AVFoundation 获取歌曲的专辑图片

ios - 在IOS中自定义UINavigationBar而不使用UINavigationController设置背景图像不起作用

带有自定义初始化器的 Swift 枚举丢失了 rawValue 初始化器

swift - RxSwift 跳过事件直到自己的序列完成