2017-10-16 69 views
3

我有一个项目目前使用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.在iOS上使用HEVC编码器输出视频大小

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

回答

1

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

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

+0

我收到了类似的回应苹果。显然,H.264的默认比特率是5mbit,HEVC是30mbit。如果我尝试了这一点,它会起作用,我会报告并接受你的答案! – jpetrichsr

相关问题