热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用AVMutableComposition合并视频时视频破解

如何解决《使用AVMutableComposition合并视频时视频破解》经验,为你挑选了1个好方法。

我正在使用AVMutableComposition将视频与下面的代码合并,

- (void)MergeAndSave_internal{

    AVMutableComposition *composition = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositiOnVideoTrack= [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositiOnAudioTrack= [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];

    NSLog(@"%@",videoPathArray);

    float time = 0;
    CMTime startTime = kCMTimeZero;

    for (int i = 0; i size.height) {

            [layerInstruction setTransform:transform atTime:CMTimeMakeWithSeconds(time, 30)];
        } else {


            float s = size.width/size.height;


            CGAffineTransform newe = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(s,s));

            float x = (size.height - size.width*s)/2;

            CGAffineTransform newer = CGAffineTransformConcat(newe, CGAffineTransformMakeTranslation(x, 0));

            [layerInstruction setTransform:newer atTime:CMTimeMakeWithSeconds(time, 30)];
        }
        if(i==0){
             [compositionVideoTrack setPreferredTransform:sourceVideoTrack.preferredTransform];
        }
        ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceVideoTrack atTime:startTime error:&error];


        ok = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceAudioTrack atTime:startTime error:nil];

        if (!ok) {
            {
                [radialView4 setHidden:YES];
                NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Something Went Wrong :("  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
                [alert show];
                [radialView4 setHidden:YES];
                break;
            }

        }

        startTime = CMTimeAdd(startTime, [sourceAsset duration]);

    }


    instruction.layerInstructiOns= [NSArray arrayWithObject:layerInstruction];
    instruction.timeRange = compositionVideoTrack.timeRange;

    videoComposition.instructiOns= [NSArray arrayWithObject:instruction];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"RampMergedVideo.mov"]];
    unlink([myPathDocs UTF8String]);
    NSURL *url = [NSURL fileURLWithPath:myPathDocs];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition
                                                                      presetName:AVAssetExportPreset1280x720];
    exporter.outputURL=url;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.shouldOptimizeForNetworkUse = YES;
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{

            switch ([exporter status]) {
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"Export failed: %@", [exporter error]);
                    break;
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"Export canceled");
                    break;
                case AVAssetExportSessionStatusCompleted:{
                    NSLog(@"Export successfully");

                }
                default:
                    break;
            }
            if (exporter.status != AVAssetExportSessionStatusCompleted){
                NSLog(@"Retry export");

            }

        });
    }];

}

但视频在保存到系统并在快速播放器中播放时看起来很破旧.我认为CFAffline转换的问题.有人可以请教吗?

这是视频中间的破解屏幕: 在此输入图像描述 在此输入图像描述



1> Sumeet..:

您还没有将videoComposition设置为AVAssetExportSession.试试这个exporter.videoComposition = videoComposition;.虽然尝试了这个但是应该工作.


推荐阅读
author-avatar
小Reve_942
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有