hprtranscode 11.8 KB
#!/bin/bash
# Copyright Ken Fallon - Released into the public domain. http://creativecommons.org/publicdomain/ 
#============================================================
# Check input
usage="usage: $(basename $0 ) [ -i ] [ -o ] <fname>, -i to add intro and -o outro, fname is a file with audio for HPR"

# Argument = -t test -r server -p password -v

usage()
{
cat << EOF
usage: $0 [options] {media file to encode} {episode number}

OPTIONS:
   -h      Show this message
   -i      add the intro
   -s      Do not add the summary
   -o      add the outro
   -b      add the intro and outro ( and summary default)
   -2      encode to 2 channels
   -n      no audio normalization
   
EOF
}

TEMP_DIR="/var/tmp/"
CHANNELS="1"
FIXAUDIO="1"
ADDSUMMARY="y"
ADDINTRO="n"
ADDOUTRO="n"
ARTIST="EMPTY"
TITLE="EMPTY"
YEAR="EMPTY"
SLOT="EMPTY"
anhonesthost="sponsor-anhonesthost.com-hpr15.flac"
internetarchive="sponsor-archive.org.flac"

while getopts "hisob2n" OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         s)
             ADDSUMMARY="n"
             ;;
         i)
             ADDINTRO="y"
             ;;
         o)
             ADDOUTRO="y"
             ;;
         b)
             ADDINTRO="y"
             ADDOUTRO="y"
             ;;
         2) 
             CHANNELS="2"
             ;;
         n)
             FIXAUDIO="0"
             ;;         
         ?)
             usage
             exit
             ;;
     esac
done

shift $(($OPTIND - 1))

if [ "$#" -ne 2 ]; then
    echo "Please enter the source file and episode number"
    exit
fi

mediafile=${1}
ep_num=${2}

ep_num=$(echo $ep_num | sed 's/hpr//g')
re='^[0-9]+$'
if ! [[ $ep_num =~ $re ]] ; then
   echo "error: episode \"${ep_num}\" is not a number" >&2; exit 1
fi

if [ ! -f "$anhonesthost" ]; then
  echo "sorry, file \"$anhonesthost\" does not exist"
  exit 1
fi

if [ ! -f "$internetarchive" ]; then
  echo "sorry, file \"$internetarchive\" does not exist"
  exit 1
fi

if [ ! -f "intro.flac" ]; then
  echo "sorry, file \"intro.flac\" does not exist"
  exit 1
fi

if [ ! -f "outro.flac" ]; then
  echo "sorry, file \"outro.flac\" does not exist"
  exit 1
fi

if [ ! -f "${mediafile}" ]; then
  echo "sorry, media file \"${mediafile}\" does not exist"
  exit
fi

if [ ! -r "${mediafile}" ]; then
  echo "sorry, media file \"${mediafile}\" is not readable"
  exit
fi

if [ $(ffprobe "${mediafile}" 2>&1 | grep "Audio:" | wc -l ) -eq 0 ]; then
  echo "sorry, media file \"${mediafile}\" has no audio track"
  exit
fi

if [[ -s hpr${ep_num}.wav ]] || [[ -s hpr${ep_num}.mp3 ]] || [[ -s hpr${ep_num}.ogg ]] || [[ -s hpr${ep_num}.spx ]] || [[ -s hpr${ep_num}_summary.wav ]]
then
  echo "Files for this episode already exist."
  ls -al hpr${ep_num}*
  exit 1
fi

