【问题标题】:Using AVAssetWriter to re-encode H264 mov file - how to set frame-rate?使用 AVAssetWriter 重新编码 H264 mov 文件 - 如何设置帧速率?
【发布时间】:2017-05-16 12:35:07
【问题描述】:

我正在尝试在 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" 的崩溃。 (在使用模拟器的单元测试中。)

只有一种编解码器类型可以在 iOS 中使用,AVVideoCodecH264 / "avc1" - 我注意到其他项目使用了 AVVideoAverageNonDroppableFrameRateKey。事实上,我使用的是SDAVAssetExportSession,在那个代码库中我看到了这个键的明确使用。所以我会认为必须有一种方法可以使用这个键来设置帧率..?

我也尝试过使用AVVideoMaxKeyFrameIntervalKey 来代替,但这根本不会改变我的帧速率...

所以,总而言之,谁能帮我为基于 iOS AVFoundation 的视频转换设置不同的(总是较低的)输出帧速率?谢谢!

【问题讨论】:

  • 您找到解决方案了吗?
  • 啊是的,我做了,将作为答案发布!

标签: ios avfoundation avassetwriter


【解决方案1】:

如问题中所述,我使用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);

  // ...

成功了。

【讨论】:

    猜你喜欢
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多