gpt4 book ai didi

ios - 使用AVCaptureSession录制视频不会保存任何视频

转载 作者:行者123 更新时间:2023-12-01 16:36:13 25 4
gpt4 key购买 nike

我正在尝试录制视频,并使用AVCaptureSession将其保存到文件中。我使用单例来处理捕获 session ,因为我的应用程序中有多个VC在其中显示视频预览。

视频预览可以正确显示,但是当我尝试录制视频时,一切似乎都可以正常工作,但是当我查看应用程序容器时,我没有收到任何错误,没有保存任何视频文件。

这是我的捕获 session 代码中的相关功能

-(void)recordVideoSetup{
// Code adapted from
// www.ios-developer.net/iphone-ipad-programmer/development/camera/record-video-with-avcapturesession-2

NSLog(@"Adding Movie File Output");

MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

Float64 TotalSeconds = 600; // 10 minutes max
int32_t preferredTimeScale = 30; //fps
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale); // Set Max Duration

MovieFileOutput.maxRecordedDuration = maxDuration;
MovieFileOutput.minFreeDiskSpaceLimit = 1024*1024*10; // 10 MB

if ([captureSession canAddOutput:MovieFileOutput]) {
NSLog(@"Added output to capture session");
[captureSession addOutput:MovieFileOutput];
}


AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];

// SET LANDSCAPE ORIENTATION
if ([CaptureConnection isVideoOrientationSupported]) {
AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight;
[CaptureConnection setVideoOrientation:orientation];
}


NSLog(@"Setting image quality");
[captureSession setSessionPreset:AVCaptureSessionPresetMedium];
if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480])
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];

}

-(NSURL*)startRecordingVideo:(NSString *)surveyID {
DDLogCInfo(@"Video Recording Started");

// Get URL to record to

NSString *documentsDirPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
documentsDirPath = [documentsDirPath stringByAppendingPathComponent:@"video"];
NSURL *documentsDirUrl = [NSURL fileURLWithPath:documentsDirPath isDirectory:YES];
NSString *filename = [NSString stringWithFormat:@"%@.mp4", surveyID];
NSURL *url = [NSURL URLWithString:filename relativeToURL:documentsDirUrl];

DDLogCInfo(@"Path to video: %@", url.path);

NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:[documentsDirPath stringByAppendingString:filename]]) {
NSError *error;
if ([filemanager removeItemAtURL:url error:&error] == NO) {
DDLogCError(@"Could not record video");
return nil;
}
}

[MovieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];

return url;
}

-(void)stopRecordingVideo {
DDLogCInfo(@"Video Recording Ended");
[MovieFileOutput stopRecording];

}


-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {

// Check if Recording was successfull

BOOL recordedSuccessfully = YES;

if ([error code] != noErr) {
NSLog(@"A Problem occurred with recording");

// A Problem Occurred
id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
if (value) {
recordedSuccessfully = [value boolValue];
}
}

if (!recordedSuccessfully) {
DDLogCInfo(@"!!! - VIDEO RECORDING ERROR");
}

}

编辑:一些其他信息
这是我得到的日志输出:
SafeCurveSpeed[9060:3691230] Adding Movie File Output
SafeCurveSpeed[9060:3691230] Added output to capture session
SafeCurveSpeed[9060:3691230] Setting image quality
SafeCurveSpeed[9060:807] Video Recording Started
SafeCurveSpeed[9060:807] Path to video: "/var/mobile/Containers/Data/Application/44A52ADB-0E36-4303-9DE3-99E9CCCAF3FC/Documents/video/survey-video-10.mp4"
SafeCurveSpeed[9060:807] Video Recording Ended

编辑2:
我添加了一种方法来观察“AVCaptureSessionRuntimeErrorNotification”通知,但在“记录”期间未触发任何通知。我还在 captureOutput didFinishRecordingToOutputFileAtURL:方法的顶部添加了一个日志调用,发现在录制完成后它根本没有到达该方法。虽然仍然不确定原因。

最佳答案

之后添加此代码
视频路径:

UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

在VC中添加此方法
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo {
if (error == nil) {
[[[UIAlertView alloc] initWithTitle:@"Saved to camera roll" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {
[[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}

使用此第三方库

https://www.cocoacontrols.com/controls/screcorder

https://github.com/rFlex/SCRecorder

http://www.ios-developer.net/iphone-ipad-programmer/development/camera/record-video-with-avcapturesession-2

关于ios - 使用AVCaptureSession录制视频不会保存任何视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28037252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com