【问题标题】:AVVideoComposition FailureAVVideoComposition 失败
【发布时间】:2011-08-17 05:46:39
【问题描述】:

和我一样。使用它。

我将几个不同的视频剪辑串成一个 AVMutableComposition 并尝试在需要时纠正它们的方向。

这是我的代码:

composition = [[AVMutableComposition alloc] init];
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

CMTime nextClipStartTime = kCMTimeZero;

// orientation compensation vars
AVMutableVideoCompositionInstruction *inst = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
NSMutableArray *compInst = [[NSMutableArray alloc] init];

// get view size
CGSize viewSize = playerView.frame.size;

// generate movie assets
for (NSString* moviePath in [currentBlam valueForKey:@"movies"]) {
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    AVURLAsset *movieAsset = [AVURLAsset URLAssetWithURL:movieURL options:nil];  

    // scale asset to fit screen

    CMTimeRange tr = CMTimeRangeFromTimeToTime(CMTimeMakeWithSeconds(0.0f, 1), CMTimeMakeWithSeconds(0.0f, 1));

    //    create video track
    AVAssetTrack *clipVideoTrack = [[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    //    create audio track
    AVAssetTrack *clipAudioTrack = [[movieAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    tr = CMTimeRangeFromTimeToTime(CMTimeMakeWithSeconds(0.0f, 1), CMTimeMakeWithSeconds(CMTimeGetSeconds([movieAsset duration]), 1));

    AVMutableVideoCompositionLayerInstruction *layerInst;
    layerInst = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];

    int or = [self orientationForTrack:movieAsset];
    if (or==1) { 
        float rot = (0.0f);
        [layerInst setTransform:CGAffineTransformMakeRotation(rot) atTime:nextClipStartTime];
    } else if (or==2) { 
        float rot = (M_PI);
        [layerInst setTransform:CGAffineTransformMakeRotation(rot) atTime:nextClipStartTime];
    } else if (or==3) { 
        float rot = (M_PI*-0.5f);
        [layerInst setTransform:CGAffineTransformMakeRotation(rot) atTime:nextClipStartTime];
    } else if (or==4) { 
        float rot = (M_PI*0.5f);
        [layerInst setTransform:CGAffineTransformMakeRotation(rot) atTime:nextClipStartTime];
    }

    [layerInst setTransform:clipVideoTrack.preferredTransform atTime:nextClipStartTime];
    [compInst addObject:layerInst];

    //    insert video track
    [compositionVideoTrack insertTimeRange:tr 
                                   ofTrack:clipVideoTrack 
                                    atTime:nextClipStartTime 
                                     error:nil];
    //    insert audio track
    [compositionAudioTrack insertTimeRange:tr 
                                   ofTrack:clipAudioTrack 
                                    atTime:nextClipStartTime 
                                     error:nil];

    nextClipStartTime = CMTimeAdd(nextClipStartTime, tr.duration);
}

//set size and duration
composition.naturalSize = viewSize;
videoComposition.frameDuration = composition.duration;
videoComposition.renderSize = viewSize;
videoComposition.renderScale = 1.0f;

//apply instructions
inst.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
inst.layerInstructions = compInst;
videoComposition.instructions = [NSArray arrayWithObject:inst];

playerItem = [[AVPlayerItem alloc] initWithAsset:composition];
playerItem.videoComposition = videoComposition;
[playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(playerItemDidReachEnd:) 
                                             name:AVPlayerItemDidPlayToEndTimeNotification 
                                           object:playerItem];
player = [AVPlayer playerWithPlayerItem:playerItem];

[playerView setPlayer:player];

当我运行它并填充内容时,什么都没有显示。

这在之前没有将 AVVideoComposition 应用于播放器的情况下工作。事实上,注释掉 playerItem.videoComposition = videoComposition 允许它工作,尽管没有更正资产旋转。

在这一点上,我知道我只是在这里误解了一些东西。有人能指出什么吗?

【问题讨论】:

  • 我会跟进并说我知道视频仍然可以播放。声音播放和擦洗滑块正确推进。我只是在视频中没有看到任何内容。
  • 有没有想过这个问题?

标签: iphone avfoundation avvideocomposition


【解决方案1】:

您是否考虑过视频旋转的点可能不是中心并且视频正在从屏幕上绘制出来?这可能完全是错误的,但这只是我的第一个想法。

【讨论】:

    【解决方案2】:

    我认为您将每个单帧设置为整个剪辑的长度:

    videoComposition.frameDuration = composition.duration;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多