Raspberry Pi - Bluetooth & Audio

Setting up Bluetooth

Notes on setting up bluetooth on a headless Pi (in this case c3Pio.local)

  1. DON’T bother with pulseaudio
  2. Relevant mac addresses:
  1. c3Pio’s inbuilt controller is: Controller B8:27:EB:F2:FA:38
  2. the CSR 4.0 controller is: Controller 00:15:83:D1:E3:7B
  3. the Bose speaker is: 2C:41:A1:8C:65:F5
  1. Connecting to a Bluetooth speaker

Following this link gist-BluetoothA2DP resulted in the following recommendations:

sudo apt-get install bluealsa python-dbus
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
        The following additional packages will be installed:
            gir1.2-glib-2.0 libbluetooth3 libgirepository-1.0-1 libsbc1 python-gi
        Suggested packages:
            python-dbus-dbg python-dbus-doc python-gi-cairo
        The following NEW packages will be installed:
            bluealsa gir1.2-glib-2.0 libbluetooth3 libgirepository-1.0-1 libsbc1 python-dbus python-gi
        0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
        Need to get 707 kB of archives.
        After this operation, 2,478 kB of additional disk space will be used.

Also, need to be able to access bluetoothctl without sudo, which requires:

sudo adduser andrew bluetooth

Running newgroup bluetooth is meant to enable the new group’s permissions for me, but that didn’t seem to work until a system reboot had happened. Having installed the above, disabled the onboard bluetooth (as follows) and rebooted, I can now connect to the bluetooth speaker :)

Disabling onboard bluetooth

Add the following to /boot/config.txt:

# Disable onboard Bluetooth
dtoverlay=pi3-disable-bt

Execute the following command:

sudo systemctl disable hciuart.service

Routing sound to bluetooth

Information taken from MichelDeslierres-RPi_BTth_Audio

andrew@c3Pio:~ $ sudo apt install libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
        The following additional packages will be installed:
            gir1.2-ical-3.0 icu-devtools libblkid-dev libdb-dev libdb5.3-dev libffi-dev libglib2.0-bin libglib2.0-dev-bin libical3 libicu-dev libmount-dev libncurses-dev libpcre16-3 libpcre3-dev libpcre32-3 libpcrecpp0v5 libselinux1-dev libsepol1-dev python3-distutils python3-lib2to3 uuid-dev
        Suggested packages:
            db5.3-doc libglib2.0-doc libxml2-utils icu-doc ncurses-doc readline-doc
        The following NEW packages will be installed:
            gir1.2-ical-3.0 icu-devtools libblkid-dev libdb-dev libdb5.3-dev libdbus-1-dev libffi-dev libglib2.0-bin libglib2.0-dev libglib2.0-dev-bin libical-dev libical3 libicu-dev libmount-dev libncurses-dev libpcre16-3 libpcre3-dev libpcre32-3 libpcrecpp0v5 libreadline-dev libselinux1-dev libsepol1-dev libudev-dev python3-distutils python3-lib2to3 uuid-dev
        0 upgraded, 26 newly installed, 0 to remove and 0 not upgraded.
        Need to get 15.2 MB of archives.
        After this operation, 67.4 MB of additional disk space will be used.

Copy a .wav file onto the device and use aplay to test that audio can be routed as required. Having ensured that the bluetooth speaker is still connected, run:

aplay -D bluealsa:SRV=org.bluealsa,DEV=2C:41:A1:8C:65:F5,PROFILE=a2dp test.wav

If this works, can then try writing an .asoundrc file under my home directory that contains

pcm.!default {
type plug
slave.pcm {
type bluealsa
device "2C:41:A1:8C:65:F5"
profile "a2dp"
}
}
ctl.!default {
type bluealsa
}

… and then reboot.

Should then be able to play the test file just by typing:

aplay test.wav

All being well, nvlc will then also be able to use this default audio routing to the bluetooth speaker.

Restarting Bluetooth

sudo  /etc/init.d/bluetooth  restart

Bluetooth Volume

There seems to be an issue with playback volume using alsamixer, amixer and bluealsa. The more recent Raspberry Pi releases have avoided PulseAudio and Pavucontrol which are the oft quoted cures to the volume issue.

I’m starting to wonder if perhaps the answer lies in the configuration of alsa - talk of a file /usr/share/alsa/init/default, and I suspect that if I understood how to configure that it might help. I have found that pvolume (which means playback volume) as opposed to cvolume (capture volume) is the control that I am after!

