ios - AVMutableVideoComposition 停止渲染视频

标签 ios objective-c video avfoundation

我正在尝试根据本教程在不同位置同时播放 2 个视频的组合:https://abdulazeem.wordpress.com/2012/04/02/video-manipulation-in-ios-resizingmerging-and-overlapping-videos-in-ios/

这是我的代码:

-(AVPlayer*)create{

// assets
AVAsset* videoAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video_mov" ofType:@"mov"]]];
AVAsset* videoAsset2 = [AVAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video_mov" ofType:@"mov"]]];

// create a mutable composition and 2 tracks
AVMutableComposition* mutableComposition = [AVMutableComposition composition];

AVMutableCompositionTrack* videoTrack1 = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack* videoTrack2 = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

// add tracks from the video asset into the videoTrack1 (MutableCompositionTrack)
[videoTrack1 insertTimeRange: CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                     ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                      atTime:kCMTimeZero
                       error:nil];

[videoTrack2 insertTimeRange: CMTimeRangeMake(kCMTimeZero, videoAsset2.duration)
                     ofTrack:[[videoAsset2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                      atTime:kCMTimeZero
                       error:nil];

// Add audio tracks from the audio asset to our audioCompositionTrack
[audioCompositionTrack insertTimeRange: CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
                               ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                atTime:kCMTimeZero
                                 error:nil];

// Now create composition instructions for
AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);

// Track 1
AVMutableVideoCompositionLayerInstruction* FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack1];
CGAffineTransform Scale = videoTrack1.preferredTransform;
CGAffineTransform Move = CGAffineTransformMakeTranslation(300,200);
[FirstlayerInstruction setTransform:CGAffineTransformConcat(Scale,Move) atTime:kCMTimeZero];

// Track 2
AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack2];
CGAffineTransform SecondScale = videoTrack1.preferredTransform;
CGAffineTransform SecondMove = CGAffineTransformMakeTranslation(0,0);
[SecondlayerInstruction setTransform:CGAffineTransformConcat(SecondScale,SecondMove) atTime:kCMTimeZero];

//Now we add our 2 created AVMutableVideoCompositionLayerInstruction objects to our AVMutableVideoCompositionInstruction in form of an array.
MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil];

// now create a video composition with the instruction, and set our instructions
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = [NSArray arrayWithObject:MainInstruction];
videoComposition.frameDuration = CMTimeMake(1, 30);
videoComposition.renderSize = mutableComposition.naturalSize;

// Create the player item
AVPlayerItem* avPlayerItem =[[AVPlayerItem alloc]initWithAsset:mutableComposition];
avPlayerItem.videoComposition = videoComposition;

// Create the player and return it
AVPlayer* avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];

return avPlayer;

视频不呈现。

如果我删除该行:

avPlayerItem.videoComposition = videoComposition;

视频再次呈现,因为它没有设置自定义 videoComposition。我看到的其他几个例子似乎也是以同样的方式进行的。

我只能假设我的转换数据不正确,尽管我尝试了各种不同的数字。

最佳答案

尝试创建不可变合成苹果不建议玩可变合成

AVComposition *immutableSnapshotOfMyComposition = [myMutableComposition copy];

// Create a player to inspect and play the composition.
AVPlayerItem *playerItemForSnapshottedComposition =
    [[AVPlayerItem alloc] initWithAsset:immutableSnapshotOfMyComposition];

关于ios - AVMutableVideoComposition 停止渲染视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968425/

相关文章:

ios - 带有部分问题的 UITableView?

iphone - SQLite3 数据库未通过 NSLog 在控制台上显示条目(将数据保存到数据库有效)

ios - UILabel 上的 SizeToFit 和自动布局

ios - 转换以创建在 Apple 的相机应用程序中看到的桶形效果

ios - 根据 key 的值从字典的 NSArray 中获取公共(public)元素

android - 使用 phonegap 'captureVideo' 捕获视频后将视频保存到库

ios - 在 iOS 中播放视频失败

ASP.Net:上传视频,创建缩略图

ios - 如何从持续集成服务器推送IPA到App Center分发

ios - 何时以及如何关闭 UIActivityViewController