# extract file name and extension
fname=${mediafile%.*}
ext=${mediafile/*./}

echo "--------------------------------------------------------------------------------"
echo "Geting metadata for hpr${ep_num}"

while read -r line
do
  field=$(echo $line | awk -F ':' '{print $1}')
  case $field in
  "HPR_summary")
      HPR_summary=$(echo $line | grep "HPR_summary: " | cut -c 14- )
      continue
      ;;
  "HPR_album")
     HPR_album=$(echo $line | grep "HPR_album: " | cut -c 12- )
      continue
      ;;
  "HPR_artist")
    HPR_artist=$(echo $line | grep "HPR_artist: "  | cut -c 13- )
      continue
      ;;
  "HPR_comment")
    HPR_comment=$(echo $line | grep "HPR_comment: " | cut -c 14- )
      continue
      ;;
  "HPR_genre")
    HPR_genre=$(echo $line | grep "HPR_genre: " | cut -c 12- )
      continue
      ;;
  "HPR_title")
    HPR_title=$(echo $line | grep "HPR_title: " | cut -c 12- )
      continue
      ;;
  "HPR_track")
    HPR_track=$(echo $line | grep "HPR_track: " | cut -c 12- )
      continue
      ;;
  "HPR_year")
    HPR_year=$(echo $line | grep "HPR_year: " | cut -c 11- )
      continue
      ;;
  "HPR_duration")
    HPR_duration=$(echo $line | grep "HPR_duration: " | cut -c 15- )
      continue
      ;;
  "HPR_explicit")
    HPR_explicit=$(echo $line | grep "HPR_explicit: " | cut -c 15- )
      continue
      ;;
  "HPR_license")
    HPR_license=$(echo $line | grep "HPR_license: " | cut -c 14- )
      continue
      ;;
  esac
done < <( wget --timeout=10 --tries=1 --quiet http://hackerpublicradio.org/say.php?id=${ep_num} -O - )

if [[ -z "$ADDINTRO" || -z "$ADDSUMMARY" || -z "$ADDOUTRO" || -z "$HPR_album" || -z "$HPR_artist" || -z "$HPR_comment" || -z "$HPR_genre" || -z "$HPR_title" || -z "$HPR_track" || -z "$HPR_year" || -z "$HPR_summary" || -z "$HPR_duration" || -z "$HPR_explicit" || -z "$HPR_license" ]]
then
  echo "Could not find information on ${ep_num}. Has the show been posted ?"
  exit;
fi
echo "--------------------------------------------------------------------------------"
echo "Add intro   : $ADDINTRO"
echo "Add Summary : $ADDSUMMARY"
echo "Add outro   : $ADDOUTRO"
echo "album       : $HPR_album"
echo "artist      : $HPR_artist"
echo "comment     : $HPR_comment"
echo "genre       : $HPR_genre"
echo "title       : $HPR_title"
echo "track       : $HPR_track"
echo "year        : $HPR_year"
echo "summary     : $HPR_summary"
echo "duration    : $HPR_duration"
echo "explicit    : $HPR_explicit"
echo "license     : $HPR_license"

if [[ $HPR_duration == "0" ]]
then
  echo "The duration is set to 0. Please update the show with the correct time."
  exit;
fi

#============================================================
# Preproc`s the source file

echo "--------------------------------------------------------------------------------"
echo "Prepare mezzanine file"
ffmpeg -i ${mediafile} -ar 44100 -ac $CHANNELS ${fname}_sox.wav > ${fname}_tmp.log 2>&1

if [ "$FIXAUDIO" = "1" ];then 
	echo "normalizing audio"
	sox --temp "${TEMP_DIR}" --norm ${fname}_sox.wav ${fname}_sox_norm.wav
	mv -v ${fname}_sox_norm.wav ${fname}_sox.wav >> ${fname}_tmp.log 2>&1
#	normalize -a 0.5 ${fname}_sox.wav >> ${fname}_tmp.log 2>&1
fi

echo "--------------------------------------------------------------------------------"
echo "Add HPR Branding"

if [ "$ADDSUMMARY" = 'y' ]; then
  echo "Adding the summary"
#  echo "$HPR_summary" - | maryspeak --stdin -vcmu-rms-hsmm -whpr${ep_num}_summary.wav
  echo "$HPR_summary" - | espeak -w hpr${ep_num}_summary.wav
  ffmpeg -i hpr${ep_num}_summary.wav -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_hh.pcm 2>> ${fname}_tmp.log
  ffmpeg -i hpr${ep_num}_summary.wav -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_ia.pcm 2>> ${fname}_tmp.log
  rm hpr${ep_num}_summary.wav
fi

ffmpeg -i "$anhonesthost" -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_hh.pcm 2>> ${fname}_tmp.log
ffmpeg -i "$internetarchive" -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_ia.pcm 2>> ${fname}_tmp.log

if [ "$ADDINTRO" = 'y' ]; then
  echo "Adding the intro"
  ffmpeg -i intro.flac -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_hh.pcm 2>> ${fname}_tmp.log 
  ffmpeg -i intro.flac -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_ia.pcm 2>> ${fname}_tmp.log 
fi

echo "convert the uploaded episode and add it to the temp pcm file"
ffmpeg -i ${fname}_sox.wav -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_hh.pcm 2>> ${fname}_tmp.log 
ffmpeg -i ${fname}_sox.wav -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_ia.pcm 2>> ${fname}_tmp.log 

if [ "$ADDOUTRO" = 'y' ]; then
  echo "Adding the outro"
  ffmpeg -i outro.flac -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_hh.pcm 2>> ${fname}_tmp.log 
  ffmpeg -i outro.flac -ar 44100 -ac $CHANNELS -acodec pcm_s16le -f s16le - >> ${fname}_tmp_ia.pcm 2>> ${fname}_tmp.log 
fi

echo "Convert the pcm file to a know wav format"
ffmpeg -f s16le -ar 44100 -ac $CHANNELS -acodec pcm_s16le -i ${fname}_tmp_hh.pcm ${fname}_mez.wav 2>> ${fname}_tmp.log 
ffmpeg -f s16le -ar 44100 -ac $CHANNELS -acodec pcm_s16le -i ${fname}_tmp_ia.pcm -metadata album="$HPR_album" -metadata author="$HPR_artist" -metadata comment="$HPR_comment" -metadata genre="$HPR_genre" -metadata title="$HPR_title" -metadata track="$HPR_track" -metadata year="$HPR_year" -metadata copyright="$HPR_license" hpr${ep_num}.wav 2>> ${fname}_tmp.log 

echo "Normalizing the wav files"
sox --temp "${TEMP_DIR}" --norm ${fname}_mez.wav ${fname}_mez_norm.wav
mv -v ${fname}_mez_norm.wav ${fname}_mez.wav >> ${fname}_tmp.log 2>&1
# normalize -a 0.5 ${fname}_mez.wav >> ${fname}_tmp.log 2>&1
sox --temp "${TEMP_DIR}" --norm hpr${ep_num}.wav hpr${ep_num}_norm.wav
mv -v hpr${ep_num}_norm.wav hpr${ep_num}.wav >> ${fname}_tmp.log 2>&1
# normalize -a 0.5 hpr${ep_num}.wav >> ${fname}_tmp.log 2>&1

for file_type in ogg mp3 spx opus
do 
  echo "Processing hpr${ep_num}.${file_type}"
  ffmpeg -i ${fname}_mez.wav -metadata album="$HPR_album" -metadata Artist="$HPR_artist" -metadata comment="$HPR_comment" -metadata genre="$HPR_genre" -metadata title="$HPR_title" -metadata track="$HPR_track" -metadata date="$HPR_year" -metadata copyright="$HPR_license" hpr${ep_num}.${file_type} 2>> ${fname}_tmp.log 
  
  
  ffmpeg -i ${fname}_mez.wav -metadata title="title" -metadata author="author" -metadatacComment="Comment" -metadata date="date" -metadata artist="artist" -metadata album_artist="album_artist" -metadata album="album" -metadata grouping="grouping" -metadata composer="composer" -metadata year="year" -metadata track="track" -metadata comment="comment" -metadata genre="genre" -metadata copyright="copyright" -metadata description="description" -metadata synopsis="synopsis" -metadata show="show" -metadata episode_id="episode_id" -metadata network="network" -metadata lyrics="lyrics" hpr${ep_num}X.${file_type}  2>> ${fname}_tmp.log 
done

# # # echo "--------------------------------------------------------------------------------"
# # # echo "File information"
# # # ffprobe ${fname}_mez.wav 2>&1 | grep Audio:
# # # mediainfo ${fname}_mez.wav
# # # 
# # # echo "--------------------------------------------------------------------------------"
# # # echo "Convert to mp3"
# # # sox --temp "${TEMP_DIR}" -S ${fname}_mez.wav hpr${ep_num}.mp3 
# # # 
# # # echo "--------------------------------------------------------------------------------"
# # # echo "Convert to ogg"
# # # sox --temp "${TEMP_DIR}" -S ${fname}_mez.wav hpr${ep_num}.ogg
# # # 
# # # echo "--------------------------------------------------------------------------------"
# # # echo "Convert to spx"
# # # sox --temp "${TEMP_DIR}" -S ${fname}_mez.wav -c 1 -r 16000 -t wav  - | speexenc - hpr${ep_num}.spx
# # # 
# # # if [[ ! -s hpr${ep_num}.wav ]] || [[ ! -s hpr${ep_num}.mp3 ]] || [[ ! -s hpr${ep_num}.ogg ]] || [[ ! -s hpr${ep_num}.spx ]]
# # # then
# # #   echo "ERROR: Something went wrong encoding the files"
# # #   exit 1
# # # fi

# # # fix_tags -album="$HPR_album" -artist="$HPR_artist" -comment="$HPR_comment" -genre="$HPR_genre" -title="$HPR_title" -track="$HPR_track" -year="$HPR_year" hpr${ep_num}* 2>> ${fname}_tmp.log 1>&2
# # # fix_tags hpr${ep_num}*
# # # 
# # # #echo "Changing the file dates to the time of upload"
# # # touch -r ${mediafile} hpr${ep_num}*
# # # 
# # # #echo "Remove temp files"
# # # rm ${fname}_sox.wav ${fname}_tmp*.pcm ${fname}_tmp.log ${fname}_mez.wav
# # # 
# # # mv -v hpr${ep_num}.wav /var/IA/uploads/
# # # 
# # # ###rsync -ave ssh --partial --progress --ignore-existing hpr${ep_num}.mp3 hpr${ep_num}.ogg hpr${ep_num}.spx hpr:www/eps/
# # # 
# # # read -p "Remove files for \"${fname}\" (y|N) ? " -n 1 -r
# # # echo    # (optional) move to a new line
# # # if [[ $REPLY =~ ^[Yy]$ ]]
# # # then
# # #   mv -v ${fname}* hpr${ep_num}*  *_${ep_num}_* /var/IA/done/
# # # fi
# # # wget -q "http://hackerpublicradio.org/hpr_total_rss.php?gomax=1" -O - | xmlstarlet val --err -
# # # wget -q "http://hackerpublicradio.org/hpr_total_spx_rss.php?gomax=1" -O - | xmlstarlet val --err -
# # # wget -q "http://hackerpublicradio.org/hpr_total_ogg_rss.php?gomax=1" -O - | xmlstarlet val --err -