<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.tordeu.com &#187; flac</title>
	<atom:link href="http://blog.tordeu.com/?feed=rss2&#038;tag=flac" rel="self" type="application/rss+xml" />
	<link>http://blog.tordeu.com</link>
	<description>a blog about stuff</description>
	<lastBuildDate>Thu, 25 Apr 2013 07:03:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>Converting FLAC to MP3 (including Tags) on command line</title>
		<link>http://blog.tordeu.com/?p=184</link>
		<comments>http://blog.tordeu.com/?p=184#comments</comments>
		<pubDate>Wed, 23 Nov 2011 07:29:14 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[flac]]></category>
		<category><![CDATA[metaflac]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=184</guid>
		<description><![CDATA[ (Download the script from this article ) When converting FLAC files to MP3, the first problem you will notice is that lame does not support FLAC files. This might now seem like big problem, because you can easily decode FLAC files using the command line tool flac and then use the decoded file with lame. <a href='http://blog.tordeu.com/?p=184'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p style="text-align: right;"> (Download the script from this article <a href="http://blog.tordeu.com/downloads/2">flac2mp3.sh</a>)</p>
<p>When converting FLAC files to MP3, the first problem you will notice is that lame does not support FLAC files. This might now seem like big problem, because you can easily decode FLAC files using the command line tool flac and then use the decoded file with lame.</p>
<h2>The Problem</h2>
<p>The real problem is that you will loose your metadata in this process, so the tags you added to your FLAC won&#8217;t be copied to the MP3 file. While you could &#8220;easily&#8221; add them by hand, it is pretty annoying to do that even if you only want to convert a handful of files.</p>
<p><span id="more-184"></span></p>
<h2>The solution</h2>
<p>The solution is to extract the metadata from the FLAC file using metaflac and to use the options lame provides to inject the tags into the MP3 file when calling lame.</p>
<p>Note:</p>
<p>This section is merely provided to explain the steps involved. You do not need to read this nor do you need to do this by hand, since I offer you a small script that will do these steps for you (look at the very first line of the article or below under &#8220;Download&#8221; to download the script).</p>
<h3>Step 1 &#8211; Extract the Metadata</h3>
<p>To extract a single tag, you can use metaflac like this:</p>
<pre>metaflac --show-tag=&lt;tag&gt; &lt;file&gt;</pre>
<p>To extract the artist of the track from the file named &#8220;1 &#8211; Let It In.flac&#8221;, you could call:</p>
<pre>metaflac --show-tag=ARTIST "1 - Let It In.flac"</pre>
<p>&nbsp;</p>
<p>In general you will get a result like this:</p>
<pre>&lt;TAG&gt;=&lt;VALUE&gt;</pre>
<p>In the mentioned example you might get the following:</p>
<pre>ARTIST=Josh Woodward</pre>
<h3></h3>
<h3> Step 2 &#8211; Fomat the Metadata</h3>
<p>We only need the name of the artist, so we need to get rid of  &#8220;ARTIST=&#8221;. This can be easily achieved with sed.  The general way of replacing something with sed is this:</p>
<pre>sed 's/OLD/NEW/'</pre>
<p>OLD is the part that you want to replace and NEW is the part that you want to replace it with. What we want to replace is &#8220;ARTIST=&#8221; (which is our OLD) and what we want to replace it with is nothing (which will essentially remove OLD), so our NEW is empty. And this is what we get:</p>
<pre>sed 's/ARTIST=//'</pre>
<p>But that would only work for the ARTIST tag. A general solution would be to remove anything up to and including the equal sign. I won&#8217;t go into detail on this one, but this would be achieved by setting OLD to &#8220;.*=&#8221; and the line would look like this:</p>
<pre>sed 's/.*=//'</pre>
<p>To make this work, we need to we need to pass the output of metaflac to our sed command, which we would do like this:</p>
<pre>metaflac --show-tag=ARTIST "1 - Let It In.flac" | sed 's/.*=//'</pre>
<p>This way the output of metaflac (&#8220;ARTIST=Josh Woodward&#8221;) is send to our sed command, which then removes the &#8220;ARTIST=&#8221; part and thus we get the output:</p>
<pre>Josh Woodward</pre>
<p>&nbsp;</p>
<h3>Step 3 &#8211; Store the tag</h3>
<p>While the line from Step 2 works, it merely prints the tag. What we need to do is to store the tag somehow, so that we can use it again later for adding the tag to the MP3 file. To store the output of a command in a variable, you generally type:</p>
<p>&lt;var&gt;=`&lt;command&gt;`</p>
<p>(Those are not single quotes in this line, but backticks/grave accents) This will execute &lt;command&gt; and then store the output to the variable &lt;var&gt;.</p>
<p>In this example we could do this:</p>
<p>ARTIST=`metaflac &#8211;show-tag=ARTIST &#8220;1 &#8211; Let It In.flac&#8221; | sed &#8216;s/.*=//&#8217;`</p>
<p>This would execute out metaflac command and save the output (&#8220;Josh Woodward&#8221;) in the variable ARTIST.</p>
<p>We can test if everything worked by printing the value of ARTIST:</p>
<pre>echo $ARTIST</pre>
<p>which should now give us:</p>
<pre>Josh Woodward</pre>
<p>&nbsp;</p>
<h3>Step 4 &#8211; Passing the tags to lame</h3>
<p>In general, calling lame (and using VBR mode) would work like this:</p>
<pre>lame -V &lt;oldfile&gt; &lt;mp3file&gt;</pre>
<p>If you want lame to save metadata to the mp3 file, you can use several options do to this. To continue our example, let&#8217;s assume we want to add the name of the artist (that we have stored in the variable ARTIST in the last step) to the new MP3 file. This can be done with the &#8211;ta option:</p>
<pre>lame -V --ta "$ARTIST" &lt;oldfile&gt; &lt;mp3file&gt;</pre>
<p>In a similar way, we can add other tags to the mp3 file as well. Here is a small table that shows you the options lame offers to add metadata, the name of the FLAC tag that theese options correspond to and what they mean.</p>
<table border="1">
<tbody>
<tr>
<th>FLAC tag</th>
<th>Lame option</th>
<th>Description</th>
</tr>
<tr>
<td>TITLE</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--tt</div></div>
</td>
<td>The title of the track</td>
</tr>
<tr>
<td>ARTIST</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--ta</div></div>
</td>
<td>The title artist</td>
</tr>
<tr>
<td>ALBUM</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--tl</div></div>
</td>
<td>The album name</td>
</tr>
<tr>
<td>DATE</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--ty</div></div>
</td>
<td>The year of the album</td>
</tr>
<tr>
<td>COMMENT</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--tc</div></div>
</td>
<td>a comment for this track</td>
</tr>
<tr>
<td>TRACKNUMBER, TRACKTOTAL</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--tn</div></div>
</td>
<td>The number of the track and the total number of the tracks</td>
</tr>
<tr>
<td>GENRE</td>
<td>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--tg</div></div>
</td>
<td>the genre of the track</td>
</tr>
</tbody>
</table>
<h2></h2>
<h3>Step 5 &#8211; Passing the FLAC to lame</h3>
<p>We have not solved the original problem yet: you can not pass a flac file to lame. We could decode the FLAC to WAV and then pass the WAV file to lame, but we don&#8217;t need do really create a WAV file, because we can just decode the FLAC and pass the decoded data directly to lame. To do this, we need to call flac with two options:</p>
<ul>
<li>-d to tell it we want to decode a flac file</li>
<li>-c, so that the decoded data is not written to a file, but to stdout</li>
</ul>
<p>Then, we can tell lame that it should read the data from stdin instead of from a file, which can be done by providing a dash for the &lt;oldfile&gt; parameter. Now we can connect them both with a pipe and the decoded flac data will directly flow into our lame command:</p>
<pre>flac -c -d &lt;flacfile&gt;| lame - &lt;mp3file&gt;</pre>
<p>In our example, we would get something like this (which includes adding the ARTIST tag and a VBR quality of 6):</p>
<pre>flac -c -d "1 - Let It In.flac" | lame -V 6 --ta "$ARTIST" - "1 - Let It In.mp3"</pre>
<p>Now that the basic  steps involved are covered, we can let a script do the work.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>The Script</h2>
<p>I wrote a small script that converts a FLAC file to MP3 while preserving  the following tags (=all the tags listed in &#8220;Step 4&#8243; above):</p>
<ul>
<li>Track Title</li>
<li>Track Artist</li>
<li>Album Title</li>
<li>Year</li>
<li>Comment</li>
<li>Tracknumber</li>
<li>Total number of tracks</li>
<li>Genre</li>
</ul>
<h3>Download</h3>
<p>You can download the script here: <a href="http://blog.tordeu.com/downloads/2">flac2mp3.sh</a></p>
<h3>Usage</h3>
<p>The script takes three arguments:</p>
<ul>
<li>The VBR quality (This corresponds to the -V option of lame, which means that you can use a number between 0 and 9, with smaller numbers meaning higher quality and bigger files). If you omit this option, -V 4 will be used.</li>
<li>The filename of the FLAC file you want to convert</li>
<li>The filename of the new MP3 file that you want to create.</li>
<li>You can (but don&#8217;t have to) add &#8220;mtime&#8221; as the fourth argument in which case the script will not start the conversion if the MP3 file is newer than the FLAC file, which indicates that a conversion is not necessary. (If you don&#8217;t add &#8220;mtime&#8221;, the script will convert the files no matter what)</li>
</ul>
<p>Example:</p>
<p>If you want to convert the file &#8220;1 &#8211; Let It In.flac&#8221; to &#8220;1 &#8211; Let It In.mp3&#8243; with a VBR quality level of 3, you would call the script like this:</p>
<pre>flac2mp3.sh 3 "1 - Let It In.flac" "1 - Let It In.mp3"</pre>
<pre>flac2mp3.sh 3 "1 - Let It In.flac" "1 - Let It In.mp3" mtime</pre>
<h3>Source</h3>
<p>The script generally works as described under &#8220;The Solution&#8221;, but instead of repeating the line from &#8220;Step 3&#8243; for every tag, it uses a more elegant approach (which in this case just means less code). But instead of posting the complete source code (which you can look at by download the script), let me just show you  the meat of the script (extracting the 8 tags and passing them to lame for conversion):</p>
<pre>for tag in TITLE ARTIST ALBUM DATE COMMENT TRACKNUMBER TRACKTOTAL GENRE; do
    eval "$tag=\"`metaflac --show-tag=$tag "$FLAC" | sed 's/.*=//'`\""
done

flac -cd "$FLAC" | lame -V "$V" --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tc "$COMMENT" --tn "$TRACKNUMBER/$TRACKTOTAL" --tg "$GENRE" - "$MP3"</pre>
<p>&nbsp;</p>
<h2>Update (2012-06-05)</h2>
<p>I have updated the script a little. It&#8217;s now possible to append a &#8220;mtime&#8221; after the three arguments. When adding &#8220;mtime&#8221;, the script will only start the conversion if the MP3 file does not exist yet or the FLAC file is newer than the MP3 file.</p>
<p>This will avoid unnecessary conversions in case the MP3 is newer than the FLAC file, which indicates it is up-to-date. This is useful when using this script to somehow manage your library as it will reduce a large number of conversions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=184</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>&#8220;ERROR: Input file  is not a supported format&#8221; when using oggenc to convert a FLAC to OGG Vorbis</title>
		<link>http://blog.tordeu.com/?p=61</link>
		<comments>http://blog.tordeu.com/?p=61#comments</comments>
		<pubDate>Wed, 16 Nov 2011 13:53:03 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[flac]]></category>
		<category><![CDATA[metaflac]]></category>
		<category><![CDATA[ogg]]></category>
		<category><![CDATA[oggenc]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=61</guid>
		<description><![CDATA[(Download the script here: ) Problem When using oggenc to convert a FLAC file to OGG Vorbis using oggenc, you might encounter the following error: "ERROR: Input file &#60;file&#62; is not a supported format" Explanation One possible reason for this can be an ID3 tag in the flac file. This can happen if the program <a href='http://blog.tordeu.com/?p=61'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p style="text-align: right;">(Download the script here: <a href="http://blog.tordeu.com/downloads/1">reflac.sh</a>)</p>
<h2>Problem</h2>
<p>When using oggenc to convert a FLAC file to OGG Vorbis using oggenc, you might encounter the following error:</p>
<pre>"ERROR: Input file &lt;file&gt; is not a supported format"</pre>
<h2>Explanation</h2>
<p>One possible reason for this can be an ID3 tag in the flac file. This can happen if the program you used to create your FLAC files uses ID3 tags instead of native FLAC tags to store metadata.</p>
<p><span id="more-61"></span>You can confirm this by typing the following:</p>
<pre>flac --ogg enc &lt;file&gt;</pre>
<p>which would then output something like this:</p>
<pre>ERROR: input file &lt;file&gt; has an ID3v2 tag</pre>
<p>If this is the problem you are having, then the following should help you</p>
<h2>Bad &#8220;Solutions&#8221;</h2>
<p>The &#8220;solutions&#8221; I found on the web have drawbacks that I could not accept.</p>
<p>The first &#8220;solution&#8221; is to just rip the CD again with a different software or different options. This is annoying, because you need to go through the whole process again and ripping a CD can take a long time, depending on how you do it.</p>
<p>Another &#8220;solution&#8221; is to convert your FLACs to WAV files and then either to OGG or back to FLAC (to get &#8220;repaired&#8221; FLACs you can now use with oggenc). The drawback of this approach is that all the metadata gets lost during the conversion from FLAC to WAV, because WAV files can not hold metadata.</p>
<p>I am using quotes (&#8220;solution&#8221;) to make clear that I don&#8217;t really consider them solutions to the problem, because the amount of work that would be required makes them practically useless for large collections.</p>
<h2>Solution</h2>
<p>The &#8220;only&#8221; problem with the second &#8220;solution&#8221; is the lost metadata. Other than that the solution works fine. Fortunately it is possible to solve the problem by using a command line tool that is already installed with flac, but that most people don&#8217;t seem to know about: metaflac.</p>
<p>Metaflac can extract metadata from a FLAC file and add metadata to a FLAC file as well. Therefore, all we need to do is:</p>
<ol>
<li>extract the metadata using metaflac</li>
<li>decode the FLAC to a WAV file</li>
<li>encode the WAV file back to FLAC</li>
<li>import the metadata using metaflac into the new FLAC file</li>
</ol>
<h2>Using a script to &#8220;repair&#8221; the FLACs</h2>
<p>Because this is pretty annoying by hand, I wrote a script named &#8220;reflac&#8221;. You can either use the script in a terminal or &#8220;integrate&#8221; it with Nautilus.</p>
<h3>Downloading the script</h3>
<p>You can download the script here: <a href="http://blog.tordeu.com/downloads/1">reflac.sh</a></p>
<h3>Using the script from the command line</h3>
<p>The script can be used like this:</p>
<pre>reflac.sh &lt;flac file&gt;...</pre>
<p>Example 1:</p>
<pre>refla.shc foo.flac bar.flac</pre>
<p>Example 2:</p>
<pre>reflac.sh *.flac</pre>
<p>It just goes through the four steps for every file you throw at it.</p>
<h3>Adding the script to Nautilus</h3>
<p>If you don&#8217;t like the terminal that much, you can also use the script directly from Nautilus. Just copy the script to the folder ~/.gnome2/nautilus-scripts/ (To copy the script to this location from within Nautilus, download the script</p>
<p>Just as a disclaimer: This is a quick &amp; dirty script I wrote and I don&#8217;t really see something in it that could go horribly wrong. Nevertheless, there is ALWAYS a risk when using software, so backing up your data is YOUR responsibility. And always keep in mind: &#8220;If it&#8217;s worth saving, it&#8217;s worth backing up!&#8221;.</p>
<h2>Download</h2>
<p>Download the script here: <a href="http://blog.tordeu.com/downloads/1">reflac.sh</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=61</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
