https://wiki.archlinux.org/api.php?action=feedcontributions&user=U19809&feedformat=atomArchWiki - User contributions [en]2024-03-29T12:03:08ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Convert_FLAC_to_MP3&diff=263075Convert FLAC to MP32013-06-17T07:13:56Z<p>U19809: /* Installation */</p>
<hr />
<div>[[Category:Audio/Video]]<br />
[[Category:Scripts]]<br />
{{Tip|You can use {{AUR|flac2mp3-bash}} script from aur to convert Flac to Mp3 easily.}}<br />
<br />
==Introduction==<br />
Here is a script that will convert FLAC to MP3 via the commandline. <br />
<br />
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.<br />
<br />
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. <br />
<br />
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. <br />
<br />
More information on flac: [http://wiki.hydrogenaudio.org/index.php?title=Flac FLAC]<br />
<br />
===Installation===<br />
<br />
First you need to install the following packages: flac, lame, and id3<br />
<br />
<pre><br />
pacman -S flac lame id3<br />
</pre><br />
<br />
Once those are installed, copy the following script into your preferred editor:<br />
<br />
<pre><br />
#! /bin/sh<br />
<br />
for a in *.flac; do<br />
OUTF=${a%.flac}.mp3<br />
<br />
ARTIST=$(metaflac "$a" --show-tag=ARTIST | sed s/.*=//g)<br />
TITLE=$(metaflac "$a" --show-tag=TITLE | sed s/.*=//g)<br />
ALBUM=$(metaflac "$a" --show-tag=ALBUM | sed s/.*=//g)<br />
GENRE=$(metaflac "$a" --show-tag=GENRE | sed s/.*=//g)<br />
TRACKNUMBER=$(metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g)<br />
DATE=$(metaflac "$a" --show-tag=DATE | sed s/.*=//g)<br />
<br />
flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"<br />
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"<br />
done<br />
</pre><br />
<br />
Below is a modified version of the above script which:<br />
* adds an optional parameter (-d), which, if passed on the command line, causes each source FLAC file to be deleted after successful conversion;<br />
* changes the LAME encoding options as follows:<br />
** 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;<br />
** drops "-m j", as LAME defaults to the specified value (joint stereo) when using --vbr-new (see above);<br />
** drops "-q 0", as LAME defaults to this behaviour when using VBR;<br />
** drops "-s 44.1", as LAME detects the proper sample rate to use;<br />
** adds "--noreplaygain" (personal preference);<br />
* 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).<br />
<br />
Now for the script:<br />
<br />
<pre><br />
#! /bin/sh<br />
<br />
for a in *.flac; do<br />
OUTF=${a%.flac}.mp3<br />
<br />
ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`<br />
TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`<br />
ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`<br />
GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`<br />
TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`<br />
DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`<br />
<br />
flac -c -d "$a" | lame --noreplaygain -V0 \<br />
--add-id3v2 --pad-id3v2 --ignore-tag-errors --tt "$TITLE" --tn "${TRACKNUMBER:-0}" \<br />
--ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tg "${GENRE:-12}" \<br />
- "$OUTF"<br />
RESULT=$?<br />
if [ "$1" ] && [ "$1" = "-d" ] && [ $RESULT -eq 0 ]; then<br />
rm "$a"<br />
fi<br />
done<br />
</pre><br />
<br />
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.<br />
<br />
<pre><br />
#! /bin/bash<br />
<br />
find . -type f -name "*.flac" -print0 | while read -d $'\0' a<br />
<br />
do<br />
OUTF=${a%.flac}.mp3<br />
<br />
ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`<br />
TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`<br />
ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`<br />
GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`<br />
TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`<br />
DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`<br />
<br />
flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"<br />
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"<br />
done<br />
</pre><br />
<br />
Save the script as ''flac2mp3'' and make the script executable:<br />
<br />
<pre><br />
chmod a+x flac2mp3<br />
</pre><br />
<br />
As root, copy the script to /usr/local/bin (or anywhere else that is in your $PATH).<br />
<br />
<pre><br />
cp flac2mp3 /usr/local/bin<br />
</pre><br />
<br />
To make /usr/local/bin in your $PATH, do (as normal user):<br />
<br />
<pre><br />
PATH=$PATH:/usr/local/bin/<br />
</pre><br />
<br />
and then (as normal user):<br />
<pre><br />
nano .bashrc<br />
</pre><br />
<br />
and add ''export PATH=$PATH:/usr/local/bin/''<br />
<br />
====Usage====<br />
<br />
Open up a terminal and cd to the directory of .flac files that you wish to convert and enter ''flac2mp3''<br />
<br />
You'll see the verbose decoding/encoding process in the terminal which may take a few moments. <br />
<br />
Done.<br />
<br />
====Addendum====<br />
<br />
With a small modification the command file can be used to transcode the files into a new directory structure:<br />
<pre><br />
#! /bin/bash<br />
find "$1" -name *.flac -print0 | while read -d $'\0' IF<br />
do<br />
OF=$(echo "$IF" | sed -e 's/\.flac$/.mp3/g' -e "s,$1,$2,g")<br />
echo "$IF -> $OF"<br />
mkdir -p "${OF%/*}"<br />
<br />
ARTIST=`metaflac "$IF" --show-tag=ARTIST | sed s/.*=//g`<br />
TITLE=`metaflac "$IF" --show-tag=TITLE | sed s/.*=//g`<br />
ALBUM=`metaflac "$IF" --show-tag=ALBUM | sed s/.*=//g`<br />
GENRE=`metaflac "$IF" --show-tag=GENRE | sed s/.*=//g`<br />
TRACKNUMBER=`metaflac "$IF" --show-tag=TRACKNUMBER | sed s/.*=//g`<br />
DATE=`metaflac "$IF" --show-tag=DATE | sed s/.*=//g`<br />
<br />
flac -c -d "$IF" 2> /dev/null | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OF" 2> /dev/null<br />
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OF"<br />
done<br />
</pre><br />
if saved in flac2mp3 this can be used as follows<br />
<pre>./flac2mp3 /srv/media/music /srv/media/music-lofi</pre><br />
which will take the directory structure under /srv/media/music and transcode its content into /srv/media/music-lofi.<br />
<br />
With some more tweaks the script will support directories that contain '.' (dots) somewhere in the path and<br />
executing the script from folders that contain 'flac' files<br />
<br />
Also made specifying an alternate folder location optional<br />
<pre><br />
#! /bin/bash<br />
find "$1" -name "*.flac" -print0 | while read -d $'\0' IF<br />
do<br />
if [ "$2" != "" ]<br />
then<br />
OF="${IF%.flac}.mp3"<br />
OF="$2/${OF##*/}"<br />
else<br />
OF="${IF%.flac}.mp3"<br />
fi<br />
echo "$IF -> $OF"<br />
mkdir -p "${OF%/*}"<br />
<br />
ARTIST=`metaflac "$IF" --show-tag=ARTIST | sed s/.*=//g`<br />
TITLE=`metaflac "$IF" --show-tag=TITLE | sed s/.*=//g`<br />
ALBUM=`metaflac "$IF" --show-tag=ALBUM | sed s/.*=//g`<br />
GENRE=`metaflac "$IF" --show-tag=GENRE | sed s/.*=//g`<br />
TRACKNUMBER=`metaflac "$IF" --show-tag=TRACKNUMBER | sed s/.*=//g`<br />
DATE=`metaflac "$IF" --show-tag=DATE | sed s/.*=//g`<br />
<br />
flac -c -d "$IF" 2> /dev/null | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OF" 2> /dev/null<br />
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OF"<br />
done<br />
</pre><br />
if saved in flac2mp3 this can be used as follows<br />
<pre>./flac2mp3 /srv/media.store/music /srv/media/music-lofi</pre><br />
or<br />
<pre>./flac2mp3 /srv/media.store/music </pre><br />
<br />
=====Nautilus-scripts=====<br />
<br />
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:<br />
<pre><br />
#!/bin/bash<br />
#<br />
# Copyright 2008 Octavio Ruiz<br />
# Distributed under the terms of the GNU General Public License v3<br />
# $Header: $<br />
#<br />
# Yet Another FLAC to MP3 script<br />
#<br />
# Author:<br />
# Octavio Ruiz (Ta^3) <tacvbo@tacvbo.net><br />
# Thanks:<br />
# Those comments at:<br />
# http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3<br />
# WebPage:<br />
# https://github.com/tacvbo/yaflac2mp3/tree<br />
#<br />
# This program is distributed in the hope that it will be useful,<br />
# but WITHOUT ANY WARRANTY. YOU USE AT YOUR OWN RISK. THE AUTHOR<br />
# WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY<br />
# OTHER KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.<br />
# See the GNU General Public License for more details.<br />
#<br />
# Modified by woohoo<br />
#<br />
# you need zenity package for notifications.<br />
# please note that you can put this script in ~/.gnome2/nautilus-scripts<br />
# and it will show up in right-click menu in any folder in gnome.<br />
#<br />
# modify the lame options to your preference example change -b 320 to -b 128 or -b 192 or -b 256<br />
# LAME_OPTS="--vbr-new -V 0 -b 256"<br />
# LAME_OPTS="-V 0 --vbr-new"<br />
<br />
LAME_OPTS="-b 320 -h --cbr"<br />
<br />
old_IFS=${IFS}<br />
IFS='<br />
'<br />
<br />
# when running from nautilus-scripts, it useful to find the current folder<br />
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"<br />
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then<br />
dir="$base"<br />
else<br />
while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done<br />
dir="$base/$1"<br />
fi<br />
<br />
if [ "$dir" != "" ]; then<br />
cd "$dir"<br />
fi<br />
<br />
id3v2=$(which id3v2)<br />
<br />
files=`find . -type f -regex '^.+\.flac$' | sort`<br />
<br />
declare -i nn=0<br />
for file in ${files}<br />
do<br />
fn=$(readlink -f "$file")<br />
vars=( `metaflac --no-utf8-convert --export-tags-to=- "${fn}"` )<br />
<br />
for N_vars in ${!vars[@]}<br />
do<br />
export "$(echo "${vars[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${vars[${N_vars}]#*=}"<br />
done<br />
<br />
dest=`echo "$fn"|sed -e 's/\.flac$/\.mp3/'`<br />
<br />
flac -dc "$fn" |\<br />
lame --ignore-tag-errors --add-id3v2 ${LAME_OPTS} \<br />
${artist:+--ta} ${artist} \<br />
${tracknumber:+--tn} ${tracknumber} \<br />
${title:+--tt} ${title} \<br />
${album:+--tl} ${album} \<br />
${date:+--ty} ${date} \<br />
${genre:+--tg} ${genre} \<br />
${comment:+--tc} ${comment} \<br />
- $dest<br />
<br />
[[ -x ${id3v2} ]] && ${id3v2} \<br />
${artist:+--artist} ${artist} \<br />
${tracknumber:+--track} ${tracknumber} \<br />
${title:+--song} ${title} \<br />
${album:+--album} ${album} \<br />
${date:+--year} ${date} \<br />
${genre:+--genre} ${genre} \<br />
${comment:+--comment} ${comment} \<br />
$dest<br />
<br />
let nn=nn+1<br />
done<br />
<br />
zenity --notification --text "Finished converting flac to mp3.${IFS}Processed ${nn} files."<br />
#zenity --info --text "Done!"<br />
IFS=${old_IFS}<br />
<br />
</pre><br />
<br />
More such conversion scripts on pastebin.com [http://pastebin.com/u/woohoo here].<br />
<br />
==More generic solution==<br />
<br />
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.</div>U19809