我需要能够同时拥有AVCaptureVideoDataOutput和AVCaptureMovieFileOutput工作.以下代码有效,但视频录制没有.在didFinishRecordingToOutputFileAtURL直接后调用委托startRecordingToOutputFileURL被调用.现在,如果我AVCaptureVideoDataOutput从
AVCaptureSession简单的评论中删除该行:
[captureSession addOutput:captureDataOutput];
视频录制工作,但然后不调用SampleBufferDelegate(我需要).
我怎样才能兼顾AVCaptureVideoDataOutput并AVCaptureMovieFileOutput同时工作.
- (void)initCapture {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:NULL];
captureDataOutput = [[AVCaptureVideoDataOutput alloc] init];
[captureDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
m_captureFileOutput = [[AVCaptureMovieFileOutput alloc] init];
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureDataOutput setVideoSettings:videoSettings];
captureSession = [[AVCaptureSession alloc] init];
[captureSession addInput:captureInput];
[captureSession addOutput:m_captureFileOutput];
[captureSession addOutput:captureDataOutput];
[captureSession beginConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetLow];
[captureSession commitConfiguration];
[self performSelector:@selector(startRecording) …Run Code Online (Sandbox Code Playgroud) 我AVCaptureMovieFileOutput用来录制一些视频.我显示了预览图层,使用AVLayerVideoGravityResizeAspectFill它稍微放大.我遇到的问题是最终视频较大,包含在预览期间不适合屏幕的额外图像.
这是预览和结果视频

有没有办法可以指定CGRect我想要从视频中删除的内容AVAssetExportSession?
编辑----
当我应用它CGAffineTransformScale时,AVAssetTrack它放大到视频,并AVMutableVideoComposition renderSize设置为view.bounds它裁剪结束.太棒了,还剩下一个问题.视频的宽度不会拉伸到正确的宽度,只是填充黑色.
编辑2 ----建议的问题/答案是不完整的..
我的一些代码:
在我的- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error方法中,我有这个来裁剪和调整视频大小.
- (void)flipAndSave:(NSURL *)videoURL withCompletionBlock:(void(^)(NSURL *returnURL))completionBlock
{
AVURLAsset *firstAsset = [AVURLAsset assetWithURL:videoURL];
// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
// 2 - Video track
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我允许用户以纵向模式录制视频.我在应用程序中使用它,这很好.但是,我正在实施一项功能,在Instagram上分享视频,视频必须是方形的.
将视频裁剪为正方形,比如说中心部分,是一个选项,但是由于用户在拍摄视频时没有考虑到这一点,因此通常会发现它很糟糕.
因此,我想导出视频方块,但具有宽高比和左右两个条.最好的是,有没有办法定义背景的颜色(通过代码或通过UIImage).
我能找到的最接近的是这个,但我无法充分利用它,因为我不知道如何设置转换.
示例(红色框仅用于标记图像边界):
