Linux - Convert to mp3

From The TinkerNet Wiki
Jump to navigation Jump to search

flac to mp3

From Ask Ubuntu

First of all you must make sure that it's installed.

sudo apt-get install libav-tools

It should have lame and flac codecs, now is just create a bash script to finish the job:

cat > flac2mp3

Here the shell will wait for your commands, copy and paste this:

#!/bin/bash
[[ $# == 0 ]] && set -- *.flac
   for f; do
  avconv -i "$f" -qscale:a 0 "${f[@]/%flac/mp3}"
done

Now press Ctrl + D. Make your script executable chmod +x flac2mp3. Now go you can use it like this:

./flac2mp3 /path/with/all/my/flacs/*.flac

You can also copy the script to somewhere in your PATH and then cd to the directory with the flacs and execute it.


With regards to the following parameter used above:

-qscale:a 0

will not actually give you a exact 320k file, although it is probably the best setting to use anyway. The suggested settings actually give a target bitrate of 245 kbits/s with a range of 220-260. If you really wanted 320k mp3s you would have to go to CBR and use:

-c:a libmp3lame -b:a 320k

but you would need great ears to notice the difference...

Reference:

m4a to mp3

Fuck me...

Copy the flac2mp3 script & just change "flac" to "m4a"

Seems to work just fine.

from StackExchange

(Doesn't actually work... but some ideas to think about in it...)

#!/bin/bash
## jc 2016
## convert [m4a mp3 wma] to mp3 128k
## [-vn] disable video recording
##
## avconv with lame mp3 plugin
## [-acodec libmp3lame]
##
## 192 k constant bitrate
## [-ab 192k]
## [-ab 128k]
##
## 44.1kHz sampling rate
## [-ar 44100]
##
## 2 channel audio
## [-ac 2]
##

##  force the shell to do a case insensitive comparison
shopt -s nocasematch

working_directory="./mp3_converted"
# check if dir exist
if [ ! -d "$working_directory" ];
then
  # dir does not exist
      echo "convert directory does not exist $working_directory..."
  `mkdir -p "$working_directory"`
       echo "convert directory created $working_directory..."
fi

COUNT=0

for i in *; do

  case $i in
    *.mp3)
      avconv -analyzeduration 999999999 -map_metadata 0 -i "$i" -vn -acodec libmp3lame -ac 2 -ab 128k -ar 44100 "$working_directory/`basename "$i" .mp3`.mp3"
      echo $i
      ;;
    *.m4a)
      ##avconv -analyzeduration 999999999 -map_metadata 0 -i "$i" -vn -acodec libmp3lame -ac 2 -ab 128k -ar 44100 "$working_directory/`basename "$i" .m4a`.mp3"
      # adjusted for ffmpeg to test.
      ffmpeg -i "$i" -n -acodec libmp3lame -ab 128k "$working_directory/`basename "$i" .m4a`.mp3"
      echo $i
      ;;
    *.wma)
      avconv -analyzeduration 999999999 -map_metadata 0 -i "$i" -vn -acodec libmp3lame -ac 2 -ab 128k -ar 44100 "$working_directory/`basename "$i" .wma`.mp3"
      echo $i
      ;;
    *)
      echo "other"
      ;;
  esac

done

## back to normal comparison
shopt -u nocasematch
exit 0