2014-01-23 88 views
2

我正在爲視頻添加疊加層,並在Facebook上分享以及使用哪種方式。但視頻的質量變得非常差,特別是在whatsapp,而其他沒有覆蓋的質量最好。編碼覆蓋覆蓋視頻的代碼:在視頻上添加疊加

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:outputFileURL options:nil]; 
AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
AVMutableCompositionTrack *audioComposition = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
AVAssetTrack *sourceAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
           ofTrack:clipVideoTrack 
           atTime:kCMTimeZero error:nil]; 
[audioComposition insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:sourceAudioTrack atTime:kCMTimeZero error:nil]; //[asset duration] 

[compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]); 
AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 

//*****************// 
UIImageOrientation videoAssetOrientation_;// = UIImageOrientationUp; 
BOOL isVideoAssetPortrait_ = NO; 
CGAffineTransform videoTransform = clipVideoTrack.preferredTransform; 

if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) { 
    videoAssetOrientation_ = UIImageOrientationRight; 
    isVideoAssetPortrait_ = YES; 
} 
if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) { 
    videoAssetOrientation_ = UIImageOrientationLeft; 
    isVideoAssetPortrait_ = YES; 
} 
if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) { 
    videoAssetOrientation_ = UIImageOrientationUp; 
    isVideoAssetPortrait_ = NO; 
} 
if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) { 
    videoAssetOrientation_ = UIImageOrientationDown; 
    isVideoAssetPortrait_ = NO; 
} 
[layerInstruction setTransform:clipVideoTrack.preferredTransform atTime:kCMTimeZero]; 
[layerInstruction setOpacity:0.0 atTime:videoAsset.duration]; 
//*****************// 
CGSize naturalSize; 
if(isVideoAssetPortrait_){ 
    naturalSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.width); 
} else { 
    naturalSize = clipVideoTrack.naturalSize; 
} 


UIImage *myImage = [UIImage imageNamed:@"overlay.png"]; 
CALayer *aLayer = [CALayer layer]; 
if (naturalSize.width > 359) { 
    aLayer.frame = CGRectMake((naturalSize.width-340)/2, 0, 340, 90); 

} 
else if (naturalSize.width > 200) { 
    aLayer.frame = CGRectMake(10, 0, naturalSize.width-20, 65); 
} 
else{ 
    aLayer.frame = CGRectMake(15, 0, naturalSize.width-30, 60); 

} 
aLayer.contents = (id)myImage.CGImage; 
//Needed for proper display. We are using the app icon (57x57). If you use 0,0 you will not see it 
// aLayer.opacity = 1.0; //Feel free to alter the alpha here 

aLayer.backgroundColor = [UIColor clearColor].CGColor; 
[aLayer setMasksToBounds:YES]; 
// CGSize videoSize = [videoAsset naturalSize]; 
CALayer *parentLayer = [CALayer layer]; 
CALayer *videoLayer = [CALayer layer]; 


//NSURL *url = [NSURL fileURLWithPath:[urlsOfVideos objectAtIndex:self.pageControl.currentPage]]; 

NSLog(@"videoSize ++++: %@", NSStringFromCGSize(naturalSize)); 
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition] ; 

float renderWidth, renderHeight; 
renderWidth = naturalSize.width; 
renderHeight = naturalSize.height; 
videoComp.renderSize = CGSizeMake(renderWidth, renderHeight); 
videoComp.instructions = [NSArray arrayWithObject:instruction]; 
videoComp.frameDuration = CMTimeMake(1, 30); 


videoLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height); 
parentLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height); 
[parentLayer addSublayer:videoLayer]; 
[parentLayer addSublayer:aLayer]; 

videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; 

/// instruction 

instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; 
videoComp.instructions = [NSArray arrayWithObject: instruction]; 

assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 

assetExport.outputURL = exportUrl; 
assetExport.outputFileType = AVFileTypeQuickTimeMovie; 
assetExport.shouldOptimizeForNetworkUse = YES; 
assetExport.videoComposition = videoComp; 

//[strRecordedFilename setString: exportPath]; 

[assetExport exportAsynchronouslyWithCompletionHandler: 
^(void) { 
    switch (assetExport.status) { 
     case AVAssetExportSessionStatusFailed:{ 
      NSLog(@"Fail"); 
      NSLog(@"asset export fail error : %@", assetExport.error); 

      dispatch_async(dispatch_get_main_queue(),^
          { 

          }); 

      break; 
     } 
     case AVAssetExportSessionStatusCompleted:{ 
      NSLog(@"Success"); 

           dispatch_async(dispatch_get_main_queue(),^
          { 

          }); 


      break; 
     } 

     default: 
      break; 
    } 

}  
]; 
} 

我在做什麼錯了?任何想法?

回答

1

使用下面的代碼在視頻中添加疊加:

CALayer *overlayLayer = [CALayer layer]; 
UIImage *overlayImage = [UIImage imageNamed:@"overlay.png"]; 
[overlayLayer setContents:(id)[overlayImage CGImage]]; 
overlayLayer.frame = CGRectMake(0, 0, size.width, size.height); 
[overlayLayer setMasksToBounds:YES]; 

//Set Up the Parent Layer 
CALayer *parentLayer = [CALayer layer]; 
CALayer *videoLayer = [CALayer layer]; 
parentLayer.frame = CGRectMake(0, 0, size.width, size.height); 
videoLayer.frame = CGRectMake(0, 0, size.width, size.height); 
[parentLayer addSublayer:videoLayer]; 
[parentLayer addSublayer:overlayLayer]; 

看一看下面的鏈接containig很好的教程:

http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

+0

我也一樣。請檢查 –

+0

您是否瀏覽了我提供的鏈接? –

+0

是的。並下載項目並分享視頻,但同樣的事情發生(即視頻質量差)。 –