2011-08-17 4 views
2

鉱山の場合と同じです。それを使用する。AVVideoComposition失敗

私はいくつかの異なるビデオクリップを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をコメントアウトすると、資産の回転を修正することなく動作することができます。

この時点で、私はここで何かを誤解していることを知っています。誰かが何を指摘できますか?

+0

私はフォローアップし、ビデオがまだ再生できることを知っていると言います。サウンドが再生され、スクラブスライダが適切に前進します。私はビデオで何も見ていないだけです。 – Lane

+0

これを解明したことがありますか? – scttnlsn

答えて

2

動画が回転している点が中心でなく、動画が画面から外れている可能性がありますか?これはまったく間違っているかもしれませんが、ちょうど私の最初の考えです。

1

は、私はあなたがクリップ全体の長さにそれぞれ単一のフレームを設定していると思う:

videoComposition.frameDuration = composition.duration。