2017-10-16 70 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

相關問題