使用 AVAssetWriter 创建白噪声音轨

发布于 2024-12-13 11:52:34 字数 1867 浏览 0 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-20 11:52:34

您的代码中存在很多问题:

  1. 白噪声有统计定义,您正在创建未定义噪声。
  2. 您没有将audioFormatDescription传递给CMSampleBufferCreate
  3. 您使用CMSampleBufferCreate而不是CMAudioSampleBufferCreateWithPacketDescriptions使您的生活变得复杂(您没有数据包描述,所以只是过去的null和0)
  4. 您需要说何时您的音频缓冲区带有演示时间戳
  5. 您的音频缓冲区的长度为 0 帧。那太短了。
  6. 最后,您需要不断添加音频缓冲区,足以满足电影的整个持续时间

CoreMedia 代码可能很棘手,因为有数百个参数需要恰到好处。就您的目的而言,它可能有点太笼统了,所以为什么不获取可循环的白噪声片段并使用 AVMutableComposition 对其进行组装呢?这将为您提供一部电影&和一个音频文件,然后您可以使用另一个 AVMutableComposition 和 AVAssetExportSession 将其压缩在一起。

There are a bunch of problems in your code:

  1. White noise has a statistical definition, you're creating undefined noise.
  2. You're not passing the audioFormatDescription to CMSampleBufferCreate
  3. You're complicating your life by using CMSampleBufferCreate instead of CMAudioSampleBufferCreateWithPacketDescriptions (you don't have packet descriptions, so just past null & 0)
  4. You need to say when your audio buffer is with a Presentation Time Stamp
  5. Your audio buffer is 0 frames long. That's too short.
  6. and finally, you need to keep adding audio buffers, enough for the whole duration of your film

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文