【问题标题】:iPhone SDK. is it possible to rotate videos in AVplayer using AVURLlAsset / AVMutableComposition?iPhone SDK。是否可以使用 AVURLlAsset / AVMutableComposition 在 AVplayer 中旋转视频?
【发布时间】:2011-03-01 16:43:26
【问题描述】:

我有一个使用 AVPlayer 的简单视频编辑视图。我使用一个或多个 AVURLAsset 创建了一个 AVMutableComposition。一切正常,但视频总是旋转 90 度。即,如果源视频是以纵向模式拍摄的。 AVPlayer 以横向模式显示它,反之亦然。我已经浏览了文档并将其“谷歌搜索”到死,但找不到解决方案。可能我找错地方了。

谁能帮忙?

提前致谢;

让-皮埃尔

【问题讨论】:

  • 在 AVFoundation 上观看了 WWDC10 会议后,我相信某处有一个设置,但我需要再看一遍。当我确定时我会发布答案
  • 你解决过这个问题吗?我有同样的问题。
  • 你解决了这个问题?我有同样的问题,我不知道该怎么办

标签: iphone avplayer avurlasset avmutablecomposition


【解决方案1】:

据我了解,您有一些方向问题,例如纵向视频处于横向模式,有时视频会上下颠倒。这是由于默认的 AVAsset 方向。使用默认 iPhone 相机应用程序录制的所有电影和图像文件都将视频帧设置为横向,因此媒体以横向模式保存。 AVAsset 有一个包含媒体方向信息的preferredTransform 属性,当您使用Photos 应用程序或QuickTime 查看媒体文件时,该属性将应用到媒体文件。 您可以通过对 AVAsset 对象应用必要的转换来轻松纠正此问题。但是由于您的两个视频文件可以有不同的方向,您需要使用两个单独的 AVMutableCompositionTrack 实例,而不是像最初那样使用一个(假设)。 创建两个 AVMutableCompositionTrack 视频轨道 由于您现在有两个单独的 AVMutableCompositionTrack 实例,因此您需要对每个轨道应用 AVMutableVideoCompositionLayerInstruction 以固定方向。所以添加以下代码

    //  Create AVMutableVideoCompositionInstruction
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration));
// Create an AVMutableVideoCompositionLayerInstruction for the first track
AVMutableVideoCompositionLayerInstruction *firstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];
AVAssetTrack *firstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
UIImageOrientation firstAssetOrientation_  = UIImageOrientationUp;
BOOL isFirstAssetPortrait_  = NO;
CGAffineTransform firstTransform = firstAssetTrack.preferredTransform;
if (firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0) {
    firstAssetOrientation_ = UIImageOrientationRight; 
    isFirstAssetPortrait_ = YES;
}
if (firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) {
    firstAssetOrientation_ =  UIImageOrientationLeft; 
    isFirstAssetPortrait_ = YES;
}
if (firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) {
    firstAssetOrientation_ =  UIImageOrientationUp;
}
if (firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) {
    firstAssetOrientation_ = UIImageOrientationDown;
}
[firstlayerInstruction setTransform:firstAsset.preferredTransform atTime:kCMTimeZero];
[firstlayerInstruction setOpacity:0.0 atTime:firstAsset.duration];
//  Create an AVMutableVideoCompositionLayerInstruction for the second track
AVMutableVideoCompositionLayerInstruction *secondlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:secondTrack];
AVAssetTrack *secondAssetTrack = [[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
UIImageOrientation secondAssetOrientation_  = UIImageOrientationUp;
BOOL isSecondAssetPortrait_  = NO;
CGAffineTransform secondTransform = secondAssetTrack.preferredTransform;
if (secondTransform.a == 0 && secondTransform.b == 1.0 && secondTransform.c == -1.0 && secondTransform.d == 0) {
    secondAssetOrientation_= UIImageOrientationRight; 
    isSecondAssetPortrait_ = YES;
}
if (secondTransform.a == 0 && secondTransform.b == -1.0 && secondTransform.c == 1.0 && secondTransform.d == 0) {
    secondAssetOrientation_ =  UIImageOrientationLeft; 
    isSecondAssetPortrait_ = YES;
}
if (secondTransform.a == 1.0 && secondTransform.b == 0 && secondTransform.c == 0 && secondTransform.d == 1.0) {
    secondAssetOrientation_ =  UIImageOrientationUp;
}
if (secondTransform.a == -1.0 && secondTransform.b == 0 && secondTransform.c == 0 && secondTransform.d == -1.0) {
    secondAssetOrientation_ = UIImageOrientationDown;
}
[secondlayerInstruction setTransform:secondAsset.preferredTransform atTime:firstAsset.duration];
}

最后添加指令,它只是应用于第二个轨道的方向修复。

    mainInstruction.layerInstructions = [NSArray arrayWithObjects:firstlayerInstruction, secondlayerInstruction,nil];
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);

CGSize naturalSizeFirst, naturalSizeSecond;
if(isFirstAssetPortrait_){
    naturalSizeFirst = CGSizeMake(FirstAssetTrack.naturalSize.height, FirstAssetTrack.naturalSize.width);
} else {
    naturalSizeFirst = FirstAssetTrack.naturalSize;
}
if(isSecondAssetPortrait_){
    naturalSizeSecond = CGSizeMake(SecondAssetTrack.naturalSize.height, SecondAssetTrack.naturalSize.width);
} else {
    naturalSizeSecond = SecondAssetTrack.naturalSize;
}

float renderWidth, renderHeight;
if(naturalSizeFirst.width > naturalSizeSecond.width) {
    renderWidth = naturalSizeFirst.width;
} else {
    renderWidth = naturalSizeSecond.width;
}
if(naturalSizeFirst.height > naturalSizeSecond.height) {
    renderHeight = naturalSizeFirst.height;
} else {
    renderHeight = naturalSizeSecond.height;
}
MainCompositionInst.renderSize = CGSizeMake(renderWidth, renderHeight);

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果没有要检查的代码,很难知道发生了什么……但很可能,您的问题是由于您丢失了 preferredTransform 属性的值而引起的。

    所有AVAssetTrack 对象都有一个preferredTransform 属性,用于指定是否应旋转视频。如果您要创建新的 AVMutableAssetTrack 对象,您可能需要设置该属性的值,以便视频保持预期的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多