iTunes Mp4 Mp3 , Mkv , flac LibAV avconv ffmpeg

This used to be a part of a post about iTunes and Musicbrainz Picard.
iTunes being a popular Media player with reasonable database and organisation features.
Picard is a popular media tagger scanner for many formats.

I tend to use Picard as a preprocessor for iTunes, it essentially lets me ensure things are right with my media i also like it renaming files for me usually track number trackname. I also set the save destination to an iTunes folder called ‘Automatically add to iTunes’. Even if I make no changes to a media file it happily moves it into iTunes “inbox” – from now on i’m going to refer to that folder as iTunes inbox.

The nice thing really is no matter where the media is when i open it in Picard it finishes up stored in the iTunes media folders unless it doesn’t.

iTunes will not file everything and sometimes media needs converting. Flac and Mkv files are 2 unsupported formats.

Music converter is on the app store and you can drag and drop one file at a time and transfer into iTunes. What a pain in the butt, it also advertises a couple of other programs as it converts and produces 192kbs files as its output. This isn’t ideal and you need to buy the Pro version in order to work with any speed.

There has to be a better way, and there is with libav.

Libav is a fork of ffmpeg an open source project generally the 2 programs can be used with the same command line arguments.  avconv and ffmpeg are pretty close to being the same program.  The abilities of the two programs depends on how they were compiled. Something you can do yourself if there is options missing that you need.  For Macs you can get versions from homebrew or macports for linux *debian based.

sudo apt-get install libav

can do the trick my nas runs debian but its an arm system with not much ram and a slowish processor so its faster to convert files on my mac rather than on the nas.

OSX and Linux is very similar, practically identical when it comes to the terminal so generally any terminal based stuff is available on both platforms. IF you run Windows you can run Linux in a virtual machine, with or without a desktop. You can google how to do that, right.

Back to the subject in hand converting with LibAV or strictly speaking avconv or ffmpeg avconv is probably the best choice but they both take the same arguments on the command line so if you use ffmpeg just write that instead of avconv and it will probably work. Probably because some versions are built without support for some options. If you don’t have the options you need then you can build it yourself.

Flac files 

Flac is a lossless format for music its compressed but complete, unfortunately iTunes doesn’t recognise it as a format it can handle.

For a single file this works

avconv -i [FileName.flac] -c:a libmp3lame -b:a 320k [FileName.mp3]

but obviously it needs the file name changing for each file converting is around 3 seconds typically

This Script will work to convert any flac file in a folder to an mp3 at 320k

nano flac2mp3 and write this into the file ctrl X and save it

#!/bin/bash

for i in *.flac;
do name=`echo “${i%.*}”`;
echo $name;
avconv -i “${i}” -ab 320k -map_metadata 0 -id3v2_version 3 “${name}”.mp3;
done

now

chmod +x flac2mp3

I copied this to /usr/bin/local/

cp flac2mp3 /usr/bin/local

it probably could be better, by running it in any directory with flac files it processes all of them with no arguments.

Here is another example of using avconv to convert mkv to mp4

Changing container without re-enconding content could not be simpler:

avconv -i input.mkv -codec copy output.mp4

It auto-detects a Matroska to MP4 container conversion based on input/output filenames.
-codec copy stream copies, or “re-muxes”, the streams from the input to the output without re-encoding. Think of it like a copy and paste.
Default stream selection behavior is to select only one stream per stream type. For example, if your input has two video streams and one audio stream then only the video stream with the largest frame size will be selected. Add -map 0 if you want to select all streams from the input.

Some containers may not support some formats. So check if your chosen container format, be it mkv, mp4 or even avi has support for all the content in your files (video, audio, subtitles, data, etc). For example, mp4 does not support SubRip subtitles (.srt files).

If you take my flac2mp3 script you can change the conversion line to convert mkv to mp4
save it as say mkv2mp4 and put it with the flac2mp3 script you made earlier.

Personally remembering command line arguments is not my idea of fun so making a simple script to do a task is easier.

A little on containers and formats

Mkv and avi are containers and perhaps it is easiest to think of them like multicore cables. A simple cable might contain 2 cores so you have the black shielding which encases a blue wire and a brown wire. There can be more wires.  Now our wires are data streams perhaps the brown might be video information and the blue audio information if there was a third core it might be subtitles an additional audio stream maybe another language or directors commentary.

The data can be in different formats too there are different ways of packing audio and video data.  There are a number of them. If the codec for the format used is available then your player should be able to render video as pictures and sound as different formats usually stereo but there are surround sound types and others.  Since video and audio are separate streams sometimes you can get audio no picture or picture no audio.

Trouble is for humans we have no clue as to what formats are in a mkv file or an avi file. we just see the container but the formats can be a little obscure. To us some avi files work fine others don’t.

VLC is a great player that handles most formats iTunes is limited. There is a possibility that even after converting mkv to mp4 it could be still in a format iTunes doesn’t understand. So use another player or decode the raw stream then pack it with a codec that iTunes can understand. This can be slow.

Codec stands for COde and DECode basically a little library that takes in one format and spits out another.

I will probably add further avconv recipes to this page later and improve that script, it is a bit brutal 🙂