Search code examples
audiom4a

Programatically detect if an audio file is stereo or mono?


I am wanting to write a bash script on a MacOS platform that will perform different operations to m4a files with ALAC encoding, depending on whether they are stereo or mono. How can I go about the stereo/mono detection? Are there any tools/libraries that I can utilise? Thank you.


Solution

  • Got it using https://ffmpeg.org/ffprobe.html

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

    Works as ffprobe will output a result like this:

    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
    

    Note, this does not work for he-aac, due to this bug in ffprobe