#!/bin/sh
#
# This is a modified (Enhanced) version of http://www.zyzomys.com/nhk.sh.gz
#
# This shell script downloads and encodes in ogg (or mp3) the japanese lessons which are provided by the NHK ( http://www.nhk.or.jp/lesson/ )
#
# mplayer with "real audio" support, oggenc and lame are required !
#
# If you want to order the textbook for free, just ask here :
# - https://www.nhk.or.jp/lesson/mail/mail.html
#
# The latest version of this script is available here :
# - http://mastermac.free.fr/nhk.sh
#
# The mplayer syntax has changed recently, if this script doesn't work for
# your version of mplayer, look at :
# - http://mastermac.free.fr/nhk-mplayer-deprecated.sh
#
# If you want a verbose output, run the script with the debug argument : ./nhk debug
#
# If you have some questions email me : kernel[/dot/]sensei[/at/]gmail[/dot/]com
#
#
#
#In which encoding do you want to save the audio files ? Available formats are : ogg and mp3
#FORMAT="mp3"
FORMAT="ogg"
#
# Put in this list all the languages in which you want to have a course material :
LANG_LIST="fre eng bup spa swe ita ger por"
# The available languages are :
# Arabic : ara
# Bengali : ben
# Burmese : bur
# Chinese : chi
# English : eng bup # bup is for the lesson "Brush Up Your Japanese"
# French : fre
# German : ger
# Hindi : hin
# Indonesian : ind
# Italian : ita
# Korean : kor
# Malay : mal
# Persian : per
# Portuguese : por
# Russian : rus
# Spanish : spa
# Swahili : swa
# Swedish : swe
# Thai : tha
# Urdu : urd
# Vietnamese : vie
# Here begins the main script code, don't modify it unless you know what you're doing ...
if [ "$1" = "debug" ] ; then
DEBUG=1
fi
for lang in $LANG_LIST; do
echo
echo
echo "##### Proceeding : language => $lang #####"
echo
# Getting the stream's informations :
echo "Checking if the actual lesson is already recorded !"
echo
echo
if [ $DEBUG ] ; then
mplayer -nocache rtsp://stream.nhk.or.jp/lesson/${lang}.rm -frames 0 |tee ~/.nom_nhk_${lang}
else
mplayer -nocache rtsp://stream.nhk.or.jp/lesson/${lang}.rm -frames 0 &> ~/.nom_nhk_${lang}
fi
# Set the name and numeration :
echo "----- Stream Informations -----"
echo
if [ -r ~/.nom_nhk_${lang} ] ; then
NOM=$(grep " name:" ~/.nom_nhk_${lang} |cut -d: -f 2|tr -d '[:space:]'|tr -d '[:digit:]')
echo "The lesson's name is :" $NOM
NUMERO=$(grep " name:" ~/.nom_nhk_${lang} |cut -d: -f 2|tr -d '[:space:]'|tr -d '[:alpha:]')
echo "The lesson's number is :" $NUMERO
else
echo "The numeration file doesn't exist, aborting !"
exit 1
fi
if [[ -z $NOM ]] ; then
echo "The name is not valid , aborting !"
exit 1
elif [[ -z $NUMERO ]] ; then
echo "The number is not valid , aborting !"
exit 1
fi
if [ $NUMERO -lt 10 ] ; then
NUM="00$NUMERO"
elif [ $NUMERO -lt 100 ] ; then
NUM="0$NUMERO"
else
NUM=$NUMERO
fi
echo
echo "The filename is :" ${NOM}_${NUM}
LABEL=${NOM}_${NUM}
# Check if the file already exists :
if [ -e ~/NHK/${lang}/${LABEL}.??? ] ; then
echo
echo
echo "The file ${LABEL} already exists aborting !"
continue
fi
# Recording the lesson :
echo
echo
echo "Downloading the stream..."
echo
if [ $DEBUG ] ; then
mplayer -nocache rtsp://stream.nhk.or.jp/lesson/${lang}.rm -ao pcm:file=$HOME/.${lang}.wav
else
mplayer -nocache rtsp://stream.nhk.or.jp/lesson/${lang}.rm -ao pcm:file=$HOME/.${lang}.wav &> /dev/null
fi
echo "Recording for the language ${lang} ... done"
echo
# Compare the stream's length to the wav's length (Check if the stream wasn't broken) :
STREAM_LENGTH=$(midentify -nocache rtsp://stream.nhk.or.jp/lesson/${lang}.rm |grep LENGTH|cut -d= -f2)
WAV_LENGTH=$(midentify ~/.${lang}.wav |grep LENGTH|cut -d= -f2)
DIFFERENCE=$(( $STREAM_LENGTH - $WAV_LENGTH ))
if [ $DIFFERENCE -gt 10 ] ; then
echo "There is a big difference between the original stream and the wav file (broken stream ?) ... aborting !"
echo
echo "Please re-run the script"
exit 1
fi
# Beginning from the encoding process with automatic numeration :
# Creating the required directories if they don't exist :
if [ ! -d ~/NHK/${lang}/ ] ; then
mkdir -p ~/NHK/${lang}/
fi
if [ "$FORMAT" = "ogg" ] ; then
oggenc -q 5 -a "Radio Japan" -t ${LABEL} ~/.${lang}.wav -o ~/NHK/${lang}/${LABEL}.ogg &&
echo "Encoding in ogg done"
echo
echo "LANGUAGE : ${lang} => OK !"
elif [ "$FORMAT" = "mp3" ] ; then
lame -m s -a --abr 30 ~/.${lang}.wav ~/NHK/${lang}/${LABEL}.mp3 &&
echo "Encoding in mp3 done"
echo
echo "LANGUAGE : ${lang} => OK !"
else
echo "Requested file encoding is not supported, please check your configuration"
fi
rm ~/.${lang}.wav
done
exit 0