【问题标题】:Creating `CMSampleBufferRef` from a .mov file从 .mov 文件创建 `CMSampleBufferRef`
【发布时间】:2015-07-12 09:23:52
【问题描述】:

我的应用程序捕获了一个 3 秒的视频剪辑,并且我想以编程方式通过循环 5 次从录制的 3 秒剪辑创建一个 15 秒剪辑。最后必须在 CameraRoll 中保存 15 秒的剪辑。

我通过AVCaptureMovieFileOutput 获得了我的 3 秒视频剪辑,并且我从代表那里获得了它的 NSURL,目前在 NSTemporaryDirectory()

我正在使用AVAssetWriterInput 循环它。但它要求CMSampleBufferRef 像:

[writerInput appendSampleBuffer:sampleBuffer];

我将如何从 NSTemporaryDirectory() 的视频中获取此 CMSampleBufferRef

我已经看到将UIImage 转换为CMSampleBufferRef 的代码,但我可以找到任何用于视频文件的代码。

任何建议都会有所帮助。 :)

【问题讨论】:

    标签: ios ios8 video-capture cmsamplebufferref


    【解决方案1】:

    最后,我使用AVMutableComposition 解决了我的问题。这是我的代码:

    AVMutableComposition *mixComposition = [AVMutableComposition new];
    AVMutableCompositionTrack *mutableCompVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    
    AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:3SecFileURL options:nil];
    CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);
    
    CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
    [mutableCompVideoTrack setPreferredTransform:rotationTransform];
    
    CMTime currentCMTime = kCMTimeZero;
    
    for (NSInteger count = 0 ; count < 5 ; count++)
    {
        [mutableCompVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:currentCMTime error:nil];
        currentCMTime = CMTimeAdd(currentCMTime, [videoAsset duration]);
    }
    
    NSString *fullMoviePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"moviefull" stringByAppendingPathExtension:@"mov"]];
    NSURL *finalVideoFileURL = [NSURL fileURLWithPath:fullMoviePath];
    
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
    [exportSession setOutputFileType:AVFileTypeQuickTimeMovie];
    [exportSession setOutputURL:finalVideoFileURL];
    
    CMTimeValue val = [mixComposition duration].value;
    CMTime start = CMTimeMake(0, 1);
    CMTime duration = CMTimeMake(val, 1);
    CMTimeRange range = CMTimeRangeMake(start, duration);
    [exportSession setTimeRange:range];
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
    
        switch ([exportSession status])
        {
            case AVAssetExportSessionStatusFailed:
            {
                NSLog(@"Export failed: %@ %@", [[exportSession error] localizedDescription], [[exportSession error]debugDescription]);
            }
            case AVAssetExportSessionStatusCancelled:
            {
                NSLog(@"Export canceled");
                break;
            }
            case AVAssetExportSessionStatusCompleted:
            {
                NSLog(@"Export complete!");
            }
    
            default:    NSLog(@"default");
        }
    }];
    

    【讨论】:

      【解决方案2】:

      看看AVAssetReader,它可以返回一个CMSampleBufferRef。请记住,您需要为自己的工作方法操作时间戳。

      【讨论】:

        猜你喜欢
        • 2018-08-13
        • 2020-02-10
        • 2016-03-30
        • 1970-01-01
        • 2011-03-19
        • 2012-12-20
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多