mp3 to m4b
#!/bin/bash
## The iPod Audiobook Encoder for Linux.
## Version 1.0.5
## Created by Jaroslaw Czarniak, 21 May, 2006.
## Released under the GPL.
##
##
## Changes:
## 1.0.2
## + Files split function added
##
## 1.0.3
## e mpg123 was changed to madplay due some errors
##
## 1.0.4
## + other types support
## r wavmerge required
## r mplayer required
## + automatic batches execution
## + output files are numbered
## + input files type detection
##
## 1.0.5
## + remove merged files
## + sort files
## f decode none-mp3 files
##
## To DO:
## add cover arts
##
########################################################
# Functions
function deps()
{
check1=`which faac`
if [ "$check1" = "" ]
then
echo "* faac is missing...."
echo "Check on http://www.audiocoding.com/"
echo ""
cont="n"
fi
check2=`which id3info`
if [ "$check2" = "" ]
then
echo "* id3info is missing...."
echo "Check on http://tangent.org/index.pl?node=libID3"
echo ""
cont="n"
fi
check3=`which mp3wrap`
if [ "$check3" = "" ]
then
echo "* mp3wrap is missing...."
echo "Check on http://mp3wrap.sourceforge.net/"
echo ""
cont="n"
fi
check4=`which madplay`
if [ "$check4" = "" ]
then
echo "* madplay is missing...."
echo "Check on http://www.underbit.com/products/mad/"
echo ""
cont="n"
fi
check5=`which id3v2`
if [ "$check5" = "" ]
then
echo "id3v2 is missing...."
echo "Check on http://id3v2.sourceforge.net/"
cont="n"
fi
check1=`which wavmerge`
if [ "$check1" = "" ]
then
echo "* wavmerge is missing...."
echo "Check on http://huli.org/wavbreaker/"
echo ""
cont="n"
fi
check1=`which mplayer`
if [ "$check1" = "" ]
then
echo "* mplayer is missing...."
echo "Check on http://www.mplayerhq.hu/"
echo ""
cont="n"
fi
if [ "$cont" = "n" ]
then
space
echo "Missing dependencies..."
echo "Can't continue..."
exit 0
fi
}
function clearmeta()
{
ALBUM=""
ARTIST=""
COMMENT=""
YEAR=""
TITLE=""
GENRE=""
TRACKNUMBER=""
}
function space()
{
echo ""
echo ""
}
function getmetamp3()
{
clearmeta
test -z "$(which id3info)" && return
echo "Getting metadata from MP3"
ALBUM="$(id3info "$1" |grep ^=== TALB|head -1|sed 's/.*: //g')"
ARTIST="$(id3info "$1" |grep ^=== TPE1|head -1|sed 's/.*: //g')"
COMMENT="$(id3info "$1" |grep ^=== COMM|head -1|sed 's/.*: //g')"
YEAR="$(id3info "$1" |grep ^=== TYER|head -1|sed 's/.*: //g')"
TITLE="$(id3info "$1" |grep ^=== TIT2|head -1|sed 's/.*: //g')"
GENRE="$(id3info "$1" |grep ^=== TCON|head -1|sed 's/.*: //g')"
TRACKNUMBER="$(id3info "$1" |grep ^=== TRCK|head -1|sed 's/.*: //g')"
}
function countall()
{
for pliki in *
do
rozmiarpl=`du -b "$pliki" |tr -cs '[a-zA-Z0-9._()]' '[n*]'|head -n 1`
total=$[total + rozmiarpl]
done
echo "Total files size in this directory is $total bytes"
}
function getdata()
{
space
sugestia=`pwd | mawk '{ print test[split($0,test,"/")];}'|tr -cs '[a-zA-Z0-9n*]' '.'`
echo "Enter output file name (without extension) ($sugestia)"
read plik
#if [ -e "$outputfile" ]
#then
# echo "File exist! Suggestions enabled"
# getmetamp3 "$outputfile"
#fi
echo "Enter title (${TITLE})"
read tytul
echo "Enter artist (${ARTIST})"
read autor
#echo "Enter album (${ALBUM})"
#read album
album="$tytul"
echo "Enter year (${YEAR})"
read rok
#sleep 1
#echo "If you want to add cover art to your audiobook, please"
#echo "copy it to just created "cover" directory"
#echo "and name it as "cover.jpg" "
#(or gif or png)
#echo "Also you can copy it there before running this script."
#sleep 3
#echo "3"
#sleep 3
#echo "2"
#sleep 3
#echo "1"
#space
#read track
coment="From Sir_Yaro2006 Collection"
}
function splitfiles()
{
splitval="150000000"
if [ "$typ" = "o" ]
then
splitval="1850000000"
fi
echo "Default split value for mp3 is about 150MB (150000000 bytes)"
echo "and for other files is 1.85GB (1850000000 bytes) because they're"
echo "decoded to wav format and its up limit is 2048MB."
countall
echo ""
echo "(current split value is $splitval bytes)"
echo ""
echo "Would you like to change it? (y/n) [n]"
read question
if [ "$question" = "y" ]
then
echo "Enter new value in bytes and press enter"
read splitval
fi
#
# Split start
#
licznik="1"
suma="0"
mkdir "batch_1"
echo ""
echo "Batch no 1 created"
if [ "$typ" = "m" ]
then
for file in *.mp3
do
rozmiar=`du -b "$file" |tr -cs '[a-zA-Z0-9.()_]' '[n*]'|head -n 1`
suma=$[suma + rozmiar]
if [ "$suma" -gt "$splitval" ]
then
licznik=$[licznik + 1]
echo "Batch no $licznik created"
mkdir "batch_$licznik"
suma=0
suma=$[suma + rozmiar]
fi
mv "$file" "batch_$licznik"
done
else
for file in *.wav
do
rozmiar=`du -b "$file" |tr -cs '[a-zA-Z0-9.()_]' '[n*]'|head -n 1`
suma=$[suma + rozmiar]
if [ "$suma" -gt "$splitval" ]
then
licznik=$[licznik + 1]
echo "Batch no $licznik created"
mkdir "batch_$licznik"
suma=0
suma=$[suma + rozmiar]
fi
mv "$file" "batch_$licznik"
done
fi
echo ""
echo "$licznik batches created."
space
echo "Done."
#
# Split end
#
#
# Encode loop
#
for batch in `seq 1 $licznik`;
do
space
echo "======================"
echo "Batch no $batch in progress"
echo "======================"
# echo ""
cd batch_$batch
checktype $batch $licznik
cd ..
rmdir "batch_$batch"
done
}
function checktype()
{
#getdata
space
if [ "$typ" = "m" ]
then
encodemp3 $1 $2
else
encodeothers $1 $2
fi
}
function encodemp3()
{
outputfile=${plik}_MP3WRAP.mp3
if [ `ls -1 *mp3| wc -l` -gt "1" ]
then
mp3wrap "$plik.mp3" *mp3
else
echo "Only one file found! Making file copy"
cp *mp3 "$outputfile"
fi
space
id3v2 -D "$outputfile"
track="$1/$2"
id3v2 --artist "$autor" --album "$album" --song "$tytul" --comment "":"$coment":"PL" --genre 101 --year "$rok" --track "$track" "$outputfile"
space
m4bname="$plik.cz.$1.m4b"
getmetamp3 "$outputfile"
space
echo "================================="
id3v2 --list "$outputfile"
echo "================================="
wielkosc=`du -b "$outputfile" |tr -cs '[a-zA-Z0-9._()]' '[n*]'|head -n 1`
if [ "$wielkosc" -gt "160000000" ]
then
space
echo "$outputfile seems to be to large to convert it to wav file."
echo "due header size limitation wav files can't be bigger than 2048MB."
echo ""
wielkosc2=`du -h "$outputfile"|tr -cs '[a-zA-Z0-9.()_]' '[n*]'|head -n 1`
wielkosc3=`du -h "$outputfile"|tr -cs '[a-ln-zA-LN-Z0-9.()_]' '[n*]'|head -n 1`
echo "Your mp3 file size is $wielkosc2, and wav size will be approximately"
echo "11x bigger, which is ~$(($wielkosc3*11))MB"
echo ""
echo "I recommend you split your files into few batches and convert then separately"
space
echo "Continue anyway? (y/n) [n]"
read odp
if [ "$odp" != "y" ]
then
odp="n"
fi
fi
if [ $odp = "y" ]
then
space
nice -10 madplay -q -o wave:- "$outputfile" | nice -10 faac -w --artist "${ARTIST}" --title "${TITLE} cz.$track" --album "${ALBUM}" --year "${YEAR}" --comment "${COMMENT}" --genre "Speech" --track "${TRACKNUMBER}" -q 80 -o "$m4bname" -
# nice -10 mpg123 --rate 44100 -m -w - "$outputfile" | nice -10 faac -w --artist "${ARTIST}" --title "${TITLE} cz.$track" --album "${ALBUM}" --year "${YEAR}" --comment "${COMMENT}" --genre "Speech" --track "${TRACKNUMBER}" -q 80 -o "$m4bname" -
# --cover-art "../cover/cover.jpg"
rm -f "$outputfile"
echo "File "$outputfile" deleted"
mv "$m4bname" ../Audiobook
echo "File "$m4bname" moved to "../Audiobook" located in parent directory"
mv *mp3 ..
echo "Source mp3 files moved to parent directory"
space
echo "Batch $1 done"
space
fi
}
function encodeothers()
{
echo "Merging wave files"
echo "Please wait..."
echo ""
wavmerge -o "merged.wav" *wav
ls -l "merged.wav"
echo "Merge done"
m4bname="$plik.cz.$1.m4b"
track="$1/$2"
faac -w --artist "$autor" --title "$tytul cz.$track" --album "$album" --year "$rok" --comment "$coment" --genre "Speech" --track "$track" -q 80 -o "$m4bname" "merged.wav"
# --cover-art "../cover/cover.jpg"
#rm -f "merged.wav"
#echo "File merged.wav deleted"
mv "$m4bname" ../Audiobook
echo "File "$m4bname" moved to "../Audiobook" located in parent directory"
rm -f *wav
echo "Wav files deleted"
mv * ..
echo "Source files moved to parent directory"
space
echo "Batch $1 done"
space
}
function decode()
{
for file in *.*
do
echo "================================="
echo "Processing $file file"
echo "================================="
echo ""
mplayer -really-quiet -channels 1 -nojoystick -nolirc -nortc -vo null -vc dummy -af resample=44100 -ao pcm:waveheader:file=$file.wav "$file"
done
space
echo "================================="
echo " Done"
echo "================================="
space
}
########################################################
## Functions end
########################################################
# Variables
clear
odp="y"
cont="y"
########################################################
# Start
deps
alias ls='ls'
echo "Welcome to the iPod Audiobook Encoder for Linux"
echo "Version 1.0.5"
echo "Created by Jaroslaw Czarniak, 21 May, 2006."
echo "Released under the GPL."
rename 'y/MP /mp./' *
getdata
space
typ=`ls -1 *.* | grep -o '.[^.]*$'|head -n 1`
if [ "$1" = "o" ]
then
typ=".other"
echo "Files type forced!"
space
fi
if [ "$typ" = ".mp3" ]
then
typ="m"
echo "Mp3 files detected."
echo "You may force to encode files as none mp3"
echo "by adding "o" as a parameter of the script"
space
else
typ="o"
echo "Files other than mp3 detected."
echo -e "You may need a E[033;01;32m_LOT_E[0m (gigabytes) of disk space"
echo -e "Decoding files... E[033;01;32mPlease wait...E[0m"
echo ""
echo -e "E[033;01;32mYour interaction will be required shortlyE[0m"
space
decode
space
space
fi
echo "Split files to batches? (y/n) [y]"
read split
mkdir "Audiobook"
#mkdir "cover"
if [ "$split" != "n" ]
then
# split
splitfiles
space
echo "All done!"
space
else
# don't split
checktype
fi