8

我正在组装一堆在 iPhone 上以纵向模式拍摄的视频剪辑。为了组装它们,我采取了如下简单的方法:

AVURLAsset 来获取不同的视频,然后将它们推入 AVMutableCompositionTrack 中,然后将其放入 AVMutableComposition 中,我将使用 AVAssetExportSession 将其导出到文件中

我的问题是,当我在 UIWebView 中显示视频时,它以横向模式出现。但是,如果我查看任何组件视图,它们会出现在纵向视图中。有谁知道如何整理方向。我尝试使用 AVMutableComposition naturalSize 来改变周围的宽度和高度,但这只会让我的人看起来又矮又胖!(虽然站在他们这边)

提前感谢您的任何想法/建议

都铎王朝

4

3 回答 3

18

如果您只想保留视频方向,则可能需要从 AVAssetTrack 分配 AVMutableCompositionTrack prefferedTransition 值,如下所示:

AVAssetTrack *videoAssetTrack= [[videoAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];

AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error];

videoCompositionTrack.preferredTransform = videoAssetTrack.preferredTransform;
于 2013-09-13T14:24:19.710 回答
12

设置将处理旋转 + 缩放的视频合成:

AVMutableVideoComposition* videoComposition = [[AVMutableVideoComposition videoComposition]retain];
videoComposition.renderSize = CGSizeMake(320, 240);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

AVMutableVideoCompositionLayerInstruction* rotator = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation( 0,-320);    
CGAffineTransform rotateBy90Degrees = CGAffineTransformMakeRotation( M_PI_2);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(0.66, 1); // needed because Apple does a "stretch" by default - really, we should find and undo apple's stretch - I suspect it'll be a CALayer defaultTransform, or UIView property causing this
CGAffineTransform finalTransform = CGAffineTransformConcat( shrinkWidth, CGAffineTransformConcat(translateToCenter, rotateBy90Degrees) );
[rotator setTransform:finalTransform atTime:kCMTimeZero];

instruction.layerInstructions = [NSArray arrayWithObject: rotator];
videoComposition.instructions = [NSArray arrayWithObject: instruction];
于 2011-03-08T11:24:49.353 回答
11

下面是一些使用首选变换调整旋转的代码:

    // our composition, with one video and one audio track
AVMutableComposition* composition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// loop through all the clips, adding them to our track and inserting composition instructions
NSDictionary* options = @{ AVURLAssetPreferPreciseDurationAndTimingKey: @YES };
CMTime insertionPoint = kCMTimeZero;
NSMutableArray* instructions = [NSMutableArray array];
NSError* error = nil;
for (NSURL* outputFileURL in self.movieFileURLs) {
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputFileURL options:options];

    // insert this clip's video track into our composition
    NSArray *videoAssetTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
    AVAssetTrack *videoAssetTrack = (videoAssetTracks.count > 0 ? [videoAssetTracks objectAtIndex:0] : nil);
    [videoTrack insertTimeRange:CMTimeRangeFromTimeToTime(kCMTimeZero, asset.duration) ofTrack:videoAssetTrack atTime:insertionPoint error:&error];

    // create a layer instruction at the start of this clip to apply the preferred transform to correct orientation issues
    AVMutableVideoCompositionLayerInstruction *instruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack];
    [instruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];

    // create the composition instructions for the range of this clip
    AVMutableVideoCompositionInstruction * videoTrackInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoTrackInstruction.timeRange = CMTimeRangeMake(insertionPoint, asset.duration);
    videoTrackInstruction.layerInstructions = @[instruction];
    [instructions addObject:videoTrackInstruction];

    // insert this clip's audio track into our composition
    NSArray *audioAssetTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
    AVAssetTrack *audioAssetTrack = (audioAssetTracks.count > 0 ? [audioAssetTracks objectAtIndex:0] : nil);
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:audioAssetTrack atTime:insertionPoint error:nil];

    // advance the insertion point to the end of the clip
    insertionPoint = CMTimeAdd(insertionPoint, asset.duration);
}

// create our video composition which will be assigned to the player item
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = instructions;
videoComposition.frameDuration = CMTimeMake(1, videoTrack.naturalTimeScale);
videoComposition.renderSize = videoTrack.naturalSize;
_videoComposition = videoComposition;
于 2012-07-13T17:30:17.303 回答