【问题标题】:Shorten/edit video recorded as AVCaptureMovieFileOutput with fixed time以固定时间缩短/编辑录制为 AVCaptureMovieFileOutput 的视频
【发布时间】:2013-08-18 05:35:27
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您尝试过 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);
        }];
    

    【讨论】:

    • 谢谢。虽然不需要所有的轨道等,只是资产和 AVAssetExportSession,它有一个变量timeRange,我可以轻松输入开始和停止位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多