简体   繁体   English

使用AVAssetWriter重新编码H264 mov文件-如何设置帧频?

[英]Using AVAssetWriter to re-encode H264 mov file - how to set frame-rate?

I'm attempting to re-encode an input MOV file with changeable frame-rate and clipped in duration, in iOS. 我正在尝试在iOS中以可变的帧速率和持续时间剪辑对输入的MOV文件进行重新编码。 At the moment I have an AVAssetWriter setting video properties a bit like this: 目前,我有一个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,
};

That comes out at runtime looking like this: 这样会在运行时显示如下:

videoSettings = 
{
AVVideoCodecKey = avc1;
AVVideoCompressionPropertiesKey =     {
    AverageNonDroppableFrameRate = 15;
    ProfileLevel = "H264_High_4_0";
};
AVVideoHeightKey = 960;
AVVideoWidthKey = 640;
}

At the end of which, I get a crash with NSInvalidArgumentException: "Compression property AverageNonDroppableFrameRate is not supported for video codec type avc1" . 最后,我收到NSInvalidArgumentException: "Compression property AverageNonDroppableFrameRate is not supported for video codec type avc1"崩溃NSInvalidArgumentException: "Compression property AverageNonDroppableFrameRate is not supported for video codec type avc1" (In unit tests using the simulator.) (在使用模拟器的单元测试中。)

There's only one codec type that's useable in iOS, AVVideoCodecH264 / "avc1" - and I notice other projects have used the AVVideoAverageNonDroppableFrameRateKey . 在iOS中,只有一种编解码器类型可用,即AVVideoCodecH264 /“ avc1”-我注意到其他项目也使用了AVVideoAverageNonDroppableFrameRateKey In fact, I'm using SDAVAssetExportSession and in that codebase I see explicit use of this key. 实际上,我正在使用SDAVAssetExportSession ,在该代码库中,我看到了此键的显式使用。 So I would have thought there must be a way to use this key to set frame-rate..? 因此,我本以为必须有一种使用此键来设置帧率的方法。

I've also experimented a bit using AVVideoMaxKeyFrameIntervalKey instead, but that doesn't change my frame-rate at all... 我还使用AVVideoMaxKeyFrameIntervalKey进行了一些实验,但这完全不会改变我的帧速率...

So, to summarise, can anyone help me with setting a different (always lower) output frame-rate for an iOS AVFoundation-based video conversion? 因此,总而言之,有人可以帮助我为基于iOS AVFoundation的视频转换设置不同的(总是更低的)输出帧率吗? Thanks! 谢谢!

As mentioned in the question, I used SDAVAssetExportSession for ease of video export. 正如问题中提到的,我使用SDAVAssetExportSession来简化视频导出。 I made some small changes to it that enabled me to use that easily to change the framerate. 我对其进行了一些小的更改,使我能够轻松地使用它来更改帧速率。

The main gist is you can change framerate using AVMutableVideoComposition , setting the frameDuration property to your desired framerate, and passing this composition object to the AVAssetReaderVideoCompositionOutput object used in the transcoding. 主要要点是,您可以使用AVMutableVideoComposition更改帧速率,将frameDuration属性设置为所需的帧速率,然后将此合成对象传递给转码中使用的AVAssetReaderVideoCompositionOutput对象。

In SDAVAssetExportSession's buildDefaultVideoComposition method, I modified it to look a bit like this: 在SDAVAssetExportSession的buildDefaultVideoComposition方法中,我对其进行了修改,使其看起来像这样:

- (AVMutableVideoComposition *)buildDefaultVideoComposition
{
  AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
  AVAssetTrack *videoTrack = [[self.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

  // ...

  videoComposition.frameDuration = CMTimeMake(1, myDesiredFramerate);

  // ...

That did the trick. 做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM