以编程方式检测音频文件是立体声还是单声道?

Programatically detect if an audio file is stereo or mono?

我想在 MacOS 平台上编写一个 bash 脚本,该脚本将对 m4a 具有 ALAC 编码的文件执行不同的操作,具体取决于它们是立体声还是单声道。我该如何进行 stereo/mono 检测?有什么 tools/libraries 可以用吗?谢谢。

通过 https://ffmpeg.org/ffprobe.html

获得
if ffprobe -i the-audio-file.mp4 |& grep stereo; then
    echo stereo
else
    echo mono
fi

作为 ffprobe 将输出如下结果:

ffprobe version 3.4.2 Copyright (c) 2007-2018 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --disable-jack --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-nonfree
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'the-audio-file.mp4':
  Metadata:
    major_brand     : M4A 
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 2018-01-27T18:53:31.000000Z
    track           : 0
    disc            : 0
  Duration: 04:59:13.02, start: 0.000000, bitrate: 381 kb/s
    Stream #0:0(eng): Audio: alac (alac / 0x63616C61), 44100 Hz, stereo, s16p, 381 kb/s (default)
    Metadata:
      creation_time   : 2018-01-27T18:53:31.000000Z
       0       0       0

请注意,这不适用于 he-aac,因为 this bug in ffprobe