2014-12-09 24 views
4

我的應用程序合併了兩個視頻。合併兩個視頻時iOS8中的問題

我使用下面的代碼使用AVVideoComposition

- (void)buildSequenceComposition:(AVMutableComposition *)mixComposition andVideoComposition:(AVMutableVideoComposition *)videoComposition withAudioMix:(AVMutableAudioMix *)audioMix 
{ 
    CMTime nextClipStartTime = kCMTimeZero; 
    NSInteger i; 

    // No transitions: place clips into one video track and one audio track in composition. 
    AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
    NSMutableArray*arrLayerInstruction = [NSMutableArray array]; 

    for (i = 0; i < [_clips count]; i++) 
    { 
     AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
     AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
     AVURLAsset *asset = [[_clips objectAtIndex:i] objectForKey:@"videoURL"]; 

     CMTimeRange timeRangeInAsset; 

     timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [asset duration]); 

     AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
     NSError*err = nil; 
     [compositionVideoTrack insertTimeRange:timeRangeInAsset ofTrack:clipVideoTrack atTime:nextClipStartTime error:&err]; 

     if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] != 0) 
     { 
      AVAssetTrack *clipAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

      [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:nil]; 


      AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]]; 
      [exportAudioMixInputParameters setVolume:[[[_clips objectAtIndex:i] objectForKey:@"videoSoundLevel"] floatValue] atTime:nextClipStartTime]; 
      exportAudioMixInputParameters.trackID = compositionAudioTrack.trackID; 
      audioMix.inputParameters=[NSArray arrayWithObject:exportAudioMixInputParameters]; 

     } 
     //FIXING ORIENTATION// 
     AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; 

     UIImageOrientation FirstAssetOrientation_ = UIImageOrientationUp; 
     BOOL isFirstAssetPortrait_ = NO; 
     CGAffineTransform firstTransform = clipVideoTrack.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; 
     } 

     CGFloat tHeight = [clipVideoTrack naturalSize].height; 
     CGFloat tWidth = [clipVideoTrack naturalSize].width; 

     if(isFirstAssetPortrait_) 
     { 
      tHeight = [clipVideoTrack naturalSize].height; 
      tWidth = [clipVideoTrack naturalSize].width; 
      CGFloat temp = tHeight; 
      tHeight = tWidth; 
      tWidth = temp; 

     } 

     CGFloat FirstAssetScaleToFitRatioWidth = [mixComposition naturalSize].width/tWidth; 
     CGFloat FirstAssetScaleToFitRatioHeight = [mixComposition naturalSize].height/tHeight; 


     CGFloat FirstAssetScaleToFitRatio = FirstAssetScaleToFitRatioWidth>FirstAssetScaleToFitRatioHeight?FirstAssetScaleToFitRatioHeight:FirstAssetScaleToFitRatioWidth; 
     CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio); 
     CGSize naturalSize = CGSizeApplyAffineTransform(CGSizeMake(tWidth, tHeight), FirstAssetScaleFactor); 

     CGAffineTransform transform = CGAffineTransformIdentity; 

     CGSize translateSize = CGSizeMake(0, 0); 

     if (FirstAssetScaleToFitRatioWidth<FirstAssetScaleToFitRatioHeight) 
     { 
      transform = CGAffineTransformMakeTranslation(0, ([mixComposition naturalSize].height-naturalSize.height)/2); 
      translateSize.height = ([mixComposition naturalSize].height-naturalSize.height)/2; 
     } 
     else if (FirstAssetScaleToFitRatioWidth==FirstAssetScaleToFitRatioHeight) 
     { 

     } 
     else 
     { 
      transform = CGAffineTransformMakeTranslation(([mixComposition naturalSize].width-naturalSize.width)/2, 0); 
      translateSize.width = ([mixComposition naturalSize].width-naturalSize.width)/2; 
     } 

     [FirstlayerInstruction setTransform:CGAffineTransformConcat(CGAffineTransformConcat(clipVideoTrack.preferredTransform, FirstAssetScaleFactor),transform) atTime:kCMTimeZero]; 

     [FirstlayerInstruction setOpacity:0.0 atTime:CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration)]; 
     [FirstlayerInstruction setOpacity:1.0 atTime:nextClipStartTime]; 

     [arrLayerInstruction addObject:FirstlayerInstruction]; 
     nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration); 
    } 
    MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, nextClipStartTime); 
    MainInstruction.layerInstructions = arrLayerInstruction;; 
    videoComposition.instructions = [NSArray arrayWithObject:MainInstruction]; 
} 

雖然它正常工作與iOS7合併兩個視頻,而在iOS8上的出口與AVVideoCompositon視頻,它給了我以下錯誤:

Title :Error Domain=AVFoundationErrorDomain Code=-11821 「Cannot Decode」 { NSLocalizedFailureReason=The media data could not be decoded. It may be damaged.} 

它適用於iOS7以及iOS之前的其他版本,但不適用於iOS8。

我也嘗試了蘋果公司從AVSampleEditor的示例代碼,它也給我在iOS8中導出視頻時出現同樣的錯誤。

請幫我解決問題。謝謝。

+0

您是否檢查此鏈接? http://stackoverflow.com/questions/26209099/error-domain-avfoundationerrordomain-code-11821-cannot-decode – NiravPatel 2014-12-09 11:53:24

+0

@NiravPatel,是的我試過了,但仍面臨同樣的錯誤。 – HarshIT 2014-12-09 11:55:48

+0

你正在使用iOS8的設備?不要嘗試模擬器。 – 2014-12-09 11:56:19

回答

相關問題