[英]How to use AVAssetWriter instead of AVAssetExportSession to re-encode existing video
[英]Using AVAssetWriter to re-encode H264 mov file - how to set frame-rate?
我正在嘗試在iOS中以可變的幀速率和持續時間剪輯對輸入的MOV文件進行重新編碼。 目前,我有一個AVAssetWriter設置視頻屬性,如下所示:
NSMutableDictionary* compressionPropertiesDict = [NSMutableDictionary new];
compressionPropertiesDict[AVVideoProfileLevelKey] = AVVideoProfileLevelH264High40;
if(self.fps > 0) {
compressionPropertiesDict[AVVideoAverageNonDroppableFrameRateKey] = [NSNumber numberWithInt:self.fps];
_sessionMgr.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: [NSNumber numberWithFloat:self.outputSize.width],
AVVideoHeightKey: [NSNumber numberWithFloat:self.outputSize.height],
AVVideoCompressionPropertiesKey: compressionPropertiesDict,
};
這樣會在運行時顯示如下:
videoSettings =
{
AVVideoCodecKey = avc1;
AVVideoCompressionPropertiesKey = {
AverageNonDroppableFrameRate = 15;
ProfileLevel = "H264_High_4_0";
};
AVVideoHeightKey = 960;
AVVideoWidthKey = 640;
}
最后,我收到NSInvalidArgumentException: "Compression property AverageNonDroppableFrameRate is not supported for video codec type avc1"
崩潰NSInvalidArgumentException: "Compression property AverageNonDroppableFrameRate is not supported for video codec type avc1"
。 (在使用模擬器的單元測試中。)
在iOS中,只有一種編解碼器類型可用,即AVVideoCodecH264 /“ avc1”-我注意到其他項目也使用了AVVideoAverageNonDroppableFrameRateKey
。 實際上,我正在使用SDAVAssetExportSession ,在該代碼庫中,我看到了此鍵的顯式使用。 因此,我本以為必須有一種使用此鍵來設置幀率的方法。
我還使用AVVideoMaxKeyFrameIntervalKey
進行了一些實驗,但這完全不會改變我的幀速率...
因此,總而言之,有人可以幫助我為基於iOS AVFoundation的視頻轉換設置不同的(總是更低的)輸出幀率嗎? 謝謝!
正如問題中提到的,我使用SDAVAssetExportSession來簡化視頻導出。 我對其進行了一些小的更改,使我能夠輕松地使用它來更改幀速率。
主要要點是,您可以使用AVMutableVideoComposition
更改幀速率,將frameDuration
屬性設置為所需的幀速率,然后將此合成對象傳遞給轉碼中使用的AVAssetReaderVideoCompositionOutput
對象。
在SDAVAssetExportSession的buildDefaultVideoComposition
方法中,我對其進行了修改,使其看起來像這樣:
- (AVMutableVideoComposition *)buildDefaultVideoComposition
{
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
AVAssetTrack *videoTrack = [[self.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// ...
videoComposition.frameDuration = CMTimeMake(1, myDesiredFramerate);
// ...
做到了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.