前阵子用一个JavaCV的FFmpeg库实现了YUV视频数据地采集,同样的采集PCM音频数据也可以采用JavaCV的FFmpeg库。

传送门:JavaCV FFmpeg采集摄像头YUV数据

首先引入 javacpp-ffmpeg依赖:

  1. <dependency>
  2. <groupId>org.bytedeco.javacpp-presets</groupId>
  3. <artifactId>ffmpeg</artifactId>
  4. <version>${ffmpeg.version}</version>
  5. </dependency>

要采集麦克风的PCM数据,首先得知道麦克风的设备名称,可以通过FFmpeg来查找麦克风设备。

  1. ffmpeg.exe -list_devices true -f dshow -i dummy

在我的电脑上结果显示如下:

其中 “麦克风阵列 (Realtek(R) Audio)” 就是麦克风的设备名称。(这里建议用耳麦[External Mic (Realtek(R) Audio)]录制,质量要好很多很多)

采集麦克风数据即将麦克风作为音频流输入,通过FFmpeg解码获取音频帧,然后将视频帧转为PCM格式,最后将数据写入文件即可,其实音频的解码过程跟视频的解码过程是几乎一致的,下面是FFmpeg音频的解码流程:

可以看出除了解码函数,音频解码流程和视频解码流程是一致的,音频解码调用的是avcodec_decode_audio4,而视频解码调用的是avcodec_decode_video2

根据FFmpeg的解码流程,实现音频帧采集器大概需要经过以下几个步骤:

FFmpeg初始化

首先需要使用av_register_all()这个函数完成编码器和解码器的初始化,只有初始化了编码器和解码器才能正常使用;另外要采集的是设备,所以还需要调用avdevice_register_all()完成初始化。

分配AVFormatContext

接着需要分配一个AVFormatContext,可以通过avformat_alloc_context()来分配AVFormatContext。

  1. pFormatCtx = avformat_alloc_context();

打开音频流

通过avformat_open_input()来打开音频流,这里需要注意的是input format要指定为dshow,可以通过av_find_input_format("dshow")获取AVInputFormat对象。

  1. ret = avformat_open_input(pFormatCtx, String.format("audio=%s", input), av_find_input_format("dshow"), (AVDictionary) null);

注意:这里是音频用的是audio,不是video。

查找音频流

需要注意的是,查找音频流之前需要调用avformat_find_stream_info(),下面是查找视音频的代码:

  1. ret = avformat_find_stream_info(pFormatCtx, (AVDictionary) null);
  2. for (int i = 0; i < pFormatCtx.nb_streams(); i++) {
  3. if (pFormatCtx.streams(i).codec().codec_type() == AVMEDIA_TYPE_AUDIO) {
  4. audioIdx = i;
  5. break;
  6. }
  7. }

打开解码器

可以通过音频流来查找解码器,然后打开解码器,对音频流进行解码,Java代码如下:

  1. pCodecCtx = pFormatCtx.streams(audioIdx).codec();
  2. pCodec = avcodec_find_decoder(pCodecCtx.codec_id());
  3. if (pCodec == null) {
  4. throw new FFmpegException("没有找到合适的解码器:" + pCodecCtx.codec_id());
  5. }
  6. // 打开解码器
  7. ret = avcodec_open2(pCodecCtx, pCodec, (AVDictionary) null);
  8. if (ret != 0) {
  9. throw new FFmpegException(ret, "avcodec_open2 解码器打开失败");
  10. }

采集音频帧

最后就是采集音频帧了,这里需要注意的是,如果向采集麦克风的音频流解码得到的是自己想要的格式,需要再次进行格式转化。

  1. public AVFrame grab() throws FFmpegException {
  2. if (av_read_frame(pFormatCtx, pkt) >= 0 && pkt.stream_index() == audioIdx) {
  3. ret = avcodec_decode_audio4(pCodecCtx, pFrame, got, pkt);
  4. if (ret < 0) {
  5. throw new FFmpegException(ret, "avcodec_decode_audio4 解码失败");
  6. }
  7. if (got[0] != 0) {
  8. return pFrame;
  9. }
  10. av_packet_unref(pkt);
  11. }
  12. return null;
  13. }

通过音频解码之后可以得到PCM数据,这里为了读取方便,我将音频数据转化为AV_SAMPLE_FMT_S16,即LRLRLR这种格式,而不是planar,这样子读取PCM数据的时候,只需要读取data[0]即可,下面是一段采集主程序,将采集的音频pcm数据写入到s16.pcm中:

  1. public static void main(String[] args) throws FFmpegException, IOException {
  2. FFmpegRegister.register();
  3. // 耳机的麦克风质量要好得多
  4. AudioGrabber a = AudioGrabber.create("External Mic (Realtek(R) Audio)");
  5. // AV_SAMPLE_FMT_S16
  6. AudioPCMWriter writer = null;
  7. for (int i = 0; i < 100; i++) {
  8. AVFrame f = a.grab();
  9. if (writer == null) {
  10. writer = AudioPCMWriter.create(new File("s16.pcm"), toChannelLayout(a.channels()), a.sample_fmt(), a.sample_rate(),
  11. toChannelLayout(a.channels()), AV_SAMPLE_FMT_S16, a.sample_rate(), f.nb_samples());
  12. }
  13. writer.write(f);
  14. }
  15. writer.release();
  16. a.release();
  17. }

采集的pcm数据可以通过ffplay播放,命令如下:

  1. ffplay.exe -ar 44100 -ac 2 -f s16le -i s16.pcm

播放的时候可以按“Q”退出:

当然如果不用ffplay来播放pcm,也可以自己写java程序来播放:

  1. public static void main(String[] args) throws IOException, LineUnavailableException {
  2. AudioPCMPlayer player = AudioPCMPlayer.create(2, AudioUtils.toBit(AV_SAMPLE_FMT_S16), 44100);
  3. InputStream is = new FileInputStream("s16.pcm");
  4. byte[] buff = new byte[4096];
  5. int ret = -1;
  6. while ((ret = is.read(buff)) != -1) {
  7. if (ret < buff.length) {
  8. break;
  9. }
  10. player.play(buff);
  11. }
  12. is.close();
  13. player.release();
  14. }

=========================================================
音频帧采集器、及pcm播放程序源码可关注公众号 “HiIT青年” 发送 “ffmpeg-pcm” 获取。

HiIT青年
关注公众号,阅读更多文章。

版权声明:本文为itqn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/itqn/p/14224295.html