There is a tutorial at alsaconfig-tutorial that might help.

Thinking from the basics:

  • A basic ‘principle’ of the Linux operating system is that everything is a file, so
  • Whatever ‘signal’ the pi sends to the bluetooth speaker must be written into a file somewhere, let’s call it the signal file
  • How the data in the signal file gets ‘routed’ to the bluetooth USB dongle must be sorted out by the USB configuration files.
  • Any configuration of the bluetooth communication itself must be affecting the way data is written into the signal file
  • ‘Routing’ the sound output to bluetooth rather than any other device must be ensuring that the sound signal is written into the bluetooth signal file rather than any other signal file.

The rfcomm command looks interesting!

Normalising the Volume of mp3 files

Just incase the problem is with the mp3 files themselves and not the playback volume on bluetooth. Sadly however I think the problem is with the latter

There is an application called mp3gain which is supposed to be quite good, but it is very much microsoft windows oriented, and I can’t get a working linux installation.

I have however found a python add-on to ffmpeg (ffmpeg-normalize) that seems to work.

Players

mpg123

Might be a better option than mpg321. More info at this interesting linuxtoday site

mpg321

Installed mpg321 by running:

sudo apt install mpg321
[sudo] password for andrew:
        Reading package lists... Done
        Building dependency tree
        Reading state information... Done
                The following additional packages will be installed:
                        libao-common libao4 libaudio-scrobbler-perl libid3tag0
                Suggested packages:
                        libaudio2 libsndio6.1
                The following NEW packages will be installed:
                        libao-common libao4 libaudio-scrobbler-perl libid3tag0 mpg321
                0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
                Need to get 138 kB of archives.
                After this operation, 413 kB of additional disk space will be used.

If running without having enabled bluetooth first, you get:

mpg321 Music/Annika_Strandhed/Series03/02-Forty_Words.mp3
        High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
        Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
        now maintained by Nanakos Chrysostomos and others.
        Uses code from various people. See 'README' for more!
        THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
        Title   : Forty Words                    Artist : BBC Radio 4
        Album   : Annika Stranded: Series 3      Year    : 2016
        Comment :                                Genre : Drama

        Directory: Music/Annika_Strandhed/Series03
        Playing MPEG stream from 02-Forty_Words.mp3 ...
        MPEG 1.0 layer III, 64 kbit/s, 48000 Hz stereo
        ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
        ALSA lib bluealsa-pcm.c:763:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA PCM: PCM not found
        Can't find a suitable libao driver. (Is device in use?)

But once Bluetooth is connected, everything works fine.

Useful commands

mpg321 <<filename>>
mpg321 -K --list playlist.txt
To stop, press [CTRL] + [C]

Webpages with further details on mpg321: * include * lindesk * deviceplus - very good intro

Playing mp3 from Python

See these articles from stackoverflow:

VLC

To install vlc:

sudo apt install vlc
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
        The following additional packages will be installed:
            adwaita-icon-theme at-spi2-core dbus-user-session dconf-gsettings-backend dconf-service fontconfig fontconfig-config fonts-dejavu-core fonts-freefont-ttf glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme liba52-0.7.4 libaa1 libaacs0 libaom0 libarchive13 libaribb24-0 libass9 libasyncns0 libatk-bridge2.0-0 libatk1.0-0 libatk1.0-data libatspi2.0-0 libauthen-sasl-perl libavahi-client3 libavc1394-0 libavcodec58 libavformat58 libavutil56 libbasicusageenvironment1 libbdplus0 libbluray2 libcaca0 libcairo-gobject2 libcairo2 libcddb2 libchromaprint1 libcodec2-0.8.1 libcolord2 libcroco3 libcups2 libdata-dump-perl libdatrie1 libdav1d3 libdc1394-22 libdca0 libdconf1 libdouble-conversion1 libdrm-amdgpu1 libdrm-common libdrm-nouveau2 libdrm-radeon1 libdrm2 libdvbpsi10 libdvdnav4 libdvdread4 libebml4v5 libegl-mesa0 libegl1 libencode-locale-perl libepoxy0 libevdev2 libfaad2 libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libflac8 libfont-afm-perl libfontconfig1 libfontenc1 libgbm1 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgl1 libgl1-mesa-dri libglapi-mesa libgles2 libglvnd0 libglx-mesa0 libglx0 libgme0 libgraphite2-3 libgroupsock8 libgsm1 libgtk-3-0 libgtk-3-bin libgtk-3-common libgudev-1.0-0 libharfbuzz0b libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libinput-bin libinput10 libio-html-perl libio-socket-ssl-perl libio-stringy-perl libipc-system-simple-perl libixml10 libjansson4 libjbig0 libjson-glib-1.0-0 libjson-glib-1.0-common libkate1 liblcms2-2 libldb1 liblirc-client0 liblivemedia64 libllvm9 liblua5.2-0 liblwp-mediatypes-perl liblwp-protocol-https-perl libmad0 libmailtools-perl libmatroska6v5 libmp3lame0 libmpcdec6 libmpeg2-4 libmpg123-0 libmtdev1 libmysofa0 libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libnfs12 libnotify4 libogg0 libopenjp2-7 libopenmpt-modplug1 libopenmpt0 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpcre2-16-0 libpixman-1-0 libplacebo7 libpostproc55 libprotobuf-lite17 libproxy-tools libproxy1v5 libpulse0 libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libqt5x11extras5 libraw1394-11 libresid-builder0c2a librest-0.7-0 librsvg2-2 librsvg2-common libsdl-image1.2 libsdl1.2debian libsecret-1-0 libsecret-common libsensors-config libsensors5 libshine3 libshout3 libsidplay2 libsm6 libsmbclient libsnappy1v5 libsndfile1 libsndio7.0 libsoup-gnome2.4-1 libsoup2.4-1 libsoxr0 libspatialaudio0 libspeex1 libspeexdsp1 libssh-gcrypt-4 libswresample3 libswscale5 libtag1v5 libtag1v5-vanilla libtdb1 libtevent0 libthai-data libthai0 libtheora0 libtie-ixhash-perl libtiff5 libtimedate-perl libtry-tiny-perl libtwolame0 libupnp13 liburi-perl libusageenvironment3 libva-drm2 libva-x11-2 libva2 libvdpau-va-gl1 libvdpau1 libvlc-bin libvlc5 libvlccore9 libvorbis0a libvorbisenc2 libvorbisfile3 libvpx5 libvulkan1 libwacom-bin libwacom-common libwacom2 libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwayland-server0 libwebp6 libwebpmux3 libwww-perl libwww-robotrules-perl libx11-protocol-perl libx11-xcb1 libx264-155 libx265-165 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-present0 libxcb-randr0 libxcb-render-util0 libxcb-render0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-util0 libxcb-xfixes0 libxcb-xinerama0 libxcb-xkb1 libxcb-xv0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxft2 libxi6 libxinerama1 libxkbcommon-x11-0 libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1 libxt6 libxtst6 libxv1 libxvidcore4 libxxf86dga1 libxxf86vm1 libzvbi-common libzvbi0 mesa-va-drivers mesa-vdpau-drivers notification-daemon perl-openssl-defaults python-talloc qt5-gtk-platformtheme qttranslations5-l10n samba-libs va-driver-all vdpau-driver-all vlc-bin vlc-data vlc-l10n vlc-plugin-base vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter vlc-plugin-visualization x11-common x11-utils x11-xserver-utils xdg-utils
        Suggested packages:
            lrzip libdigest-hmac-perl libgssapi-perl libbluray-bdj colord cups-common libdvdcss2 gvfs liblcms2-utils lirc libcrypt-ssleay-perl opus-tools pulseaudio qt5-image-formats-plugins qtwayland5 libraw1394-doc librsvg2-bin lm-sensors sndiod speex libauthen-ntlm-perl libunicode-map8-perl libunicode-string-perl xml-twig-tools mesa-utils nickle cairo-5c xorg-docs-core
        The following NEW packages will be installed:
            adwaita-icon-theme at-spi2-core dbus-user-session dconf-gsettings-backend dconf-service fontconfig fontconfig-config fonts-dejavu-core fonts-freefont-ttf glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme liba52-0.7.4 libaa1 libaacs0 libaom0 libarchive13 libaribb24-0 libass9 libasyncns0 libatk-bridge2.0-0 libatk1.0-0 libatk1.0-data libatspi2.0-0 libauthen-sasl-perl libavahi-client3 libavc1394-0 libavcodec58 libavformat58 libavutil56 libbasicusageenvironment1 libbdplus0 libbluray2 libcaca0 libcairo-gobject2 libcairo2 libcddb2 libchromaprint1 libcodec2-0.8.1 libcolord2 libcroco3 libcups2 libdata-dump-perl libdatrie1 libdav1d3 libdc1394-22 libdca0 libdconf1 libdouble-conversion1 libdrm-amdgpu1 libdrm-common libdrm-nouveau2 libdrm-radeon1 libdrm2 libdvbpsi10 libdvdnav4 libdvdread4 libebml4v5 libegl-mesa0 libegl1 libencode-locale-perl libepoxy0 libevdev2 libfaad2 libfile-basedir-perl libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libflac8 libfont-afm-perl libfontconfig1 libfontenc1 libgbm1 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgl1 libgl1-mesa-dri libglapi-mesa libgles2 libglvnd0 libglx-mesa0 libglx0 libgme0 libgraphite2-3 libgroupsock8 libgsm1 libgtk-3-0 libgtk-3-bin libgtk-3-common libgudev-1.0-0 libharfbuzz0b libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libinput-bin libinput10 libio-html-perl libio-socket-ssl-perl libio-stringy-perl libipc-system-simple-perl libixml10 libjansson4 libjbig0 libjson-glib-1.0-0 libjson-glib-1.0-common libkate1 liblcms2-2 libldb1 liblirc-client0 liblivemedia64 libllvm9 liblua5.2-0 liblwp-mediatypes-perl liblwp-protocol-https-perl libmad0 libmailtools-perl libmatroska6v5 libmp3lame0 libmpcdec6 libmpeg2-4 libmpg123-0 libmtdev1 libmysofa0 libnet-dbus-perl libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libnfs12 libnotify4 libogg0 libopenjp2-7 libopenmpt-modplug1 libopenmpt0 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpcre2-16-0 libpixman-1-0 libplacebo7 libpostproc55 libprotobuf-lite17 libproxy-tools libproxy1v5 libpulse0 libqt5core5a libqt5dbus5 libqt5gui5 libqt5network5 libqt5svg5 libqt5widgets5 libqt5x11extras5 libraw1394-11 libresid-builder0c2a librest-0.7-0 librsvg2-2 librsvg2-common libsdl-image1.2 libsdl1.2debian libsecret-1-0 libsecret-common libsensors-config libsensors5 libshine3 libshout3 libsidplay2 libsm6 libsmbclient libsnappy1v5 libsndfile1 libsndio7.0 libsoup-gnome2.4-1 libsoup2.4-1 libsoxr0 libspatialaudio0 libspeex1 libspeexdsp1 libssh-gcrypt-4 libswresample3 libswscale5 libtag1v5 libtag1v5-vanilla libtdb1 libtevent0 libthai-data libthai0 libtheora0 libtie-ixhash-perl libtiff5 libtimedate-perl libtry-tiny-perl libtwolame0 libupnp13 liburi-perl libusageenvironment3 libva-drm2 libva-x11-2 libva2 libvdpau-va-gl1 libvdpau1 libvlc-bin libvlc5 libvlccore9 libvorbis0a libvorbisenc2 libvorbisfile3 libvpx5 libvulkan1 libwacom-bin libwacom-common libwacom2 libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwayland-server0 libwebp6 libwebpmux3 libwww-perl libwww-robotrules-perl libx11-protocol-perl libx11-xcb1 libx264-155 libx265-165 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-present0 libxcb-randr0 libxcb-render-util0 libxcb-render0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-util0 libxcb-xfixes0 libxcb-xinerama0 libxcb-xkb1 libxcb-xv0 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxft2 libxi6 libxinerama1 libxkbcommon-x11-0 libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1 libxt6 libxtst6 libxv1 libxvidcore4 libxxf86dga1 libxxf86vm1 libzvbi-common libzvbi0 mesa-va-drivers mesa-vdpau-drivers notification-daemon perl-openssl-defaults python-talloc qt5-gtk-platformtheme qttranslations5-l10n samba-libs va-driver-all vdpau-driver-all vlc vlc-bin vlc-data vlc-l10n vlc-plugin-base vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter vlc-plugin-visualization x11-common x11-utils x11-xserver-utils xdg-utils
        0 upgraded, 306 newly installed, 0 to remove and 0 not upgraded.
        Need to get 110 MB of archives.
        After this operation, 906 MB of additional disk space will be used.

FFMPEG

FFMPEG: Convert m4a files to mp3 without significant loss of information / quality. Automated bitrate

Information comes from superuser-quality_mp3_conversion

I have a load of audio files (about 1000) which I want to convert from m4a to mp3 so I can use play them on a CD player which has a USB port. I tried doing something simple like:

ffmpeg -i FILE.m4a FILE.mp3

but this seems to reduce the bitrate to a very low value, which isn’t what I want.

Similarly I don’t want to convert using a constant bitrate, such as 320k, because some of the files I am converting are 320k m4a’s and some are as low quality as 96k m4a’s.

It seems to make no sense to force 320k, since some files will become many times larger than they need be. Similarly it makes no sense to destroy all my 320k files by converting them to something much lower than 96k. (At the moment, the files are being converted to about 50k.)

Does anyone know how I can do this? What I really want to do is tell ffmpeg to convert all m4a files in a directory into mp3’s while retaining the current audio quality as best it can. (Of course there is likely to be some extra losses from converting from lossy to lossy file formats.)

Thanks for your help. If this isn’t possible, is there some sort of script which might detect the required quality as it converts files individually?

PS: I am working on an intel Mac, but also have a Ubuntu box

VBR

You can use the -qscale:a option in ffmpeg to create a variable bitrate (VBR) MP3 output. From FFmpeg Wiki: Encoding VBR (Variable Bit Rate) mp3 audio: Control quality with -qscale:a (or the alias -q:a). Values are encoder specific, so for libmp3lame the range is 0-9 where a lower value is a higher quality. 0-3 will normally produce transparent results, 4 (default) should be close to perceptual transparency, and 6 usually produces an “acceptable” quality. The option -qscale:a is mapped to the -V option in the standalone lame command-line interface tool. You’ll have to experiment to see what value is acceptable for you. Also see Hydrogen Audio: Recommended LAME Encoder Settings.

