Автор Гілка: MP3 > Nokia Audiobooks  (Прочитано 2335 раз)

Відсутній Paul Carroty

  • Кореспондент
  • ***
  • дописів: 144
  • Карма: +0/-0
MP3 > Nokia Audiobooks
« : 2012-08-16 18:40:00 »
#!/bin/bash
# Hack to avoid using the Nokia Audiobooks application
# No hassle with Windoze plus conversion is faster too

# The script just takes one single compulsory parameter: "book_title"
# Execute it in a directory contaning the mp3s plus a single cover jpg
# Any file names are fine
# Output files are in the folder ./book_title
# This folder can be moved as-is to the Audiobooks directory in the phone

# Change the parameters to amrwb-encoder if you want more compression, etc
# Change the chapter values by hand in the index file if you're a perfectionist

# Note: some sections are optional in theory (chapters, content_info),
# but removing some of them breaks the program

# Required packages (Debian): mpg321, resample, amrwb, mplayer
# Required packages (Fedora): mpg321, resample(sox), amrwb-tools, mplayer

# Courtesy of Fezouro (Ubuntu forums)
# Version 0.2

if [ $# -lt 1 ]; then
echo "Book title parameter required"; exit;
fi

jpegs=`find . -maxdepth 1 -iname "*jpg"| wc -l`

if [ $jpegs -ne 1 ]; then
echo "Only one single cover JPG image can be present"; exit;
fi

echo "Converting: $1"

index="/tmp/index.inx"
tmp="/tmp/tmp.wav"
tmp2="/tmp/tmp2.wav"
rm -f $tmp $tmp2 $index
rm -rf "$1"
mkdir "$1";
echo "#BOOK" > $index
echo "$1;" >> $index
echo "#PIC" >> $index
echo "$1.jpg;" >> $index
echo "#TRACKS" >> $index

# number of files to process
count=`find . -maxdepth 1 -iname "*mp3"| wc -l`

start=`date +%s`

current=1
find . -maxdepth 1 -iname "*mp3" | sort | while read i; do
time_s=`date +%s`
j=`basename "$i" .mp3`
base=`basename "$j" .MP3`
out="$base.awb"
tput el1
echo -n -e "\r[$current/$count] $out"
# convert mp3 to wav
mpg321 -q -w $tmp "$i" > /dev/null
# resample to awb rate
resample -to 16000 -terse $tmp $tmp2 > /dev/null
# file length in seconds
lengthd=`mplayer -vo null -ao null -frames 0 -identify $tmp2 2>/dev/null | grep ID_LENGTH | awk -F= '{print $2}'`
# remove decimals
length=`echo "scale=0;$lengthd/1" | bc`
# convert wav to awb
amrwb-encoder -dtx 7 $tmp2 "$1/$out" > /dev/null 2>&1
# update index file
echo "$out:$length;" >> $index

current=$(($current+1))
rm -f $tmp $tmp2
done

echo "#CHAPTERS" >> $index
chapter=1;
find . -maxdepth 1 -iname "*mp3" | sort | while read i; do
j=`basename "$i" .mp3`
base=`basename "$j" .MP3`
out="$base.awb"
echo "$out:0s:$chapter:$base;" >> $index
chapter=$(($chapter+1))
done
echo "#VERSION" >> $index
echo "0.7;" >> $index

echo "#CONTENT_INFO" >> $index
echo "NokiaAudiobookManagerVersion=Hack by Fezouro;" >> $index
echo "CodecMode=7;" >> $index
echo "CodecBitRate=23050;" >> $index
echo "DTX=1;" >> $index
echo "SBRA=0;" >> $index
echo "SBRA_Rate=900;" >> $index
echo "InputFormat=2;" >> $index

# index must be in little endian UTF-16 unicode format
iconv -t UTF-16 $index > "$1/$1.inx"

# copy book cover with right name
cover=`find . -maxdepth 1 -iname \*.jpg`
cp "$cover" "$1/$1.jpg"

rm -f $tmp $tmp2 $index

elapsed=$(( `date +%s` - $start))
tput el1
echo -e "\rDone in $elapsed seconds"


# You can also use sox:
#sox -s -c 1 -r 16000 -t raw -2 - -t amr-wb -C $level "$outdir"/"$outdir"_${nr}.awb vol 1.2 contrast