缩短/编辑以固定时间记录为AVCaptureMovieFileOutput的视频

时间:2013-08-15 22:06:05

标签: ios cocoa-touch avfoundation avcapturesession video-editing

在我的应用中,我正在使用设备的相机录制视频,并AVCaptureSession进行保存。长话短说,我需要能够删除此录制剪辑的第一 x秒。我不希望向用户显示“编辑视频”视图,并且每次发言并不是“固定”的时间量,但在一天结束时,我留在{{1}一个值,我必须从剪辑的开头切出多少。我一直在看CMTime等,但没有运气。对于播放,我猜AVAssetWriter会这样做,但我需要将实际视频的持续时间缩短[player seekToTime:time];,从头开始剪切。什么方法,或者我在哪里可以得到这方面的文档?

1 个答案:

答案 0 :(得分:0)

您是否尝试过AVMutableComposition?它会有处理时间。类似的东西:

// get your asset
AVAsset *asset = [AVAsset assetWithURL:yourURL];

// get asset tracks
AVAssetTrack *assetTrackVideo = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVAssetTrack *assetTrackAudio = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

// create composition
AVMutableComposition *composition = [AVMutableComposition composition];

AVMutableCompositionTrack *trackVideo = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *trackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// YOUR_DURATION is something like CMTimeSubstruct(asset.duration, YOURTIME_START);
[trackVideo insertTimeRange:CMTimeRangeMake(YOURTIME_START, YOUR_DURATION)
                    ofTrack:assetTrackVideo
                     atTime:kCMTimeZero
                      error:nil];

[trackAudio insertTimeRange:CMTimeRangeMake(YOURTIME_START, YOUR_DURATION)
                    ofTrack:assetTrackAudio
                     atTime:kCMTimeZero
                      error:nil];

// do the orientation change if needed

NSString* filename = [NSString stringWithFormat:@"videoFileName-%d.mov",arc4random() % 1000];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

NSURL *exporterURL = [NSURL fileURLWithPath:path];

// Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = exporterURL;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{

        // NSLog(@"Finished Output composition with error '%@' reason '%@'",   exporter.error.localizedDescription,exporter.error.localizedFailureReason);
    }];