#!/bin/bash
# convert HD recording to SD
#
# based of vdr forum thread by Sinai & others

MSG="/usr/local/bin/svdrpsend MESG"
# MSG=echo

# reording path is: name/date.rec
REC_PATH=$1

# extract date part of record
DATE_PART=${REC_PATH##*/}

# extract name part of record & append "_SD"
NAME_PART=${REC_PATH%/*}
NAME_PART_SD=${NAME_PART}_SD

# check if info file exist

if [ ! -f $REC_PATH/info ]; then

  EXIT_MSG="$REC_PATH/info file missing, exiting"

elif [ ! -f $REC_PATH/00001.ts ] ; then

  EXIT_MSG="no 00001.ts, exiting"

else

  # get video string from info file & check if type HD
  INPUT=$(grep -i 'X 5' $REC_PATH/info)
  if [ "$INPUT" == "" ]; then
    # not HD, check if SD
    INPUT=$(grep -i 'X 1' $REC_PATH/info)
    if [ "$INPUT" == "" ]; then
      # neither HD nor SD
      EXIT_MSG="video type $INPUT unknown, exiting"
    else
      # SD
      EXIT_MSG="video type SD, exiting"
    fi

  else

    # video type HD
    # at least one ts
    # continue

    # create sorted video file list
    # choose the variant you like ...
    # VIDEOFILES=`echo $REC_PATH/*.ts | sort`
    # VIDEOFILES=`echo $REC_PATH/[0-9][0-9][0-9][0-9][0-9].ts | sort`

    cd $REC_PATH
    VIDEOFILES=$(echo ./[0-9][0-9][0-9][0-9][0-9].ts | sort)

    # video type translation

    # string to array
    i=($INPUT)

    o[0]=${i[0]}
    o[1]="1"
    o[2]=0$((0x${i[2]}-8))
    o[3]=${i[3]}
    o[4]=${i[4]}

    OUTPUT="${o[0]} ${o[1]} ${o[2]} ${o[3]} ${o[4]}"

  fi
fi

# execute via at/atd...
# to close OSD
# for SVDRPSEND MESG to work
# to run conversion in background

at now <<EOF

(

# check if we have to exit
if  [ $EXIT_MSG ]; then
  $MSG $EXIT_MSG
  exit
fi

$MSG "start converting HD recording $REC_PATH to SD"
# $MSG "Name Part of Path: $NAME_PART"
# $MSG "Date Part of Path: $DATE_PART"
# $MSG "SD Path will be: $NAME_PART_SD/$DATE_PART"
# $MSG "input: $INPUT output: $OUTPUT"
# $MSG "videofiles: $VIDEOFILES"

# gen SD folder: name_SD/date.rec
mkdir $NAME_PART_SD
mkdir $NAME_PART_SD/$DATE_PART

# copy recording info file to SD folder
cp $REC_PATH/info $NAME_PART_SD/$DATE_PART

# change mpeq4 string to mpeg2 string
# change type id
sed -i "s/$INPUT/$OUTPUT/" $NAME_PART_SD/$DATE_PART/info

# concatenate 00001.ts etc to video_hd.ts
cat $VIDEOFILES > $NAME_PART_SD/$DATE_PART/hd_video.ts

cd $NAME_PART_SD/$DATE_PART

# convert hd_video.ts to 00001.ts, version from vdr forum thread
# nice ffmpeg -i hd_video.ts -y -threads 1 -ar 48000 -ac 2 -b 3500k -r 25 -maxrate 8000k -bufsize 1000k -vcodec mpeg2video -s 720x576 -aco
dec mp2 -ab 192k -async 2 -f mpegts 00001.ts

# convert hd_video.ts to 00001.ts, copy all streams except video: convert to mpeg2
nice ffmpeg -i hd_video.ts -y -threads 1 -map 0 -c copy -c:v mpeg2video -b 4500k -r 25 -maxrate 8000k -bufsize 1000k -s 720x576 -f mpegts
00001.ts

sleep 2

#  remove hd video
rm hd_video.ts

sleep 2

# genindex for sd version
vdr --genindex=$NAME_PART_SD/$DATE_PART

# update rec list
/bin/touch /video/.update

$MSG "finished converting HD recording $REC_PATH to SD"

)  >/dev/null 2>&1

EOF