Encoding multiple files

You can use a Bash “for loop” to encode all files in a directory:

$ mkdir newfiles
$ for f in *.m4a; do ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done

Get ffmpeg

It’s recommended to use a recent build of ffmpeg if possible.

See the FFmpeg Download page for links to builds for OS X, Linux, and Windows.

You can also follow a guide to compile FFmpeg.

Comments received:

    edited Jan 21 '14 at 18:09
    answered Jan 20 '14 at 21:46
    llogan
    29.9k75797
• 1
  Okay thanks, the commands worked but I don't think it is doing what I wanted. I've set -q:a 0, which is doing what I expected for higher quality m4a's (producing large files with higher vbr rates) but lower quality files I think are still being converted into large files, with vbr's of about 250-260 kb/s. I would have expected the files which are about 96k in m4a format to be converted to a similar bitrate with the lame encoder in vbr mode. I'm assuming I haven't quite understood how the vbr encoding mode works? – user3728501 Jan 21 '14 at 22:42


    Use Ffmpeg (there are many GUI versions too)
1. MP3 is already discontinued (in 2017), obsolete codec. Internet has already switched to AAC(more efficient and quality codec):
   ffmpeg -i filenameee.m4a -acodec copy output.aac
2. However, if you still wish to use MP3 (because it is supported by more devices still):
   ffmpeg -i filenameee.m4a -acodec libmp3lame -ab 256k output.mp3
            shareimprove this answer
            edited May 6 '18 at 11:41
            answered Aug 24 '15 at 12:05
    T.Todua
    1,67542033
• 4
  Since the question is about quality, maybe it will be beneficial if convert to 256k instead of 128. – VessoVit Sep 21 '15 at 12:31

    This worked for me:
    ffmpeg -i testing.m4v -b:a 192K -vn testing.mp3
    shareimprove this answer


    Using existing answers as a basis I wrote a bash script which should do exactly what the question asks on an Ubuntu machine (tested on 16.04). You'll need to install avprobe which is in the libav-tools package. You can install avprobe with the following command
    sudo apt-get install libav-tools
    NOTE: It would be wise to make a backup of any folder in which you run this script as well as make your best effort to read and understand what it is doing before running.
    I have not tested very rigorously so YMMV.
    #!/bin/bash

    mkdir "mp3s"
    for f in *.m4a;
    do
bitrate=$(avprobe "${f}" 2> >(grep bitrate) | sed 's/^.*bitrate://g' | sed 's/[^0-9]*//g')
bitrate="${bitrate}K"
new_filename=$(echo "${f}" | sed 's/.m4a$/.mp3/g')
ffmpeg -y -i "${f}" -acodec libmp3lame -ab "${bitrate}" "mp3s/${new_filename}"
    done


    If you work at 48 KhZ and at least 24-bit in the input file (from a file editor i.ex.) -q:a 0 will reproduce the 20 kHz at no so large file as flac... But low quality audio unless you reduce the noise from high frequencies (hiss) by dithering or nose reduction audio editors, created in conversions, that will be reproduced, then will use data file as a inaudible HF, but expensive noise.