• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - AVAssetWriter 不录制音频

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:34:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我无法在 iPhone 上使用 avassetwriter 将音频录制到视频中。我可以在手机上从相机录制视频没有问题,但是当我尝试添加音频时,我什么也得不到,而且相册应用程序中视频中显示的持续时间也显示出一些非常不正常的东西,一个 4 秒的视频会显示 15:54:01 或类似的内容,即使视频更短,在该数字之后制作的每个视频都会增加。我一直在尝试遵循我在此处的其他问题中看到的内容,但没有运气。

这就是我设置音频输入的方式

captureSession = [[AVCaptureSession alloc] init];
//set up audio input 
AVCaptureDevice *audioDevice     = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio];
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error ]; 
audioOutput = [[AVCaptureAudioDataOutput alloc] init];

if([captureSession canAddOutput:audioOutput])
{
    [captureSession addOutput:audioOutput];
}    

[audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

这是我如何设置 AVAssetWriter

videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:MOVIE_PATH] fileType:AVFileTypeQuickTimeMovie error:&error];

AudioChannelLayout acl;
bzero( &acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;


NSDictionary* audioOutputSettings = audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
         [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
         [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
         [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
         [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
         [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                           nil];

然后这是我如何使用 audioOutput 回调发送的 CMSampleBufferRef 编写音频样本缓冲区

- (void) captureAudioCMSampleBufferRef)sampleBuffer
 {
   if([audioWriterInput isReadyForMoreMediaData]){
     [audioWriterInput appendSampleBuffer:sampleBuffer];
   }
 }

非常感谢任何帮助,我整天都被困在这个问题上。



Best Answer-推荐答案


我没有看到你调用 [videoWriter startSessionAtSourceTime],当 audioWriterInput 没有准备好时,你也正在丢弃音频样本缓冲区(这不可能你想要什么)。

所以您的问题在于您所写内容的 PTS(演示时间戳)。您可以使用 startSessionAtSourceTime 告诉输出您的时间线从给定时间 t 开始,或者您可以修改附加的缓冲区,使其具有从零开始的presentationTimeStamps。

关于ios - AVAssetWriter 不录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9235936/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap