簡體   English   中英

從.mov文件創建`CMSampleBufferRef`

[英]Creating `CMSampleBufferRef` from a .mov file

我的應用程序捕獲視頻剪輯3秒,並以編程方式我想通過循環5次來記錄3秒剪輯創建15秒剪輯。 最后必須在CameraRoll中保存15秒的剪輯。

我通過AVCaptureMovieFileOutput獲得了我的3秒視頻剪輯,我有來自委托的NSURL ,目前在NSTemporaryDirectory()

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

[writerInput appendSampleBuffer:sampleBuffer];

我如何從NSTemporaryDirectory()中的視頻中獲取此CMSampleBufferRef

我見過將UIImage轉換為CMSampleBufferRef代碼,但我可以找到任何視頻文件。

任何建議都會有所幫助。 :)

最后,我使用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");
    }
}];

看看AVAssetReader,它可以返回一個CMSampleBufferRef。 請記住,您需要操縱工作方法的時間戳。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM