#!/bin/sh
# Variables
#TSVIDEO=$1    
FILENAME=$1    
b=$2        
TRANSCODEPATH='/recordings/'                    # tvheadend에서 지정한 파일 저장 경로 수정
title=$(echo $b | sed s'/\([0-9]\{4,\}\).*\([0-9]\{2,\}\).*\([0-9]\{2,\}\)/\1\2\3/' | sed s'/[\-]/ /g' | sed 's/ \{2,\}/ /g' | sed s'/ \[.*\]//g' | sed s'/...$//')
target_dir_name=$(echo $title | sed s'/.[0-9].*$//')

TSVIDEO=$TRANSCODEPATH$FILENAME".ts"
MKVVIDEO=$TRANSCODEPATH$title".H264.1080P.mkv"    
# 출력할 파일에 새로 추가할 내용 및 확장자 ".H264.1080P.mkv" 부분은
# mkv 확장자로 할 경우 '.H264.1080P.mkv'
# mp4 확장자로 할 경우 'H264.1080P.MP4'

/bin/sync    # flush diskbuffer
# Transcode to mkv

echo "***** 시작시간 $(/bin/date +%Y.%m.%d' '%H:%M:%S) *****" >$TRANSCODEPATH$title.log	
echo "****************************************************" >>$TRANSCODEPATH$title.log
echo "********** $MKVVIDEO 로 인코딩을 시작합니다. *************" >>$TRANSCODEPATH$title.log
echo "***************************************************" >>$TRANSCODEPATH$title.log
echo "*************** INPUT = $TSVIDEO ******************" >>$TRANSCODEPATH$title.log
echo "*************** OUTPUT = $MKVVIDEO ****************" >>$TRANSCODEPATH$title.log
echo "***************************************************" >>$TRANSCODEPATH$title.log
echo "****** Created by $(/usr/bin/whoami) **************" >>$TRANSCODEPATH$title.log 
echo "***************************************************" >>$TRANSCODEPATH$title.log
ffmpeg -i "$TSVIDEO" -vcodec libx264 -acodec copy -b:a 192k -level 4.1 "$MKVVIDEO" </dev/null >>$TRANSCODEPATH$title.log 2>&1
#720p 해상도로 인코딩 할 경우 'hd720'추가
#ffmpeg -i "$TSVIDEO" -vcodec libx264 -acodec copy -b:a 192k -level 4.1 -s hd720 "$MKVVIDEO" </dev/null >>$TRANSCODEPATH$title.log 2>&1

echo "***************************************************" >>$TRANSCODEPATH$title.log

#rm -f "$TSVIDEO"
echo "** 종료시간 $(/bin/date +%Y.%m.%d' '%H:%M:%S) **" >>$TRANSCODEPATH$title.log
