Difference between revisions of "Convert Flac to Mp3"

From ArchWiki
Jump to: navigation, search
m (Style fixes.)
m (Parallel version: Sigh)
 
(40 intermediate revisions by 15 users not shown)
Line 1: Line 1:
[[Category:Audio/Video]]
+
[[Category:Multimedia]]
[[Category:Scripts]]
+
[[ja:Flac を Mp3 に変換]]
{{Article summary start}}
+
{{Related articles start}}
{{Article summary text|Converting audio formats}}
+
{{Related|Convert any Movie to DVD Video}}
{{Article summary heading|Related}}
+
{{Related articles end}}
{{Article summary wiki|Convert Any To Mp3}}
+
{{Article summary wiki|Convert any Movie to DVD Video}}
+
{{Article summary end}}
+
  
Here are a few scripts and tools that facilitate converting FLAC to MP3.
+
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.
 
+
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, 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].
  
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 ({{ic|.nfo}}, images, {{ic|.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.
  
 
{{bc|
 
{{bc|
 
#!/bin/bash
 
#!/bin/bash
  
for f in *.flac; do
+
for a in ./*.flac; do
   ffmpeg -i "$f" -qscale:a 0 "${f[@]/%flac/mp3}"
+
   ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
 
done
 
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 {{Pkg|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|1=Makefiles do not handle spaces correctly, see [https://bbs.archlinux.org/viewtopic.php?pid=1506405#p1506405] for details.}}
 +
 +
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:
 +
 +
{{bc|<nowiki>
 +
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) "$(@)"
 +
</nowiki>}}
  
 
=== 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 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.
  
 
{{bc|<nowiki>
 
{{bc|<nowiki>
Line 56: Line 92:
 
</nowiki>}}
 
</nowiki>}}
  
=== Usage ===
+
=== 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 ({{ic|for .... do}}) with:
  
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 -type f -name "*.flac" -print0 | while read -d $'\0' a; do
  
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
+
=== Usage ===
  
$ find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
+
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.
  
== Packages ==
+
=== 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|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|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|flac2mp3-bash}} - Bash script to convert Flac to Mp3 easily.
 +
 +
== Graphical applications ==
 +
 +
*{{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}}}}
  
 
== See also ==
 
== See also ==
  
 
* https://www.xiph.org/flac/
 
* https://www.xiph.org/flac/
* https://en.wikipedia.org/wiki/FLAC
+
* [[wikipedia:FLAC]]
 
* http://lame.sourceforge.net/
 
* http://lame.sourceforge.net/
 
* http://wiki.hydrogenaudio.org/index.php?title=Flac - More information on FLAC.
 
* 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