gpt4 book ai didi

c++ - 如何用音频数据填充 AVFrame

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:31 32 4
gpt4 key购买 nike

我使用 Win32 API 实现了一个桌面录制程序。视频捕获功能有效,但我不知道将音频放入 FFmpeg 编码器的数据结构中。

下面的代码将获取默认输出音频的数据。

// Set wave format when sampling the audio
WAVEFORMATEX wf;
wf->wFormatTag = WAVE_FORMAT_PCM;
wf->nChannels = 1;
wf->nSamplesPerSec = 12000;
wf->nBlockAlign = 1;
wf->wBitsPerSample = 8;
wf->cbSize = 0;
wf->nAvgBytesPerSec = nChannels * nSamplesPerSec * wBitsPerSample / 8;

// Open wave input channel
HWAVEIN hWaveIn;
OpenWaveIn(&hWaveIn,&wf);

// Prepare Wave In Header and allocate memory
WAVEHDR waveHdr;
DWORD dataSize = 240000L;
PrepareWaveIn(&hWaveIn, &waveHdr, dataSize);

// Start recording
StartRecord(&hWaveIn);

// Stop recording
MMTIME mmt;
StopRecord(&hWaveIn, &mmt);

waveHdr->lpData 是指向锁定数据缓冲区的指针。

问题是如何实现下面的函数让waveHdr->->lpData可以保存到AVFrame

static AVFrame *get_audio_frame(OutputStream *ost)
{
AVFrame *frame = ost->tmp_frame;
int j, i, v;
int16_t *q = (int16_t*)frame->data[0];
AVRational arg;
arg.num = 1;
arg.den = 1;

//the transform code should be put here

frame->pts = ost->next_pts;
ost->next_pts += frame->nb_samples;
return frame;
}

有人知道如何实现吗?

最佳答案

粘贴自 http://ffmpeg.org/doxygen/trunk/encoding-example_8c.html .这是 audio_encode_example() 函数,因此它应该包含您需要了解的有关如何对音频帧进行编码的所有信息。请注意,它们创建了一个简单的正弦声音,即从 sin() 生成声波。

/*
* Audio encoding example
*/
static void audio_encode_example(const char *filename)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int frame_size, i, j, out_size, outbuf_size;
FILE *f;
short *samples;
float t, tincr;
uint8_t *outbuf;

printf("Audio encoding\n");

/* find the MP2 encoder */
codec = avcodec_find_encoder(CODEC_ID_MP2);
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}

c= avcodec_alloc_context();

/* put sample parameters */
c->bit_rate = 64000;
c->sample_rate = 44100;
c->channels = 2;

/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}

/* the codec gives us the frame size, in samples */
frame_size = c->frame_size;
samples = malloc(frame_size * 2 * c->channels);
outbuf_size = 10000;
outbuf = malloc(outbuf_size);

f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "could not open %s\n", filename);
exit(1);
}

/* encode a single tone sound */
t = 0;
tincr = 2 * M_PI * 440.0 / c->sample_rate;
for(i=0;i<200;i++) {
for(j=0;j<frame_size;j++) {
samples[2*j] = (int)(sin(t) * 10000);
samples[2*j+1] = samples[2*j];
t += tincr;
}
/* encode the samples */
out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
fwrite(outbuf, 1, out_size, f);
}
fclose(f);
free(outbuf);
free(samples);

avcodec_close(c);
av_free(c);
}

关于c++ - 如何用音频数据填充 AVFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27439123/

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