在iOS上使用HEVC编码器输出巨大的视频 [英] Output Video Size Huge Using HEVC Encoder on iOS

查看:607
本文介绍了在iOS上使用HEVC编码器输出巨大的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,当前使用H.264编码器在iOS上录制视频.我想尝试在iOS 11中使用新的HEVC编码器来减小文件大小,但是发现使用HEVC编码器会导致文件大小急剧膨胀. 这是GitHub上的一个项目,可显示该问题-它同时使用H.264和H将相机中的帧写入文件.265(HEVC)编码器,并将生成的文件大小打印到控制台.

I have a project that currently uses the H.264 encoder to record video on iOS. I wanted to try using the new HEVC encoder in iOS 11 to reduce file sizes, but have found that using the HEVC encoder causes file sizes to balloon enormously. 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类的设置如下:

The AVFoundation classes are setup like this:

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工作?

as they come in to the AVCaptureVideoDataOutputSampleBufferDelegate. At the qualities I'm recording (720p or 1080p) the file size of an HEVC-encoded video should be 40-60% of an identical H.264-encoded video, and I am seeing this when I use the default camera app on iOS, but when I use AVAssetWriter as above (or in the project linked above) I'm seeing file sizes be about three times larger with HEVC than with H.264. Either I am doing something wrong or the HEVC encoder is not working properly. Am I missing something or is there a workaround to get HEVC working through AVFoundation?

推荐答案

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

Have you tried to specify the bitrate, etc.? As below:

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的质量应该更好.

As far as I understand, with the same bitrate, the file size should be the same for H264 and HEVC, but the quality of the HEVC should be better.

这篇关于在iOS上使用HEVC编码器输出巨大的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