Difference between revisions of "Convert Flac to Mp3"

From ArchWiki
Jump to: navigation, search
m (Installation)
m (Parallel version: Sigh)
 
(48 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Category:Audio/Video]]
+
[[Category:Multimedia]]
[[Category:Scripts]]
+
[[ja:Flac を Mp3 に変換]]
{{Tip|You can use {{AUR|flac2mp3-bash}} script from aur to convert Flac to Mp3 easily.}}
+
{{Related articles start}}
 +
{{Related|Convert any Movie to DVD Video}}
 +
{{Related articles end}}
  
==Introduction==
+
This article presents ways of doing audio transcoding between FLAC and MP3 audio files using command line/scripted tools, and suggest a few graphical utilities to do the same and more.
Here is a script that will convert FLAC to MP3 via the commandline.  
+
  
Essentially, the .flac files within a directory will be decompressed to .wav and then the resulting .wav files will be encoded to .mp3 using the latest LAME switches for encodings (''-V 0 --vbr-new''). The ID3 tags of the original .flac files will be passed to the resulting .mp3 files.
+
== Scripts ==
  
The original .flac files will not be harmed and the resulting .mp3s will be in the same directory. All other files in the directory (.nfo, images, .sfv, etc) will be ignored and unharmed.  
+
In these two examples, FLAC files in current directory are encoded by the LAME MP3 encoder. Both scripts pass the ID3 tags from the FLAC files to the resulting MP3 files, and encode to MP3 V0. V0 results in a variable bitrate usually between 220-260 kbps. The audio of a V0 file is transparent, meaning one cannot tell the difference between the lossy file and the original source (compact disc/lossless), but yet the file size is a quite lower. For more information on LAME switches/settings such as V0, visit the [http://wiki.hydrogenaudio.org/index.php?title=LAME Hydrogenaudio LAME Wiki].
  
For more information on LAME switches/settings such as V0, visit [http://wiki.hydrogenaudio.org/index.php?title=LAME Hydrogenaudio LAME Wiki]. V0 is roughly equivalent to ''--preset extreme'' which results in a variable bitrate usually between 220-260. The audio of a V0 is transparent, meaning one cannot tell the difference between the lossy file and the original source (compact disc/lossless), but yet the file size is a quite reasonable.  
+
The original {{ic|.flac}} files are not modified and the resulting {{ic|.mp3}}s will be in the same directory. All files with extensions not matching {{ic|*.flac}} in the working directory ({{ic|.nfo}}, images, {{ic|.sfv}}, etc.) are ignored.
  
More information on flac: [http://wiki.hydrogenaudio.org/index.php?title=Flac FLAC]
+
=== With FFmpeg ===
  
===Installation===
+
Chances are, your system already has {{Pkg|ffmpeg}} installed, which brings in the {{Pkg|flac}} and {{Pkg|lame}} packages. FFmpeg has all the encoding and decoding facilities built in to do the job.
  
First you need to install the following packages: flac, lame, and id3
+
{{bc|
 
+
#!/bin/bash
<pre>
+
pacman -S flac lame id3
+
</pre>
+
 
+
Once those are installed, copy the following script into your preferred editor:
+
 
+
<pre>
+
#! /bin/sh
+
 
+
for a in *.flac; do
+
    OUTF=${a%.flac}.mp3
+
 
+
    ARTIST=$(metaflac "$a" --show-tag=ARTIST | sed s/.*=//g)
+
    TITLE=$(metaflac "$a" --show-tag=TITLE | sed s/.*=//g)
+
    ALBUM=$(metaflac "$a" --show-tag=ALBUM | sed s/.*=//g)
+
    GENRE=$(metaflac "$a" --show-tag=GENRE | sed s/.*=//g)
+
    TRACKNUMBER=$(metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g)
+
    DATE=$(metaflac "$a" --show-tag=DATE | sed s/.*=//g)
+
  
    flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
+
for a in ./*.flac; do
    id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"
+
  ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
 
done
 
done
</pre>
+
}}
  
Below is a modified version of the above script which:
+
==== Parallel version ====
* adds an optional parameter (-d), which, if passed on the command line, causes each source FLAC file to be deleted after successful conversion;
+
* changes the LAME encoding options as follows:
+
** drops --vbr-new, as it is the default VBR behaviour as of LAME 3.98, and thus is automatically used when "-V 0" is used;
+
** drops "-m j", as LAME defaults to the specified value (joint stereo) when using --vbr-new (see above);
+
** drops "-q 0", as LAME defaults to this behaviour when using VBR;
+
** drops "-s 44.1", as LAME detects the proper sample rate to use;
+
** adds "--noreplaygain" (personal preference);
+
* and uses LAME to write tags instead of the id3 package, which has the dual advantage of removing the need for an additional package in the tool chain and allowing the script to write both id3v1 and id3v2 tags (the id3 package does not support id3v2 tags).
+
  
Now for the script:
+
Since LAME is a single-threaded encoder, conversion can be accelerated by encoding multiple files concurrently on multiple cores. To do this, install the {{Pkg|moreutils}} package, and run:
  
<pre>
+
#!/bin/bash
#! /bin/sh
+
 +
parallel-moreutils -i -j$(nproc) ffmpeg -i {} -qscale:a 0 {}.mp3 -- ./*.flac
 +
rename .flac.mp3 .mp3 ./*.mp3
  
for a in *.flac; do
+
==== Makefile for incremental recursive transcoding ====
    OUTF=${a%.flac}.mp3
+
  
    ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
+
{{Warning|1=Makefiles do not handle spaces correctly, see [https://bbs.archlinux.org/viewtopic.php?pid=1506405#p1506405] for details.}}
    TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
+
    ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
+
    GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
+
    TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`
+
    DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`
+
  
    flac -c -d "$a" | lame --noreplaygain -V0 \
+
Besides transcoding in parallel with {{ic|make -j$(nproc)}}, this has the added benefit of not regenerating transcoded files that already exist on subsequent executions:
        --add-id3v2 --pad-id3v2 --ignore-tag-errors --tt "$TITLE" --tn "${TRACKNUMBER:-0}" \
+
        --ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tg "${GENRE:-12}" \
+
        - "$OUTF"
+
    RESULT=$?
+
    if [ "$1" ] && [ "$1" = "-d" ] && [ $RESULT -eq 0 ]; then
+
        rm "$a"
+
    fi
+
done
+
</pre>
+
  
Alternatively, below is a script that will search for all FLAC audio files beyond where the script resides on your filesystem and convert them to MP3; including those pesky filenames with spaces.
+
{{bc|<nowiki>
 +
SOURCE_DIR := flacdir
 +
XCODE_MP3_DIR := mp3dir
 +
# NOTE: see lame -v option for quality meaning
 +
XCODE_MP3_QUALITY := 0
  
<pre>
+
# Find .flac sources and determine corresponding targets
#! /bin/bash
+
flac_srcs := $(shell find $(SOURCE_DIR) -type f -name '*.flac')
 +
flac_2_mp3_tgts := $(patsubst $(SOURCE_DIR)/%.flac, $(XCODE_MP3_DIR)/%.mp3, \
 +
    $(flac_srcs))
  
find . -type f -name "*.flac" -print0 | while read -d $'\0' a
+
.PHONY: all mp3 flac_2_mp3
  
do
+
all: mp3  
    OUTF=${a%.flac}.mp3
+
  
    ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
+
mp3: flac_2_mp3
    TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
+
    ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
+
    GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
+
    TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`
+
    DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`
+
  
    flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
+
flac_2_mp3: $(flac_2_mp3_tgts)
    id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"
+
done
+
</pre>
+
  
Save the script as ''flac2mp3'' and make the script executable:
+
$(XCODE_MP3_DIR)/%.mp3: $(SOURCE_DIR)/%.flac
 +
        @echo "converting -> $@"
 +
        @mkdir -p "$(@D)"
 +
        @ffmpeg -v error -i "$<" -codec:a libmp3lame \
 +
            -q:a $(XCODE_MP3_QUALITY) "$(@)"
 +
</nowiki>}}
  
<pre>
+
=== Without FFmpeg ===
chmod a+x flac2mp3
+
</pre>
+
  
As root, copy the script to /usr/local/bin (or anywhere else that is in your $PATH).
+
If for some reason FFmpeg is not installed and you do not want to install it, you still need to have {{Pkg|flac}} and {{Pkg|lame}} installed. Here, the tagging process is more explicit using the metadata utility that comes with {{Pkg|flac}} and passing the information to {{Pkg|lame}}. The process duration will slightly increase since FLACs must first be decoded to WAVE and then fed into the MP3 encoder.
  
<pre>
+
{{bc|<nowiki>
cp flac2mp3 /usr/local/bin
+
#!/bin/bash
</pre>
+
  
To make /usr/local/bin in your $PATH, do (as normal user):
+
for a in *.flac; do
 +
  # give output correct extension
 +
  OUTF="${a[@]/%flac/mp3}"
  
<pre>
+
  # get the tags
PATH=$PATH:/usr/local/bin/
+
  ARTIST=$(metaflac "$a" --show-tag=ARTIST | sed s/.*=//g)
</pre>
+
  TITLE=$(metaflac "$a" --show-tag=TITLE | sed s/.*=//g)
 +
  ALBUM=$(metaflac "$a" --show-tag=ALBUM | sed s/.*=//g)
 +
  GENRE=$(metaflac "$a" --show-tag=GENRE | sed s/.*=//g)
 +
  TRACKNUMBER=$(metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g)
 +
  DATE=$(metaflac "$a" --show-tag=DATE | sed s/.*=//g)
  
and then (as normal user):
+
  # stream flac into the lame encoder
<pre>
+
  flac -c -d "$a" | lame -V0 --add-id3v2 --pad-id3v2 --ignore-tag-errors \
nano .bashrc
+
    --ta "$ARTIST" --tt "$TITLE" --tl "$ALBUM" --tg "${GENRE:-12}" \
</pre>
+
    --tn "${TRACKNUMBER:-0}" --ty "$DATE" - "$OUTF"
 
+
and add ''export PATH=$PATH:/usr/local/bin/''
+
 
+
====Usage====
+
 
+
Open up a terminal and cd to the directory of .flac files that you wish to convert and enter ''flac2mp3''
+
 
+
You'll see the verbose decoding/encoding process in the terminal which may take a few moments.
+
 
+
Done.
+
 
+
====Addendum====
+
 
+
With a small modification the command file can be used to transcode the files into a new directory structure:
+
<pre>
+
#! /bin/bash
+
find "$1" -name *.flac -print0 | while read -d $'\0' IF
+
do
+
  OF=$(echo "$IF" | sed -e 's/\.flac$/.mp3/g' -e "s,$1,$2,g")
+
  echo "$IF -> $OF"
+
  mkdir -p "${OF%/*}"
+
 
+
  ARTIST=`metaflac "$IF" --show-tag=ARTIST | sed s/.*=//g`
+
  TITLE=`metaflac "$IF" --show-tag=TITLE | sed s/.*=//g`
+
  ALBUM=`metaflac "$IF" --show-tag=ALBUM | sed s/.*=//g`
+
  GENRE=`metaflac "$IF" --show-tag=GENRE | sed s/.*=//g`
+
  TRACKNUMBER=`metaflac "$IF" --show-tag=TRACKNUMBER | sed s/.*=//g`
+
  DATE=`metaflac "$IF" --show-tag=DATE | sed s/.*=//g`
+
 
+
  flac -c -d "$IF" 2> /dev/null | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OF" 2> /dev/null
+
  id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OF"
+
 
done
 
done
</pre>
+
</nowiki>}}
if saved in flac2mp3 this can be used as follows
+
<pre>./flac2mp3 /srv/media/music /srv/media/music-lofi</pre>
+
which will take the directory structure under /srv/media/music and transcode its content into /srv/media/music-lofi.
+
  
With some more tweaks the script will support directories that contain '.' (dots) somewhere in the path and
+
=== Recursively ===
executing the script from folders that contain 'flac' files
+
A useful extension of the above scripts is to let them recurse into all subdirectories of the working directory. To do so, replace the first line ({{ic|for .... do}}) with:
  
Also made specifying an alternate folder location optional
+
find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
<pre>
+
#! /bin/bash
+
find "$1" -name "*.flac" -print0 | while read -d $'\0' IF
+
do
+
  if [ "$2" != "" ]
+
  then
+
    OF="${IF%.flac}.mp3"
+
    OF="$2/${OF##*/}"
+
  else
+
    OF="${IF%.flac}.mp3"
+
  fi
+
  echo "$IF -> $OF"
+
  mkdir -p "${OF%/*}"
+
  
  ARTIST=`metaflac "$IF" --show-tag=ARTIST | sed s/.*=//g`
+
=== Usage ===
  TITLE=`metaflac "$IF" --show-tag=TITLE | sed s/.*=//g`
+
  ALBUM=`metaflac "$IF" --show-tag=ALBUM | sed s/.*=//g`
+
  GENRE=`metaflac "$IF" --show-tag=GENRE | sed s/.*=//g`
+
  TRACKNUMBER=`metaflac "$IF" --show-tag=TRACKNUMBER | sed s/.*=//g`
+
  DATE=`metaflac "$IF" --show-tag=DATE | sed s/.*=//g`
+
  
  flac -c -d "$IF" 2> /dev/null | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OF" 2> /dev/null
+
For ease of use, add the script to your {{ic|PATH}}. Open up a terminal, {{ic|cd}} to the directory of FLAC files that you wish to convert, and invoke {{ic|flac2mp3}} (or whatever you named the script). You will see the verbose decoding/encoding process in the terminal which may take a few moments. Done! At this point, it is trivial to {{ic|mv *.mp3}} all your new MP3s wherever you wish.
  id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OF"
+
done
+
</pre>
+
if saved in flac2mp3 this can be used as follows
+
<pre>./flac2mp3 /srv/media.store/music /srv/media/music-lofi</pre>
+
or
+
<pre>./flac2mp3 /srv/media.store/music </pre>
+
  
=====Nautilus-scripts=====
+
=== Packages ===
 
+
Here's a well-written version of "flac2mp3" script which will run either from command line or, if you copy it into <tt>~/.gnome2/nautilus-scripts</tt>, it will appear in the right-click menu in Nautilus:
+
<pre>
+
#!/bin/bash
+
#
+
# Copyright 2008 Octavio Ruiz
+
# Distributed under the terms of the GNU General Public License v3
+
# $Header: $
+
#
+
# Yet Another FLAC to MP3 script
+
#
+
# Author:
+
# Octavio Ruiz (Ta^3) <tacvbo@tacvbo.net>
+
# Thanks:
+
# Those comments at:
+
# http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3
+
# WebPage:
+
# https://github.com/tacvbo/yaflac2mp3/tree
+
#
+
# This program is distributed in the hope that it will be useful,
+
# but WITHOUT ANY WARRANTY. YOU USE AT YOUR OWN RISK. THE AUTHOR
+
# WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY
+
# OTHER KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.
+
# See the GNU General Public License for more details.
+
#
+
# Modified by woohoo
+
#
+
# you need zenity package for notifications.
+
# please note that you can put this script in ~/.gnome2/nautilus-scripts
+
# and it will show up in right-click menu in any folder in gnome.
+
#
+
# modify the lame options to your preference example change -b 320 to -b 128 or -b 192 or -b 256
+
# LAME_OPTS="--vbr-new -V 0 -b 256"
+
# LAME_OPTS="-V 0 --vbr-new"
+
 
+
LAME_OPTS="-b 320 -h --cbr"
+
 
+
old_IFS=${IFS}
+
IFS='
+
'
+
 
+
# when running from nautilus-scripts, it useful to find the current folder
+
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
+
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
+
  dir="$base"
+
else
+
  while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
+
  dir="$base/$1"
+
fi
+
 
+
if [ "$dir" != "" ]; then
+
  cd "$dir"
+
fi
+
 
+
id3v2=$(which id3v2)
+
 
+
files=`find . -type f -regex '^.+\.flac$' | sort`
+
 
+
declare -i nn=0
+
for file in ${files}
+
do
+
  fn=$(readlink -f "$file")
+
  vars=( `metaflac --no-utf8-convert --export-tags-to=- "${fn}"` )
+
 
+
  for N_vars in ${!vars[@]}
+
  do
+
    export "$(echo "${vars[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${vars[${N_vars}]#*=}"
+
  done
+
 
+
  dest=`echo "$fn"|sed -e 's/\.flac$/\.mp3/'`
+
 
+
  flac -dc "$fn" |\
+
    lame --ignore-tag-errors --add-id3v2 ${LAME_OPTS} \
+
        ${artist:+--ta} ${artist} \
+
        ${tracknumber:+--tn} ${tracknumber} \
+
        ${title:+--tt} ${title} \
+
        ${album:+--tl} ${album} \
+
        ${date:+--ty} ${date} \
+
        ${genre:+--tg} ${genre} \
+
        ${comment:+--tc} ${comment} \
+
        - $dest
+
+
    [[ -x ${id3v2} ]] && ${id3v2} \
+
        ${artist:+--artist} ${artist} \
+
        ${tracknumber:+--track} ${tracknumber} \
+
        ${title:+--song} ${title} \
+
        ${album:+--album} ${album} \
+
        ${date:+--year} ${date} \
+
        ${genre:+--genre} ${genre} \
+
        ${comment:+--comment} ${comment} \
+
        $dest
+
 
+
  let nn=nn+1
+
done
+
  
zenity --notification --text "Finished converting flac to mp3.${IFS}Processed ${nn} files."
+
* {{AUR|whatmp3}} - A small Python script that accepts a list of directories containing FLAC files as arguments and converts them to MP3 with the specified options.
#zenity --info --text "Done!"
+
* {{AUR|flac2all}} - Audio converter of FLAC to either Ogg Vorbis or MP3 retaining all tags and metadata.
IFS=${old_IFS}
+
* {{AUR|flac2mp3-bash}} - Bash script to convert Flac to Mp3 easily.
  
</pre>
+
== Graphical applications ==
  
More such conversion scripts on pastebin.com [http://pastebin.com/u/woohoo here].
+
*{{App|SoundConverter|A dedicated audio transcoding utility built for the [[GNOME]] desktop and relying on GStreamer. It can make use of [http://library.gnome.org/users/gnome-audio-profiles/stable/gnome-audio-profiles-usage.html.en GNOME Audio Profiles] and features multithreaded conversions. It can also extract the audio from videos.|http://soundconverter.org/|{{Pkg|soundconverter}}}}
 +
*{{App|soundKonverter|A Qt graphical frontend to various audio manipulation programs. Features conversion, ripping and other audio manipulation functionalities.|https://github.com/HessiJames/soundkonverter/wiki|{{Pkg|soundkonverter}}}}
 +
*{{App|[[Wikipedia:FFmpeg#Projects_using_FFmpeg|WinFF]]|A GUI for the powerful multimedia converter FFmpeg. Features dedicated profiles for audio transcoding.|http://code.google.com/p/winff/|{{Pkg|winff}}}}
  
==More generic solution==
+
== See also ==
  
See [[Convert_Any_To_Mp3]] for a more generic solution if you want to transform to mp3 all kind of audio formats supported by both mplayer and mutagen.
+
* https://www.xiph.org/flac/
 +
* [[wikipedia:FLAC]]
 +
* http://lame.sourceforge.net/
 +
* http://wiki.hydrogenaudio.org/index.php?title=Flac - More information on FLAC.

Latest revision as of 15:18, 9 March 2016

This article presents ways of doing audio transcoding between FLAC and MP3 audio files using command line/scripted tools, and suggest a few graphical utilities to do the same and more.

Scripts

In these two examples, FLAC files in current directory are encoded by the LAME MP3 encoder. Both scripts pass the ID3 tags from the FLAC files to the resulting MP3 files, and encode to MP3 V0. V0 results in a variable bitrate usually between 220-260 kbps. The audio of a V0 file is transparent, meaning one cannot tell the difference between the lossy file and the original source (compact disc/lossless), but yet the file size is a quite lower. For more information on LAME switches/settings such as V0, visit the Hydrogenaudio LAME Wiki.

The original .flac files are not modified and the resulting .mp3s will be in the same directory. All files with extensions not matching *.flac in the working directory (.nfo, images, .sfv, etc.) are ignored.

With FFmpeg

Chances are, your system already has ffmpeg installed, which brings in the flac and lame packages. FFmpeg has all the encoding and decoding facilities built in to do the job.

#!/bin/bash

for a in ./*.flac; do
  ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done

Parallel version

Since LAME is a single-threaded encoder, conversion can be accelerated by encoding multiple files concurrently on multiple cores. To do this, install the moreutils package, and run:

#!/bin/bash

parallel-moreutils -i -j$(nproc) ffmpeg -i {} -qscale:a 0 {}.mp3 -- ./*.flac
rename .flac.mp3 .mp3 ./*.mp3

Makefile for incremental recursive transcoding

Warning: Makefiles do not handle spaces correctly, see [1] for details.

Besides transcoding in parallel with make -j$(nproc), this has the added benefit of not regenerating transcoded files that already exist on subsequent executions:

SOURCE_DIR := flacdir
XCODE_MP3_DIR := mp3dir
# NOTE: see lame -v option for quality meaning
XCODE_MP3_QUALITY := 0

# Find .flac sources and determine corresponding targets
flac_srcs := $(shell find $(SOURCE_DIR) -type f -name '*.flac')
flac_2_mp3_tgts := $(patsubst $(SOURCE_DIR)/%.flac, $(XCODE_MP3_DIR)/%.mp3, \
    $(flac_srcs))

.PHONY: all mp3 flac_2_mp3

all: mp3 

mp3: flac_2_mp3

flac_2_mp3: $(flac_2_mp3_tgts)

$(XCODE_MP3_DIR)/%.mp3: $(SOURCE_DIR)/%.flac
        @echo "converting -> $@"
        @mkdir -p "$(@D)"
        @ffmpeg -v error -i "$<" -codec:a libmp3lame \
            -q:a $(XCODE_MP3_QUALITY) "$(@)"

Without FFmpeg

If for some reason FFmpeg is not installed and you do not want to install it, you still need to have flac and lame installed. Here, the tagging process is more explicit using the metadata utility that comes with flac and passing the information to lame. The process duration will slightly increase since FLACs must first be decoded to WAVE and then fed into the MP3 encoder.

#!/bin/bash

for a in *.flac; do
  # give output correct extension
  OUTF="${a[@]/%flac/mp3}"

  # get the tags
  ARTIST=$(metaflac "$a" --show-tag=ARTIST | sed s/.*=//g)
  TITLE=$(metaflac "$a" --show-tag=TITLE | sed s/.*=//g)
  ALBUM=$(metaflac "$a" --show-tag=ALBUM | sed s/.*=//g)
  GENRE=$(metaflac "$a" --show-tag=GENRE | sed s/.*=//g)
  TRACKNUMBER=$(metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g)
  DATE=$(metaflac "$a" --show-tag=DATE | sed s/.*=//g)

  # stream flac into the lame encoder
  flac -c -d "$a" | lame -V0 --add-id3v2 --pad-id3v2 --ignore-tag-errors \
    --ta "$ARTIST" --tt "$TITLE" --tl "$ALBUM"  --tg "${GENRE:-12}" \
    --tn "${TRACKNUMBER:-0}" --ty "$DATE" - "$OUTF"
done

Recursively

A useful extension of the above scripts is to let them recurse into all subdirectories of the working directory. To do so, replace the first line (for .... do) with:

find -type f -name "*.flac" -print0 | while read -d $'\0' a; do

Usage

For ease of use, add the script to your PATH. Open up a terminal, cd to the directory of FLAC files that you wish to convert, and invoke flac2mp3 (or whatever you named the script). You will see the verbose decoding/encoding process in the terminal which may take a few moments. Done! At this point, it is trivial to mv *.mp3 all your new MP3s wherever you wish.

Packages

  • whatmp3AUR - A small Python script that accepts a list of directories containing FLAC files as arguments and converts them to MP3 with the specified options.
  • flac2allAUR - Audio converter of FLAC to either Ogg Vorbis or MP3 retaining all tags and metadata.
  • flac2mp3-bashAUR - Bash script to convert Flac to Mp3 easily.

Graphical applications

  • SoundConverter — A dedicated audio transcoding utility built for the GNOME desktop and relying on GStreamer. It can make use of GNOME Audio Profiles and features multithreaded conversions. It can also extract the audio from videos.
http://soundconverter.org/ || soundconverter
  • soundKonverter — A Qt graphical frontend to various audio manipulation programs. Features conversion, ripping and other audio manipulation functionalities.
https://github.com/HessiJames/soundkonverter/wiki || soundkonverter
  • WinFF — A GUI for the powerful multimedia converter FFmpeg. Features dedicated profiles for audio transcoding.
http://code.google.com/p/winff/ || winff

See also