纵向模式下的 AVVideoCompositionCoreAnimationTool 和 CALayer?

2024-03-27

我正在尝试使用 iOS 4.3 上的 AVMutableComposition、AVMutableVideoComposition 和 AVVideoCompositionCoreAnimationTool 将 CALayer 烘焙为纵向模式视频(导出时)。这一切都适用于风景。但是,如果我以纵向方式捕获视频,AVVideoCompositionCoreAnimationTool 将忽略视频轨道上的转换。也就是说,对于纵向模式视频,我将 AVMutableCompositionTrack.preferredTransform 设置为原始资源视频轨道中的 PreferredTransform 值。只要我不使用 AVVideoCompositionCoreAnimationTool,它就可以工作,并且视频会以纵向模式显示。然而,一旦我添加 AVVideoCompositionCoreAnimationTool 和 CALayer,该文件就会以横向方式显示。 (CALayer 显示正确,但其后面的视频在其一侧,并且文件的宽高比关闭)。我尝试将变换应用于 CALayer,并在 ACVideoComposition 中设置变换。这些都不会改变生成的文件的方向(它仍然是 480x369,而不是 360x480)。有没有办法使用 AVVideoCompositionCoreAnimationTool 渲染纵向模式视频?

首先我设置了 AVMutableComposition 和 AVMutableVideoComposition

AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

CGSize videoSize = CGSizeApplyAffineTransform(clipVideoTrack.naturalSize, clipVideoTrack.preferredTransform);
videoSize.width = fabs(videoSize.width);
videoSize.height = fabs(videoSize.height);

CMTime titleDuration = CMTimeMakeWithSeconds(5, 600);
CMTimeRange titleRange = CMTimeRangeMake(kCMTimeZero, titleDuration);

[compositionVideoTrack insertTimeRange:titleRange ofTrack:nil atTime:kCMTimeZero error:nil];
[compositionVideoTrack insertTimeRange:timeRange ofTrack:clipVideoTrack atTime:titleDuration error:nil];
compositionVideoTrack.preferredTransform = clipVideoTrack.preferredTransform;

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, [composition duration]);
AVAssetTrack *videoTrack = [[composition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];    
passThroughInstruction.layerInstructions = [NSArray arrayWithObject:passThroughLayer];
videoComposition.instructions = [NSArray arrayWithObject:passThroughInstruction];       
videoComposition.frameDuration = CMTimeMake(1, 30); 
videoComposition.renderSize = videoSize;
videoComposition.renderScale = 1.0;

和一个带有标题的 CALayer

CALayer *animationLayer = [CALayer layer];
animationLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);

CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string = [effect valueForKey:@"title"];
titleLayer.font = [effect valueForKey:@"font"];
titleLayer.fontSize = 30;

titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6);

[animationLayer addSublayer:titleLayer];
titleLayer.anchorPoint =  CGPointMake(0.5, 0.5);
titleLayer.position = CGPointMake(CGRectGetMidX(layer.bounds), CGRectGetMidY(layer.bounds));    

CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.additive = NO;
fadeAnimation.removedOnCompletion = NO;
fadeAnimation.beginTime = 3.5;
fadeAnimation.duration = 1.0;
fadeAnimation.fillMode = kCAFillModeBoth;
[titleLayer addAnimation:fadeAnimation forKey:nil];

最后我将 CALayer 添加到 AVMutableVideoComposition

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];

parentLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
parentLayer.anchorPoint =  CGPointMake(0, 0);
parentLayer.position = CGPointMake(0, 0);

videoLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
videoLayer.anchorPoint =  CGPointMake(0.5, 0.5);
videoLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
[parentLayer addSublayer:layer];    
animationLayer.anchorPoint =  CGPointMake(0.5, 0.5);
animationLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

并出口!

AVAssetExportSession *exportSession = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality] autorelease];
exportSession.videoComposition = videoComposition; 

NSURL *segmentFileURL = //some local URL
exportSession.outputFileType = @"com.apple.quicktime-movie";
exportSession.outputURL = segmentFileURL;


[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch ([exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            Log(@"Export failed: %@", [exportSession error]);
            break;
        case AVAssetExportSessionStatusCancelled:
            Log(@"Export canceled");
            break;
        case AVAssetExportSessionStatusCompleted:
            Log(@"Export done");
            break;
    }
}]; 

该代码适用于横向模式,如果我删除该行,也适用于纵向模式videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];


尝试这个并添加您的其他说明。

///turn video 90 degrees
AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,320,0);
[passThroughLayer setTransform:rotateTranslate atTime:kCMTimeZero];

让我知道事情的后续!

MacMike

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

