从流媒体中将音频样本附加到 AVAssetWriter

Append audio samples to AVAssetWriter from streaming

我在使用相机录制视频时正在使用一个项目,但音频来自流媒体。音频帧显然与视频帧不同步。 如果我在没有视频的情况下使用 AVAssetWriter,那么从流媒体中录制音频帧就可以正常工作。但是如果我附加视频和音频帧,我听不到任何声音。

这是将音频数据从流中转换为CMsampleBuffer的方法

AudioStreamBasicDescription monoStreamFormat = [self getAudioDescription];


CMFormatDescriptionRef format = NULL;
OSStatus status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &monoStreamFormat, 0,NULL, 0, NULL, NULL, &format);
if (status != noErr) {
    // really shouldn't happen
    return nil;
}

CMSampleTimingInfo timing = { CMTimeMake(1, 44100.0), kCMTimeZero, kCMTimeInvalid };


CMSampleBufferRef sampleBuffer = NULL;
status = CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, numSamples, 1, &timing, 0, NULL, &sampleBuffer);
if (status != noErr) {
    // couldn't create the sample alguiebuffer
    NSLog(@"Failed to create sample buffer");
    CFRelease(format);
    return nil;
}

// add the samples to the buffer
status = CMSampleBufferSetDataBufferFromAudioBufferList(sampleBuffer,
                                                        kCFAllocatorDefault,
                                                        kCFAllocatorDefault,
                                                        0,
                                                        samples);
if (status != noErr) {
    NSLog(@"Failed to add samples to sample buffer");
    CFRelease(sampleBuffer);
    CFRelease(format);
    return nil;
}

不知道是不是跟时间有关。但我想附加视频第一秒的音频帧。

这可能吗?

谢谢

我终于做到了

uint64_t _hostTimeToNSFactor = hostTime;

_hostTimeToNSFactor *= info.numer;
_hostTimeToNSFactor /= info.denom;

uint64_t timeNS = (uint64_t)(hostTime * _hostTimeToNSFactor);
CMTime presentationTime = self.initialiseTime;//CMTimeMake(timeNS, 1000000000);
CMSampleTimingInfo timing = { CMTimeMake(1, 44100), presentationTime, kCMTimeInvalid };