AVMutableComposition - scaleTimeRange导致高比特率视频在快速运动时断断续续

时间:2015-04-21 22:42:23

标签: ios video avfoundation avplayer avmutablecomposition

我正在使用scaleTimeRange:toDuration:产生高达原始视频速度10倍的快动作效果。但是我注意到具有更高比特率(比如20MBits / s及以上)的视频在通过AVPlayer播放时开始出现断断续续,超过正常速度的4倍,足以让AVPlayerLayer崩溃(如果它运行一段时间后消失)

代码。

//initialize the player
self.player = [[AVPlayer alloc] init];

//load up the asset
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[[NSBundle mainBundle] URLForResource:@"sample-video" withExtension:@"mov"] options:nil];

[asset loadValuesAsynchronouslyForKeys:@[@"playable", @"hasProtectedContent", @"tracks"] completionHandler:
 ^{
     AVMutableComposition *composition = [AVMutableComposition composition];
     AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];

     // composition set to play at 60fps
     videoComposition.frameDuration = CMTimeMake(1,60);

     //add video track to composition
     AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

     //1080p composition
     CGSize renderSize = CGSizeMake(1920.0, 1080.0);
     CMTime currentTime = kCMTimeZero;
     CGFloat scale = 1.0;
     AVAssetTrack *assetTrack = nil;

     assetTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;

     [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:assetTrack  atTime:currentTime error:nil];


     CMTimeRange scaleTimeRange = CMTimeRangeMake(currentTime, asset.duration);

     //Speed it up to 8x.
     CMTime scaledDuration = CMTimeMultiplyByFloat64(asset.duration,1.0/8.0);

     [videoTrack scaleTimeRange:scaleTimeRange toDuration:scaledDuration];

     AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

     //ensure video is scaled up/down to match the composition
     scale = renderSize.width/assetTrack.naturalSize.width;
     [layerInstruction setTransform:CGAffineTransformMakeScale(scale, scale) atTime:currentTime];


     AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
     videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
     videoInstruction.layerInstructions = @[ layerInstruction];

     videoComposition.instructions = @[videoInstruction];
     videoComposition.renderSize = renderSize;

     //pass the stuff to AVPlayer for playback
     self.playerItem = [AVPlayerItem playerItemWithAsset:composition];
     self.playerItem.videoComposition = videoComposition;
     [self.player replaceCurrentItemWithPlayerItem:self.playerItem];

     //playerView is a custom view with AVPlayerLayer, picked up from https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html#//apple_ref/doc/uid/TP40010188-CH3-SW11

     [self.playerView setPlayer:self.player];

     //call [self.player play] when ready.


 }];

一些注意事项:

  • 在iPhone 6上完成的所有测试
  • 我故意不添加任何音轨以排除音频在这里播放的可能性。
  • 正常比特率视频(平均16Mbits / s)在10x
  • 上正常播放
  • 相同的合成代码可在OSX应用程序上平滑播放
  • 比特率越高,口吃越明显。
  • 所有正在测试的视频均为1080p 60fps
  • 如果打开并输出到1080,则高比特率视频表现良好,因此降低比特率并保持FPS。
  • 没有涉及视频的渲染/导出。

有没有其他人遇到这个并知道解决方法?

0 个答案:

没有答案