gpt4 book ai didi

objective-c - 使用 Obj-C 插件和 AVAssetWriterInput 向视频添加音频

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:26 24 4
gpt4 key购买 nike

我正在尝试拍摄使用 iVidCap 插件创建的视频并向其添加音频。基本上与这个问题完全相同:Writing video + generated audio to AVAssetWriterInput, audio stuttering .我以这篇文章中的代码为基础尝试自己修改 iVidCap.mm 文件,但应用程序总是在 endRecordingSession 中崩溃。

我不确定我需要如何修改 endRecordingSession 以适应音频(原始插件只是创建一个视频文件)。这是函数:

- (int) endRecordingSession: (VideoDisposition) action {

NSLog(@"Start endRecordingSession");
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSLog(@"Auto released pool");

NSString *filePath;
BOOL success = false;

[videoWriterInput markAsFinished];
NSLog(@"Mark video writer input as finished");
//[audioWriterInput markAsFinished];

// Wait for the video status to become known.
// Is this really doing anything?
int status = videoWriter.status;
while (status == AVAssetWriterStatusUnknown) {
NSLog(@"Waiting for video to complete...");
[NSThread sleepForTimeInterval:0.5f];
status = videoWriter.status;
}

NSLog(@"Video completed");

@synchronized(self) {
success = [videoWriter finishWriting];
NSLog(@"Success: %@", success);
if (!success) {
// We failed to successfully finalize the video file.
NSLog(@"finishWriting returned NO");

} else {
// The video file was successfully written to the Documents folder.
filePath = [[self getDocumentsFileURL:videoFileName] path];
if (action == Save_Video_To_Album) {

// Move the video to an accessible location on the device.
NSLog(@"Temporary video filePath=%@", filePath);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath)) {
NSLog(@"Video IS compatible. Adding it to photo album.");
UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(copyToPhotoAlbumCompleteFromVideo: didFinishSavingWithError: contextInfo:), nil);
} else {
NSLog(@"Video IS NOT compatible. Could not be added to the photo album.");
success = NO;
}
} else if (action == Discard_Video) {
NSLog(@"Video cancelled. Removing temporary video file: %@", filePath);
[self removeFile:filePath];
}
}

[self cleanupWriter];
}

isRecording = false;

[pool drain];

return success; }

现在它在 [videoWriter finishWriting] 上崩溃了。我尝试添加 [audioWriterInput markAsFinished],但随后它崩溃了。我会联系原始发布者,因为他们似乎可以正常工作,但似乎没有办法发送私有(private)消息。

有没有人对我如何让它工作或它崩溃的原因有任何建议?我已尽力解决这个问题,但我对 Obj-C 还很陌生。如果需要,我可以发布其余代码(其中很多都在前面引用的原始帖子中)。

最佳答案

问题实际上可能出在 writeAudioBuffer 函数中。

如果您从该帖子复制代码但没有更改它,那么您肯定会遇到一些问题。

你需要做这样的事情:

if ( ![self waitForAudioWriterReadiness]) {
NSLog(@"WARNING: writeAudioBuffer dropped frame after wait limit reached.");
return 0;
}

OSStatus status;
CMBlockBufferRef bbuf = NULL;
CMSampleBufferRef sbuf = NULL;

size_t buflen = n * nchans * sizeof(float);

CMBlockBufferRef tmp_bbuf = NULL;
status = CMBlockBufferCreateWithMemoryBlock(
kCFAllocatorDefault,
samples,
buflen,
kCFAllocatorDefault,
NULL,
0,
buflen,
0,
&tmp_bbuf);

if (status != noErr || !tmp_bbuf) {
NSLog(@"CMBlockBufferCreateWithMemoryBlock error");
return -1;
}
// Copy the buffer so that we get a copy of the samples in memory.
// CMBlockBufferCreateWithMemoryBlock does not actually copy the data!
//
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault, tmp_bbuf, kCFAllocatorDefault, NULL, 0, buflen, kCMBlockBufferAlwaysCopyDataFlag, &bbuf);
//CFRelease(tmp_bbuf); // causes abort?!
if (status != noErr) {
NSLog(@"CMBlockBufferCreateContiguous error");
//CFRelease(bbuf);
return -1;
}


CMTime timestamp = CMTimeMake(sample_position_, 44100);

status = CMAudioSampleBufferCreateWithPacketDescriptions(
kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf);

sample_position_ += n;
if (status != noErr) {
NSLog(@"CMSampleBufferCreate error");
return -1;
}
BOOL r = [audioWriterInput appendSampleBuffer:sbuf];
if (!r) {
NSLog(@"appendSampleBuffer error");
}
//CFRelease(bbuf); // crashes, don't know why.. Is there a leak here?
//CFRelease(sbuf);

return 0;

这里有一些与内存管理有关的事情我不确定。

另外一定要使用:

audioWriterInput.expectsMediaDataInRealTime = YES;

关于objective-c - 使用 Obj-C 插件和 AVAssetWriterInput 向视频添加音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940888/

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