Difference between revisions of "Convert FLAC to MP3"

From ArchWiki
Jump to: navigation, search
m (Usage: clarity)
(With FFmpeg: added < /dev/null in front of ffmpeg command because without it ffmpeg fails to convert (using the find variant))
(5 intermediate revisions by 4 users not shown)
Line 9: Line 9:
  
 
Here are a few scripts and tools that facilitate converting FLAC to MP3.
 
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.
 
  
More information on FLAC: [http://wiki.hydrogenaudio.org/index.php?title=Flac FLAC]
+
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 {{ic|--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.
  
==Scripts==
+
== 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.
 
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 {{Ic|*.flac}} in the working directory (.nfo, images, .sfv, etc.) are ignored.
+
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.
  
===With FFmpeg===
+
=== With FFmpeg ===
  
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.
+
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.
  
<pre>
+
{{bc|
 
#!/bin/bash
 
#!/bin/bash
  
for f in *.flac; do
+
for a in *.flac; do
   ffmpeg -i "$f" -qscale:a 0 "${f[@]/%flac/mp3}"
+
   < /dev/null ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
 
done
 
done
</pre>
+
}}
  
===Without FFmpeg===
+
=== Without FFmpeg ===
  
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}}
+
If for some reason you have something against FFmpeg, 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}}
  
<pre>
+
{{bc|<nowiki>
 
#!/bin/bash
 
#!/bin/bash
  
Line 57: Line 54:
 
     --tn "${TRACKNUMBER:-0}" --ty "$DATE" - "$OUTF"
 
     --tn "${TRACKNUMBER:-0}" --ty "$DATE" - "$OUTF"
 
done
 
done
</pre>
+
</nowiki>}}
 +
 
 +
=== Usage ===
  
===Usage===
+
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.
  
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.
+
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
  
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
+
$ find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
  
<pre>
+
== Packages ==
find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
+
</pre>
+
  
==Packages==
+
* {{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.
  
* {{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.
+
== See also ==
* {{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
+
  
==Related Links==
 
 
* https://www.xiph.org/flac/
 
* https://www.xiph.org/flac/
 
* https://en.wikipedia.org/wiki/FLAC
 
* https://en.wikipedia.org/wiki/FLAC
 
* http://lame.sourceforge.net/
 
* http://lame.sourceforge.net/
 +
* http://wiki.hydrogenaudio.org/index.php?title=Flac - More information on FLAC.

Revision as of 16:13, 22 December 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.

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.

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

See also