AVAssetExportSession忽略视频合成旋转和剥离元数据。

12

我正在尝试在我的iOS设备上上传视频之前旋转视频,因为其他平台(如Android)不能正确地解释iOS录制的视频中的旋转信息,导致播放时显示为不正确的方向。

我查看了以下堆栈帖子,但没有成功将它们应用到我的情况中:


我复制了Apple AVSimpleEditor 项目示例,但不幸的是,在创建AVAssetExportSession并调用exportAsynchronouslyWithCompletionHandler后,不会执行任何旋转操作,并且更糟糕的是,旋转元数据被剥离出导出的文件。

这里是运行导出代码:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:[_mutableComposition copy] presetName:AVAssetExportPresetPassthrough];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileType3GPP;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.videoComposition = _mutableVideoComposition;

[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {
     NSLog(@"Status is %d %@", exportSession.status, exportSession.error);

     handler(exportSession);
     [exportSession release];
 }];

这个方法在此处初始化了可变组合值_mutableComposition和可变视频组合值_mutableVideoComposition:

- (void) getVideoComposition:(AVAsset*)asset
{

    AVMutableComposition *mutableComposition = nil;
    AVMutableVideoComposition *mutableVideoComposition = nil;

    AVMutableVideoCompositionInstruction *instruction = nil;
    AVMutableVideoCompositionLayerInstruction *layerInstruction = nil;
    CGAffineTransform t1;
    CGAffineTransform t2;

    AVAssetTrack *assetVideoTrack = nil;
    AVAssetTrack *assetAudioTrack = nil;
    // Check if the asset contains video and audio tracks
    if ([[asset tracksWithMediaType:AVMediaTypeVideo] count] != 0) {
        assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo][0];
    }
    if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] != 0) {
        assetAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio][0];
    }

    CMTime insertionPoint = kCMTimeZero;
    NSError *error = nil;


    // Step 1
    // Create a composition with the given asset and insert audio and video tracks into it from the asset
    // Check whether a composition has already been created, i.e, some other tool has already been applied
    // Create a new composition
    mutableComposition = [AVMutableComposition composition];

    // Insert the video and audio tracks from AVAsset
    if (assetVideoTrack != nil) {
        AVMutableCompositionTrack *compositionVideoTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:assetVideoTrack atTime:insertionPoint error:&error];
    }
    if (assetAudioTrack != nil) {
        AVMutableCompositionTrack *compositionAudioTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:assetAudioTrack atTime:insertionPoint error:&error];
    }


    // Step 2
    // Translate the composition to compensate the movement caused by rotation (since rotation would cause it to move out of frame)
    t1 = CGAffineTransformMakeTranslation(assetVideoTrack.naturalSize.height, 0.0);
    // Rotate transformation
    t2 = CGAffineTransformRotate(t1, degreesToRadians(90.0));


    // Step 3
    // Set the appropriate render sizes and rotational transforms
    // Create a new video composition
    mutableVideoComposition = [AVMutableVideoComposition videoComposition];
    mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);
    mutableVideoComposition.frameDuration = CMTimeMake(1, 30);

    // The rotate transform is set on a layer instruction
    instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mutableComposition duration]);
    layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:(mutableComposition.tracks)[0]];
    [layerInstruction setTransform:t2 atTime:kCMTimeZero];


    // Step 4
    // Add the transform instructions to the video composition
    instruction.layerInstructions = @[layerInstruction];
    mutableVideoComposition.instructions = @[instruction];

    TT_RELEASE_SAFELY(_mutableComposition);
    _mutableComposition = [mutableComposition retain];
    TT_RELEASE_SAFELY(_mutableVideoComposition);
    _mutableVideoComposition = [mutableVideoComposition retain];
}

我从 AVSERotateCommand 这个链接中提取了该方法。有人能否建议为什么这个方法不能成功地将我的视频旋转90度?


我遇到了与您相同的问题,并且下面提供的建议答案(例如更改为AVAssetExportPresetMediumQuality)并不能解决问题。我也尝试在layerInstruction上添加setOpacity:0,但似乎在导出电影时该指令被忽略。 - Christian A. Strømmen
我也有同样的问题,你找到解决方案了吗? - user3306145
1个回答

6

由于您使用了AVAssetExportPresetPassthroughAVAssetExportSession将忽略videoComposition,请使用其他任何预设。


这似乎不再有任何区别了。 - Christian A. Strømmen
你是在模拟器上尝试吗?请在设备上尝试。 - Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接