Linux: Audio Files Conversion and Audio Track Extraction

Time to time it is necessary to convert an audio file or extract audio track from a video file. Use the following steps to move forward.

ffmpeg -i SOURCE -vn -ar 44100 -ac 2 -ab 320k -f mp3 OUTPUT.mp3

where the SOURCE is any audio or video file and OUTPUT is the name of the output file.

FLAC to MP3

I know it sounds wierd. Why would anyone did that? Well, the following script is just in case necessary, from whatever the reasons are 🙂
Make sure the following prerequisities are adhered.

emerge -av media-libs/flac media-sound/id3 media-sound/lame

And create a script as follows:

#!/bin/sh
for a in *.flac; do
    OUTF=${a%.flac}.mp3
    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)
    flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
    id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"
done

Split a Single FLAC File into Separated Song Files

Make sure the following prerequisity is adhered:

emerge -vp media-libs/flac media-sound/wavpack
emerge -vp app-cdr/cuetools media-sound/shntool

In Gentoo, it is necessary to create a symlink to cuetag.sh script which is installed in a “hidden” directory. Log in as root and make the script executable and available for everyone:

chmod +x /usr/share/doc/cuetools-1.3.1/extras/cuetag.sh
ln -s /usr/share/doc/cuetools-1.3.1/extras/cuetag.sh /usr/bin/cuetag

The following file (taken from here) splits the single *.flac file according to its *.cue file into separated FLAC and MP3 files (these are converted after the single FLAC file extraction is successfully done).

#!/bin/bash
#
# splitflac2mp3
#
# split monolithic flac with cue, and then tag split files according to cue file - and convert to mp3
# copy this file and run it in the directory containing the monolithic flac and cue
#
# you need zenity package for notifications.
# please note that you can put this script in ~/.gnome2/nautilus-scripts
# and it will show up in right-click menu in any folder in gnome.
#
# modify the lame options to your preference example change -b 320 to -b 128 or -b 192 or -b 256
# LAME_OPTS="--vbr-new -V 0 -b 256"
# LAME_OPTS="-V 0 --vbr-new"
LAME_OPTS="-b 320 -h --cbr"

old_IFS=${IFS}
IFS='
'

# when running from nautilus-scripts, it useful to find the current folder
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
  dir="$base"
else
  while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
  dir="$base/$1"
fi

if [ "$dir" != "" ]; then
  cd "$dir"
fi

splitdir="Split Tracks"
rm -f -s "$splitdir"
mkdir "$splitdir"

# copy the cue file
cuefiles=`find . -type f -regex '^.+.cue$' | sort`
thecue=""
for file in ${cuefiles}
do
  fn=$(readlink -f "$file")
  if [ -f "$fn" ]; then
    thecue=$fn
    cp "$fn" "$splitdir/copy.cue"
    break
  fi
done

shnsplit -o flac -f "$thecue" -t "%n - %t" -d "$splitdir" *.flac 
cd "$splitdir"
# sometimes we end up with a garbage file called 00 - pregap.flac:
rm -f "00 - pregap.flac"
cuetag copy.cue *.flac

id3v2=$(which id3v2)

files=`find . -type f -regex '^.+.flac$' | sort`

declare -i nn=0
for file in ${files}
do
  fn=$(readlink -f "$file")
  vars=( `metaflac --no-utf8-convert --export-tags-to=- "${fn}"` )

  for N_vars in ${!vars[@]}
  do
    export "$(echo "${vars[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${vars[${N_vars}]#*=}"
  done

  dest=`echo "$fn"|sed -e 's/.flac$/.mp3/'`

  flac -dc "${file}" |
    lame --ignore-tag-errors --add-id3v2 ${LAME_OPTS} 
        ${artist:+--ta} ${artist} 
        ${tracknumber:+--tn} ${tracknumber} 
        ${title:+--tt} ${title} 
        ${album:+--tl} ${album} 
        ${date:+--ty} ${date} 
        ${genre:+--tg} ${genre} 
        ${comment:+--tc} ${comment} 
        - "${dest}"
 
    [[ -x ${id3v2} ]] && ${id3v2} 
        ${artist:+--artist} ${artist} 
        ${tracknumber:+--track} ${tracknumber} 
        ${title:+--song} ${title} 
        ${album:+--album} ${album} 
        ${date:+--year} ${date} 
        ${genre:+--genre} ${genre} 
        ${comment:+--comment} ${comment} 
        "${dest}"

  let nn=nn+1
done
rm -f copy.cue

zenity --notification  --text "Finished splitting and converting flac to mp3.${IFS}Created ${nn} mp3 files."
#zenity --info --text "Done!"
IFS=${old_IFS}

Once saved into the script file, don’t forget to make it executable.

chmod +x splitflac2mp3.sh

Copy the splitflac2mp3.sh file into the directory which contains the cue and flac files, and run

./splitflac2mp3.sh

Dealing with Damaged/Corrupted FLAC Files

The only solution, which I found out was to “convert” the original FLAC file (see above how to convert files) into a new one (with no compression applied). Based on the “missing information”, the new file size will decrease (and can cause the song unlistenable).

If you’re using Windows, you can try a cool software named MediaMonkey for the same.

Advertisements
This entry was posted in Audio/Video, Linux and tagged , , , , , , . Bookmark the permalink.