- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试更改相机 View Front
和 Back
。它运行良好。如果录制视频时没有使用 Pause/Record
翻转> 选项工作正常。但是如果我们Flip Camera View
一次,则不会保存进一步录制的视频,这会导致 AVAssetWriterStatusFailed
-操作无法完成
。谁能帮我找到我哪里出错了?下面是我的代码。
- (void)flipCamera{
NSArray * inputs = _session.inputs;
for ( AVCaptureDeviceInput * INPUT in inputs ) {
AVCaptureDevice * Device = INPUT.device ;
if ( [ Device hasMediaType : AVMediaTypeVideo ] ) {
AVCaptureDevicePosition position = Device . position ; AVCaptureDevice * newCamera = nil ; AVCaptureDeviceInput * newInput = nil ;
if ( position == AVCaptureDevicePositionFront )
newCamera = [ self cameraWithPosition : AVCaptureDevicePositionBack ] ;
else
newCamera = [ self cameraWithPosition : AVCaptureDevicePositionFront ] ; newInput = [ AVCaptureDeviceInput deviceInputWithDevice : newCamera error : nil ] ;
// beginConfiguration ensures that pending changes are not applied immediately
[ _session beginConfiguration ] ;
[ _session removeInput : INPUT ] ;
[ _session addInput : newInput ] ;
// Changes take effect once the outermost commitConfiguration is invoked.
[ _session commitConfiguration ] ;
break ;
}
}
for ( AVCaptureDeviceInput * INPUT in inputs ) {
AVCaptureDevice * Device = INPUT.device ;
if ( [ Device hasMediaType : AVMediaTypeAudio ] ) {
// audio input from default mic
AVCaptureDevice* mic = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput* newInput = [AVCaptureDeviceInput deviceInputWithDevice:mic error:nil];
// [_session addInput:micinput];
// beginConfiguration ensures that pending changes are not applied immediately
[ _session beginConfiguration ] ;
[ _session removeInput : INPUT ] ;
[ _session addInput : newInput ] ;
// Changes take effect once the outermost commitConfiguration is invoked.
[ _session commitConfiguration ] ;
break ;
}
}
}
- ( AVCaptureDevice * ) cameraWithPosition : ( AVCaptureDevicePosition ) position
{
NSArray * Devices = [ AVCaptureDevice devicesWithMediaType : AVMediaTypeVideo ] ;
for ( AVCaptureDevice * Device in Devices )
if ( Device . position == position )
return Device ;
return nil ;
}
- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
BOOL bVideo = YES;
@synchronized(self)
{
if (!self.isCapturing || self.isPaused)
{
return;
}
if (connection != _videoConnection)
{
bVideo = NO;
}
if ((_encoder == nil) && !bVideo)
{
CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sampleBuffer);
[self setAudioFormat:fmt];
NSString* filename = [NSString stringWithFormat:@"capture%d.mp4", _currentFile];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
_encoder = [VideoEncoder encoderForPath:path Height:_cy width:_cx channels:_channels samples:_samplerate];
}
if (_discont)
{
if (bVideo)
{
return;
}
_discont = NO;
// calc adjustment
CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime last = bVideo ? _lastVideo : _lastAudio;
if (last.flags & kCMTimeFlags_Valid)
{
if (_timeOffset.flags & kCMTimeFlags_Valid)
{
pts = CMTimeSubtract(pts, _timeOffset);
}
CMTime offset = CMTimeSubtract(pts, last);
NSLog(@"Setting offset from %s", bVideo?"video": "audio");
NSLog(@"Adding %f to %f (pts %f)", ((double)offset.value)/offset.timescale, ((double)_timeOffset.value)/_timeOffset.timescale, ((double)pts.value/pts.timescale));
// this stops us having to set a scale for _timeOffset before we see the first video time
if (_timeOffset.value == 0)
{
_timeOffset = offset;
}
else
{
_timeOffset = CMTimeAdd(_timeOffset, offset);
}
}
_lastVideo.flags = 0;
_lastAudio.flags = 0;
}
// retain so that we can release either this or modified one
CFRetain(sampleBuffer);
if (_timeOffset.value > 0)
{
CFRelease(sampleBuffer);
sampleBuffer = [self adjustTime:sampleBuffer by:_timeOffset];
}
// record most recent time so we know the length of the pause
CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime dur = CMSampleBufferGetDuration(sampleBuffer);
if (dur.value > 0)
{
pts = CMTimeAdd(pts, dur);
}
if (bVideo)
{
_lastVideo = pts;
}
else
{
_lastAudio = pts;
}
}
// pass frame to encoder
[_encoder encodeFrame:sampleBuffer isVideo:bVideo];
CFRelease(sampleBuffer);
}
- (BOOL) encodeFrame:(CMSampleBufferRef) sampleBuffer isVideo:(BOOL)bVideo
{
if (CMSampleBufferDataIsReady(sampleBuffer))
{
if (_writer.status == AVAssetWriterStatusUnknown)
{
CMTime startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
[_writer startWriting];
[_writer startSessionAtSourceTime:startTime];
}
if (_writer.status == AVAssetWriterStatusFailed)
{ // If Camera View is Flipped then Loop Enters inside this condition - writer error The operation could not be completed
NSLog(@"writer error %@", _writer.error.localizedDescription);
return NO;
}
if (bVideo)
{
if (_videoInput.readyForMoreMediaData == YES)
{
[_videoInput appendSampleBuffer:sampleBuffer];
return YES;
}
}
else
{
if (_audioInput.readyForMoreMediaData)
{
[_audioInput appendSampleBuffer:sampleBuffer];
return YES;
}
}
}
return NO;
}
提前致谢。
最佳答案
问题是这一行:
if (connection != _videoConnection)
{
bVideo = NO;
}
当您更改相机时,会创建一个新的 videoConnection,我不知道是在哪里创建的。但是,如果您像下面这样更改此行,它就会起作用:
//if (connection != _videoConnection)
if ([connection.output connectionWithMediaType:AVMediaTypeVideo] == nil)
{
bVideo = NO;
}
关于ios - 更改 AVCaptureDeviceInput 导致 AVAssetWriterStatusFailed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23805863/
我的目标是实时对相机输入应用一些过滤器。为了一步一步地做到这一点,我试图从带有 AVFoundation 的相机中获取输入,录制视频并将其保存在相机胶卷中。我试过了,但由于某种原因 AVAssetWr
我正在尝试更改相机 View Front 和 Back。它运行良好。如果录制视频时没有使用 Pause/Record 翻转> 选项工作正常。但是如果我们Flip Camera View 一次,则不会保
我正在尝试使用 AVAssetWriter 对视频进行编码。我正在使用 ARC。我想做的是通过套接字流式传输视频。 问题来了。我的设置第一次工作。 如果不先重新启动我的应用程序,我将无法再次使用 AV
我正在尝试使用 AVAssetWriter 进行屏幕录制,它也接受音频输入。但是,我一直坚持这个错误,在 appendSampleBuffer: (在 encodeAudioFrame: 内)几次调用
我正在使用 AVAssetWriterInputPixelBufferAdaptor 使用 AVAssetWriter 编写 MP4 视频文件。 源是来自 UIImagePickerControlle
我是一名优秀的程序员,十分优秀!