纵向模式下的 AVVideoCompositionCoreAnimationTool 和 CALayer? 的相关文章

  • EXC_BAD_INSTRUCTION 的 CoreData 错误(代码=EXC_I386_INVOP,子代码=0x0)

    当我打开并发调试开关 com apple CoreData ConcurrencyDebug 1 来跟踪 CoreData 的所有并发问题时 在调用 insertingNewObjectForEntityForName 时不断发生崩溃 Xc
  • 错误 ITMS-90085:“二进制文件中没有体系结构。Lipo 无法检测到捆绑可执行文件中的任何体系结构。”

    操作系统 OS X Yosemite 版本 10 10 1XCode 未安装应用程序加载器3 0 620 电话间隙 3 7 0PhoneGap 构建 在线 build phonegap com 在验证 iTunes 步骤时 出现错误 ITM
  • Iphone safari 无法在键盘打开时调整视口大小

    当键盘弹出时 Mobile safari 不会更新 window innerHeight 至少在9 3 5中 并且有几个答案 例如this https stackoverflow com a 17604856 2423187一 有评论说在
  • 当 UIView 通过自动布局调整大小时,会调用什么方法?

    我有一个图像视图 我通过在子类中覆盖以下内容来实现圆角 void setFrame CGRect frame super setFrame frame self layout setCornerRadius frame size width
  • swift:移动动画

    我在故事板中有 viewController 还有4个正方形 我想将我的方块放在视图中 首先我想显示两个正方形 如果我按下按钮 我希望我的红色 2 个方块向左移动 然后显示接下来的 2 个蓝色方块 就像这部动画 我需要创建一个scrollV
  • 如何将 UILabel 与个人资料照片图像水平对齐?

    我必须显示名称和电子邮件 ID 与个人资料图像正确水平对齐 这样姓名和电子邮件 ID 就出现在 UIImageView 的中心 但您可以看到姓名和电子邮件 ID 不在个人资料图片的中心 为什么会发生这种情况 我给出了以下限制 删除前导和尾随
  • 如何在 UINavigationController 中收到弹出视图的通知?

    我想在用户按下我的后退按钮时执行操作UINavigationController当到达某个时UIViewController 不幸的是它看起来像UINavigationControllerDelegate没有任何方法来获取视图弹出的通知 作
  • Objective C - 动态属性的respondsToSelector

    我目前面临的问题是检查对象 NSManagedObject 的属性是否存在 不幸的是方法 MyObject class respondsToSelector selector myProperty 总是返回NO 我认为这是因为CoreDat
  • 如何让我的“点击”功能与 iOS 配合使用

    我有一组充当按钮的 Div 这些按钮有一个简单的 jquery click 函数 该函数适用于除 iOS 之外的所有浏览器 例如 div class button click me div and button click function
  • Swift 每 5 天重复一次 LocalNotification

    如何每 5 天上午 10 00 重复一次 LocalNotification 我尝试这个 但它不起作用 let content UNMutableNotificationContent content title Hello content
  • Xcode 8.2 更新后二进制文件无效

    我今天尝试在更新到 Xcode 8 2 后向我的应用程序推送更新 但收到无效的二进制错误 我以前从未见过这个 我的应用程序的 iOS 部署目标是 iOS 9 0 有谁见过这个错误或知道如何修复它 这是电子邮件的内容 解释了二进制文件的无效内
  • 在 iOS 中获取 Facebook 好友时出错

    我正在尝试获取登录用户的 Facebook 好友列表 它在我的帐户中工作正常 但是当我将应用程序详细信息迁移到新帐户并更改应用程序 ID 和应用程序密码时 我收到以下错误 Error Domain com facebook sdk Code
  • iOS 应用程序崩溃 com.apple.root.background-qos

    在应用程序中发现应用程序崩溃 我怀疑这可能是由于 firebase 观察者的代码而发生的 由于在用户案例中 用户可以从一个事件转到用户配置文件 参与此事件 然后从用户配置文件可以返回到此事件 我需要一个 ref 句柄来删除特定的观察者 因此
  • 使用 popToRootViewController 时我丢失了导航栏

    我有一个 iOS 应用程序 其中主屏幕是 UICollectionViewController 从集合视图中选择项目时 视图将推送到该项目的详细信息视图 在细节视图中 我构建了一个从侧面移出的抽屉 滑块 为了让视图看起来像我想要的那样 我隐
  • 使用本地化故事板进行即时本地化

    我正在开发一个应用程序 它有一个切换按钮可以在英语和阿拉伯语之间切换 并且应该是动态的 我正在使用该方法https github com maximbilan ios language manager https github com ma
  • UITextField 中光标闪烁,但键盘不出现

    我得到了一个带有文本字段的简单详细视图 在详细的viewController中我写了这段代码 void viewDidAppear BOOL animated self textField becomeFirstResponder NSLo
  • dequeueReusableCellWithIdentifier: 如何工作?

    我想要一些精确的信息dequeueReusableCellWithIdentifier kCellIdentifier 如果我理解得很好 下面的 NSLOG 应该只打印一次 但事实并非如此 那么 dequeueReusableCell 的意
  • 允许在 Safari 上聊天应用程序使用 audio.play()

    由于苹果禁用了自动播放音频的功能HTMLMedia Element play https developer mozilla org en US docs Web API HTMLMediaElement play在没有用户交互的 java
  • 检测 UITableViewCell 何时离开屏幕

    我正在实施一个丰富的UITableView与定制创建UITableViewCell 我以一种方式在屏幕上显示这些 但是一旦它们离开屏幕 我想记下这一点 因为它们第二次出现时我希望它们以不同的方式显示 认为离开屏幕时自动 标记为已读 我一直在
  • MapKit 注释未显示在地图上

    我无法让 MKAnnotationViews 显示在 MapKit 的地图上 我正在使用 iOS 7 现在已经在论坛和网络上搜索了很多小时 尝试不同的示例和设置 下面我有 我认为 使其工作的最基本的设置 该应用程序包含一个 ViewCont

随机推荐