使用 AVAssetWriter 创建白噪声音轨
我目前正在使用 AVAssetWriter
生成 Quicktime mov。视频轨道已正确生成。现在我想添加一个“垃圾”mp4a 音轨。音频可能只是白噪声。我主要关心的是包含视频和音频轨道的 mov 文件。
如何设置仅包含 mp4a 格式的白噪声的 CMSampleBufferRef
?以下是我到目前为止所做的尝试:
CMSampleBufferRef garbageAudioSampleBuffer = NULL;
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 4;
audioFormat.mBytesPerFrame = 4;
CMAudioFormatDescriptionRef audioFormatDescrip = NULL;
CMAudioFormatDescriptionCreate(kCFAllocatorDefault,
&audioFormat,
0,
NULL,
0,
NULL,
NULL,
&audioFormatDescrip
);
CMSampleBufferCreate(kCFAllocatorDefault,
NULL,
YES,
NULL,
NULL,
NULL, // audioFormatDescrip,
0,
0,
NULL,
0,
NULL,
&garbageAudioSampleBuffer
);
if(myAVAssetAudioWriterInput isReadyForMoreMediaData)
[myAVAssetAudioWriterInput appendSampleBuffer:garbageAudioSampleBuffer];
当为audioFormatDesrip传递NULL
时,mov文件已成功生成,但仅包含视频轨道(并且没有音频轨道)。当我实际传递audioFormatDescript时,mov文件似乎已损坏。我可能必须实际传递一些样本,但我不确定如何传递。
注意:我已经验证 appendSampleBuffer
返回 YES
(为简洁起见,我省略了该代码)。
I'm currently using AVAssetWriter
to generate a Quicktime mov. The video track is generated correctly. Now I'd like to add a "garbage" mp4a audio track. The audio can just be white noise. What I'm mainly concerned with is the mov file containing both video and audio tracks.
How do I setup a CMSampleBufferRef
that just contains white noise in mp4a format? Here's what I've tried so far:
CMSampleBufferRef garbageAudioSampleBuffer = NULL;
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 4;
audioFormat.mBytesPerFrame = 4;
CMAudioFormatDescriptionRef audioFormatDescrip = NULL;
CMAudioFormatDescriptionCreate(kCFAllocatorDefault,
&audioFormat,
0,
NULL,
0,
NULL,
NULL,
&audioFormatDescrip
);
CMSampleBufferCreate(kCFAllocatorDefault,
NULL,
YES,
NULL,
NULL,
NULL, // audioFormatDescrip,
0,
0,
NULL,
0,
NULL,
&garbageAudioSampleBuffer
);
if(myAVAssetAudioWriterInput isReadyForMoreMediaData)
[myAVAssetAudioWriterInput appendSampleBuffer:garbageAudioSampleBuffer];
When NULL
is passed for the audioFormatDescrip the mov file is generated successfully but contains only a video track (and no audio track). When I actually pass audioFormatDescrip the mov file seems to be corrupted. I probably have to actually pass some samples but I'm not sure how.
Note: I've verified that appendSampleBuffer
returns YES
(for brevity I omitted that code).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中存在很多问题:
CoreMedia 代码可能很棘手,因为有数百个参数需要恰到好处。就您的目的而言,它可能有点太笼统了,所以为什么不获取可循环的白噪声片段并使用 AVMutableComposition 对其进行组装呢?这将为您提供一部电影&和一个音频文件,然后您可以使用另一个 AVMutableComposition 和 AVAssetExportSession 将其压缩在一起。
There are a bunch of problems in your code:
CoreMedia code can be tricky because there are hundreds of arguments that need to be just right. For your purposes it may be a little too general, so why not get a loopable snippet of white noise and assemble it using an AVMutableComposition? This will give you a film & and an audio file which you can then zip together using another AVMutableComposition and an AVAssetExportSession.