1

在我的应用程序中,我正在使用设备的相机录制视频并AVCaptureSession保存。长话短说,我需要能够剪掉这个录制剪辑的 x 秒。我不希望向用户呈现“编辑视频”视图,这不是每次说的“固定”时间量,但归根结底,我留在这里CMTime的值为我必须从剪辑的开头剪掉多少。我一直在看AVAssetWriter等,但没有运气。对于播放,我想[player seekToTime:time];可以,但我需要实际视频的time持续时间更短,从一开始就剪掉。什么方法,或者我在哪里可以获得相关文档?

4

1 回答 1

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);
    }];
于 2013-08-16T01:51:10.343 回答