Difference between revisions of "Convert FLAC to MP3"

From ArchWiki
Jump to: navigation, search
(use new package templates, see Help:Style)
m (Usage: clarity)
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Audio/Video (English)]]
+
[[Category:Audio/Video]]
[[Category:Scripts (English)]]
+
[[Category:Scripts]]
 +
{{Article summary start}}
 +
{{Article summary text|Converting audio formats}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|Convert Any To Mp3}}
 +
{{Article summary wiki|Convert any Movie to DVD Video}}
 +
{{Article summary end}}
  
{{Tip|You can use {{AUR|flac2mp3-bash}} script from aur to convert Flac to Mp3 easily.}}
+
Here are a few scripts and tools that facilitate converting FLAC to MP3.
 +
 +
==Introduction==
 +
For more information on LAME switches/settings such as V0, visit the [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.  
  
=Introduction=
+
More information on FLAC: [http://wiki.hydrogenaudio.org/index.php?title=Flac FLAC]
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, the FLAC files in a directory are read, decompressed to WAV, and streamed into the MP3 encoder, LAME. Both scripts pass the ID3 tags from the FLAC files to the resulting MP3 files, and encode to MP3 V0.
  
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 .flac files are not modified and the resulting .mp3s will be in the same directory. All files with extensions not matching {{Ic|*.flac}} in the working directory (.nfo, images, .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 {{Ic|ffmpeg}} installed, which brings in the {{Ic|flac}} and {{Ic|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
+
  
 
<pre>
 
<pre>
pacman -S flac lame id3
+
#!/bin/bash
</pre>
+
 
+
Once those are installed, copy the following script into your preferred editor:
+
 
+
<pre>
+
for a in *.flac
+
 
+
do
+
OUTF=`echo "$a" | sed s/\.flac$/.mp3/g`
+
 
+
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"
+
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"
+
  
 +
for f in *.flac; do
 +
  ffmpeg -i "$f" -qscale:a 0 "${f[@]/%flac/mp3}"
 
done
 
done
 
</pre>
 
</pre>
  
Below is a modified version of the above script which:
+
===Without FFmpeg===
* 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:
+
If for some reason you have something against ffmpeg, you still need to have {{Ic|flac}} and {{Ic|lame}} installed. Here, the tagging process is more explicit, using the metadata utility that comes with {{Ic|flac}}, and passing the information to {{Ic|lame}}
  
 
<pre>
 
<pre>
for a in *.flac
+
#!/bin/bash
  
do
+
for a in *.flac; do
OUTF=`echo "$a" | sed s/\.flac$/.mp3/g`
+
  # give output correct extension
 +
  OUTF="${a[@]/%flac/mp3}"
  
ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
+
  # get the tags
TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
+
  ARTIST=$(metaflac "$a" --show-tag=ARTIST | sed s/.*=//g)
ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
+
  TITLE=$(metaflac "$a" --show-tag=TITLE | sed s/.*=//g)
GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
+
  ALBUM=$(metaflac "$a" --show-tag=ALBUM | sed s/.*=//g)
TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`
+
  GENRE=$(metaflac "$a" --show-tag=GENRE | sed s/.*=//g)
DATE=`metaflac "$a" --show-tag=DATE | 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 \
+
        --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
+
  
 +
  # 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
 
done
 
</pre>
 
</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.
+
===Usage===
  
<pre>
+
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'll see the verbose decoding/encoding process in the terminal which may take a few moments. Done!!! At this point, it's trivial to {{Ic|mv *.mp3}} all your new MP3s wherever you wish.
find -name "*.flac" -print0 | while read -d $'\0' a
+
 
+
 
+
do
+
OUTF=`echo "$a" | sed s/\.flac$/.mp3/g`
+
 
+
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"
+
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:
+
A useful extension of the above scripts is to let it recurse into all subdirectories of the working directory. Replace the first line ({{Ic|for .... do}}) with
  
 
<pre>
 
<pre>
chmod a+x flac2mp3
+
find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
 
</pre>
 
</pre>
  
As root, copy the script to /usr/local/bin (or anywhere else that is in your $PATH).
+
==Packages==
 
+
<pre>
+
cp flac2mp3 /usr/local/bin
+
</pre>
+
 
+
To make /usr/local/bin in your $PATH, do (as normal user):
+
 
+
<pre>
+
PATH=$PATH:/usr/local/bin/
+
</pre>
+
 
+
and then (as normal user):
+
<pre>
+
nano .bashrc
+
</pre>
+
 
+
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 s/\.flac$/.mp3/g | sed 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
+
</pre>
+
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.
+
  
=More generic solution=
+
* {{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.
 +
* {{AUR|flac2all}} Audio converter of FLAC to either Ogg Vorbis or MP3 retaining all tags and metadata
 +
* {{AUR|flac2mp3-bash}} Bash script to convert Flac to Mp3 easily
  
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.
+
==Related Links==
 +
* https://www.xiph.org/flac/
 +
* https://en.wikipedia.org/wiki/FLAC
 +
* http://lame.sourceforge.net/

Revision as of 21:29, 8 July 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end

Here are a few scripts and tools that facilitate converting FLAC to MP3.

Introduction

For more information on LAME switches/settings such as V0, visit the 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.

More information on FLAC: FLAC

Scripts

In these two examples, the FLAC files in a directory are read, decompressed to WAV, and streamed into the MP3 encoder, LAME. Both scripts pass the ID3 tags from the FLAC files to the resulting MP3 files, and encode to MP3 V0.

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 f in *.flac; do
  ffmpeg -i "$f" -qscale:a 0 "${f[@]/%flac/mp3}"
done

Without FFmpeg

If for some reason you have something against ffmpeg, 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

#!/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

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'll see the verbose decoding/encoding process in the terminal which may take a few moments. Done!!! At this point, it's trivial to mv *.mp3 all your new MP3s wherever you wish.

A useful extension of the above scripts is to let it recurse into all subdirectories of the working directory. Replace the first line (for .... do) with

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

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

Related Links