我有一个项目,目前使用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类的设置如下:
{"click_id": 124, "created_at": "2017-02-03T10:51:33", "product_id": 97373, "product_price": 320.50, "user_id": 1, "ip": "null","status":"New"}
{"click_id": 125, "created_at": "2017-10-03T10:52:33", "product_id": 96373, "product_price": 20.50, "user_id": 1, "ip": "London","status":"Old"}
{"click_id": 126, "created_at": "2017-10-03T11:50:33", "product_id": 88373, "product_price": 220.50, "user_id": 2, "ip": "London","status":"Valid"}
然后帧写成这样:
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)
}
}
。根据我录制的质量(720p或1080p),HEVC编码视频的文件大小应该是相同H.264编码视频的40-60%,我在使用默认相机应用程序时看到了这一点。 iOS,但是当我使用上面的AVAssetWriter时(或上面链接的项目中),我看到HEVC的文件大小比H.264大三倍。要么我做错了,要么HEVC编码器工作不正常。我是否遗漏了某些内容,或者是否有解决方法让HEVC通过AVFoundation工作?
答案 0 :(得分:2)
您是否尝试过指定比特率等? 如下:
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的质量应该更好。
答案 1 :(得分:0)
使用External Libraries
10MB是硬编码号码。根据所需的比特率使用。