写一个基于ffmpeg采集usb音频设备后采集的程序代码
时间: 2023-03-13 18:23:20 浏览: 37
可以使用以下代码:#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
int main(int argc, char *argv[]) {
AVFormatContext* fmt_ctx = NULL;
AVInputFormat *ifmt = av_find_input_format("alsa");
// Open the device.
int err = avformat_open_input(&fmt_ctx, "hw:0", ifmt, NULL);
if (err < 0) {
return err;
}
err = avformat_find_stream_info(fmt_ctx, NULL);
if (err < 0) {
return err;
}
// Find the first audio stream
int audio_stream_idx = -1;
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_idx = i;
break;
}
}
// Read audio packets
AVPacket packet;
while (av_read_frame(fmt_ctx, &packet) == 0) {
if (packet.stream_index == audio_stream_idx) {
// Process audio packet
}
av_packet_unref(&packet);
}
avformat_close_input(&fmt_ctx);
return 0;
}