天天看點

視訊 裁剪 合并 ios

聲明:此文我是根據

<a href="http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+%7C+iPhone+Developer+and+Gamer)" target="_blank">http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+|+iPhone+Developer+and+Gamer)</a>

這個編寫出來的。

原文是合并視訊.而我的項目要求是裁剪視訊,是以我跟他的代碼實作了自己的需求而已。

目前這塊代碼應該是能給視訊裡面添加logo之類的東西。但是我目前沒實作,網上的代碼我也暫時沒找到。

- (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath {

   NSLog(@"nv_strVideoPath = %@ nv_strSavePath = %@n ",v_strVideoPath,v_strSavePath);

    AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]];

    CMTime assetTime = [avAsset duration];

    Float64 duration = CMTimeGetSeconds(assetTime);

    NSLog(@"視訊時長 %fn",duration);

    AVMutableComposition *avMutableComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *avMutableCompositionTrack = [avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    NSError *error = nil;

    // 這塊是裁剪,rangtime .前面的是開始時間,後面是裁剪多長 (我這裁剪的是從第二秒開始裁剪,裁剪2.55秒時長.)

    [avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))

                                       ofTrack:avAssetTrack

                                        atTime:kCMTimeZero

                                         error:&amp;error];

    AVMutableVideoComposition *avMutableVideoComposition = [[AVMutableVideoComposition videoComposition] retain];

// 這個視訊大小可以由你自己設定。比如源視訊640*480.而你這320*480.最後出來的是320*480這麼大的,640多出來的部分就沒有了。并非是把圖檔壓縮成那麼大了。

    avMutableVideoComposition.renderSize = CGSizeMake(320.0f, 480.0f);

    avMutableVideoComposition.frameDuration = CMTimeMake(1, 30); 

// 這句話暫時不用理會,我正在看是否能添加logo而已。 

    [self addDataToVideoByTool:avMutableVideoComposition.animationTool];

    AVMutableVideoCompositionInstruction *avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];

    AVMutableVideoCompositionLayerInstruction *avMutableVideoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:avAssetTrack];

    [avMutableVideoCompositionLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];

    avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositionLayerInstruction];

    avMutableVideoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];

    NSFileManager *fm = [[NSFileManager alloc] init];

    if ([fm fileExistsAtPath:v_strSavePath]) {

        NSLog(@"video is have. then delete that");

        if ([fm removeItemAtPath:v_strSavePath error:&amp;error]) {

            NSLog(@"delete is ok");

        }else {

            NSLog(@"delete is no error = %@",error.description);

        }

    }

    [fm release];

    AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x480];

    [avAssetExportSession setVideoComposition:avMutableVideoComposition];

    [avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];

    [avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie];

    [avAssetExportSession setShouldOptimizeForNetworkUse:YES];

    [avAssetExportSession exportAsynchronouslyWithCompletionHandler:^(void){

        switch (avAssetExportSession.status) {

            case AVAssetExportSessionStatusFailed:

                NSLog(@"exporting failed %@",[avAssetExportSession error]);

                break;

            case AVAssetExportSessionStatusCompleted:

                NSLog(@"exporting completed");

                // 想做什麼事情在這個做

                [self cutImageByVideoPath:v_strSavePath];

            case AVAssetExportSessionStatusCancelled:

                NSLog(@"export cancelled");

    }];

    if (avAssetExportSession.status != AVAssetExportSessionStatusCompleted){

        NSLog(@"Retry export");

    [avMutableVideoComposition release];

     [avAssetExportSession release];

}

本文轉自 卓行天下  51CTO部落格,原文連結:http://blog.51cto.com/9951038/1772553,如需轉載請自行聯系原作者

繼續閱讀