导出后视频丢失

时间:2017-12-28 12:20:38

标签: ios swift xcode avplayer

我面临非常奇怪的声音丢失问题。

我正致力于在视频上添加文字

问题是当我进入我的视频屏幕时,最初视频处于静音模式,当我添加文本并制作视频时,添加了文本图层的新视频没有声音:(

我也尝试过解决方案但不起作用

当视频取消静音时,如果我在那时添加文字,一切正常,但会发出静音声音

当用户点击保存时,我也试过我取消播放器的声音并暂停视频和显示指示器以显示视频正在处理但仍然导出视频而没有声音

仅在播放器声音最初取消静音时才有效。

问题是我不想让视频最初处于静音状态,当文本视频突然处理时,视频会播放声音。

这里是我正在使用的代码

- (void)videoOutput : (NSURL *)videoURL : (CGRect )frame : 
 (UITextView *)videoCaption :(UIImage *)image
 {
     AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL 
     options:nil];

    // 4 - Get path
     NSArray *paths1 = 
     NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *myPathDocs1 =  [documentsDirectory1 stringByAppendingPathComponent:
                          [NSString stringWithFormat:@"FinalVideo.mp4"]];
NSURL *url1 = [NSURL fileURLWithPath:myPathDocs1];

if([[NSFileManager defaultManager] fileExistsAtPath:myPathDocs1])
{
    @try
    {
        [[NSFileManager defaultManager] removeItemAtPath:myPathDocs1 error:nil];
    }
    @catch (NSException *exception)
    {
        NSLog(@"ERRRRORRRRRRR %@",@"------------------------------------------------------------------------------------------------");
    }
    @finally
    {
    }
}

// 1 - Early exit if there's no video file selected
if (!videoAsset) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Load a Video Asset First"
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    return;
}

// 2 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];


// 3 - Video track
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration)
                    ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                     atTime:kCMTimeZero error:nil];

// 3.1 - Create AVMutableVideoCompositionInstruction
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);

// 3.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
UIImageOrientation videoAssetOrientation_  = UIImageOrientationUp;
BOOL isVideoAssetPortrait_  = NO;
CGAffineTransform videoTransform = videoAssetTrack.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;
}
if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {
    videoAssetOrientation_ = UIImageOrientationDown;
}
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
[videolayerInstruction setOpacity:0.0 atTime:videoAsset.duration];

// 3.3 - Add instructions
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];

CGSize naturalSize;
if(isVideoAssetPortrait_){
    naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);
} else {
    naturalSize = videoAssetTrack.naturalSize;
}

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

[self applyVideoEffectsToComposition:mainCompositionInst size:naturalSize :frame :videoCaption];

// 4 - Get path
  NSArray *paths = 
  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
   NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory 
    stringByAppendingPathComponent:
                         [NSString stringWithFormat:@"FinalVideo-
  %d.mov",arc4random() % 1000]];
   NSURL *url = [NSURL fileURLWithPath:myPathDocs];

// 5 - Create exporter
    AVAssetExportSession *exporter1 = [[AVAssetExportSession alloc] 
    initWithAsset:mixComposition
    presetName:AVAssetExportPresetHighestQuality];
    exporter1.outputURL=url;
    exporter1.outputFileType = AVFileTypeQuickTimeMovie;
    exporter1.shouldOptimizeForNetworkUse = YES;
    exporter1.videoComposition = mainCompositionInst;
     [exporter1 exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self exportDidFinish1:exporter1];
        });
    }];

}

---这是添加文字的方法------------------------

-(void)addText:(AVMutableVideoComposition *)composition size:
(CGSize)size :(UITextView *)videoCaption : (CGRect)textviewFrame
{

// 1 - Set up the text layer
CGFloat height = textviewFrame.size.height;


CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
[subtitle1Text setFont:@"AvenirNext-Medium"];
[subtitle1Text setFontSize:videoCaption.font.pointSize];
[subtitle1Text setFrame:CGRectMake(0, -10, size.width , height)];
subtitle1Text.wrapped = YES;
[subtitle1Text setString:videoCaption.text];
[subtitle1Text setAlignmentMode:kCAAlignmentCenter];
[subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];

// 2 - The usual overlay
CALayer *overlayLayer = [CALayer layer];
[overlayLayer addSublayer:subtitle1Text];


overlayLayer.frame = CGRectMake(10 , size.height/2 - (height/2),size.width-20  , height );
[overlayLayer setBackgroundColor:[[UIColor clearColor]CGColor ]];
[overlayLayer setMasksToBounds:YES];

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, size.width, size.height);
[parentLayer setBackgroundColor:[[UIColor clearColor]CGColor ]];
videoLayer.frame = CGRectMake(0, 0, size.width, size.height);
[videoLayer setBackgroundColor:[[UIColor clearColor]CGColor ]];

[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:overlayLayer];

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

}

如果有人遇到同样的问题,请帮我解决一下

提前致谢

0 个答案:

没有答案