FFMPEG 抓取virtual-audio-capturer 数据

0. 查看本机virtual-audio-capturer默认的录取音频的采样率,采样大小,通道。

C:\Users\lili>ffmpeg -f dshow -i audio="virtual-audio-capturer" test.aac
ffmpeg version N-93815-g181031906e Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.3.1 (GCC) 20190414
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-liba
bshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable
nable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cu
  libavutil      56. 26.101 / 56. 26.101
  libavcodec     58. 52.101 / 58. 52.101
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 50.100 /  7. 50.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, dshow, from 'audio=virtual-audio-capturer':
  Duration: N/A, start: 1035314.088000, bitrate: 1536 kb/s
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, adts, to 'test.aac':
  Metadata:
    encoder         : Lavf58.27.103
    Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.52.101 aac
size=      31kB time=00:00:02.04 bitrate= 125.4kbits/s speed=0.997x
video:0kB audio:31kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.160081%
[aac @ 0000000000359500] Qavg: 39893.352
Received stop event after 207 passes
Exiting normally, received signal 2.

可以看到

virtual-audio-capturer录取声卡音频的结果是:pcm_s16le, 48000 Hz, stereo, s16

 

1.实现

#include <stdio.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include <libavutil/log.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>
}


/**
* @brief open audio device
* @return succ: AVFormatContext*, fail: NULL
*/
static
AVFormatContext* open_dev(const char* device_name){

	int ret = -1;
	char errors[1024] = { 0, };

	//char *devicename = "video=Integrated Camera";
	//char *devicename = "audio=virtual-audio-capturer";
	//char *devicename = "麦克风 (Realtek High Definition Au";
	AVInputFormat *m_pAudioInputFormat = av_find_input_format("dshow");
	AVDictionary *options = NULL;
	AVFormatContext *m_pAudioFmtCtx = avformat_alloc_context();
	if ((ret = avformat_open_input(&m_pAudioFmtCtx, device_name, m_pAudioInputFormat, &options)) < 0){
		av_strerror(ret, errors, 1024);
		fprintf(stderr, "Failed to open audio device, [%d]%s\n", ret, errors);
		return NULL;
	}

	return m_pAudioFmtCtx;
}
void record_virtual_audio_capturer() {

	//context
	AVFormatContext *fmt_ctx = NULL;
	AVCodecContext *c_ctx = NULL;

	//set log level
	av_log_set_level(AV_LOG_DEBUG);

	//register audio device
	avdevice_register_all();

	//create file
	char *out = "D:\\video\\audio.pcm";
	FILE *outfile = fopen(out, "wb+");
	if (!outfile){
		printf("Error, Failed to open file!\n");
		goto __ERROR;
	}

	//打开设备
	fmt_ctx = open_dev("audio=virtual-audio-capturer");
	if (!fmt_ctx){
		printf("Error, Failed to open device!\n");
		goto __ERROR;
	}

	//read data from device
	int count = 0, ret = -1;
	AVPacket pkt;
	while ((ret = av_read_frame(fmt_ctx, &pkt)) == 0 && count++ < 2048) {
		fwrite(pkt.data, 1, pkt.size, outfile);
		av_packet_unref(&pkt); //release pkt
	}

__ERROR:

	if (c_ctx){
		avcodec_free_context(&c_ctx);
	}

	//close device and release ctx
	if (fmt_ctx) {
		avformat_close_input(&fmt_ctx);
	}

	if (outfile){
		//close file
		fclose(outfile);
	}

	av_log(NULL, AV_LOG_DEBUG, "finish!\n");

	return;
}

int main(int argc, char *argv[])
{
	record_virtual_audio_capturer();
	return 0;
}

 

2.验证,使用默认的采样率,采样大小,通道来播放pcm裸数据

ffplay -ar 48000 -channels 2 -f s16le D:\video\audio.pcm

C:\Users\lili> ffplay -ar 48000 -channels 2 -f s16le  D:\video\audio.pcm
ffplay version N-93815-g181031906e Copyright (c) 2003-2019 the FFmpeg developers
  built with gcc 8.3.1 (GCC) 20190414
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-liba
bshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable
nable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cu
  libavutil      56. 26.101 / 56. 26.101
  libavcodec     58. 52.101 / 58. 52.101
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 50.100 /  7. 50.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
[s16le @ 0000000000471040] Estimating duration from bitrate, this may be inaccurate
Input #0, s16le, from 'D:\video\audio.pcm':
  Duration: 00:00:20.82, bitrate: 1536 kb/s
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
  11.71 M-A:  0.000 fd=   0 aq=   94KB vq=    0KB sq=    0B f=0/0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值