#!/bin/dash

CH_NAME=test1

if [ "$CH_NAME" == "" ] ; then
        echo usage: $0 channel_name
        exit
fi

CH_DIR="/var/www/html/live/${CH_NAME}"

rm -rf "${CH_DIR}"
mkdir "${CH_DIR}"
chown www-data:www-data "${CH_DIR}"
chmod og+s "${CH_DIR}"

cat >${CH_DIR}/live.m3u8 << LIVEM3U8
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2200000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=1280x720
live2100k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1800000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=1024x576
live1700k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1400000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=720x404
live1300k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=512x288
live900k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=500000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=512x288
live450k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=150000,CODECS="avc1.4d001f,mp4a.40.2",RESOLUTION=512x288
live120k.m3u8
LIVEM3U8

FFMPEG_OPTS="-threads 0 -fflags +genpts -flags +global_header"

#VIDEO_OPTIONS="-vf yadif
#-map 0:v           -map 0:v           -map 0:v           -map 0:v           -map 0:v          -map 0:v
#-s:v:0 1280x720    -s:v:1 1024x576    -s:v:2 720x404     -s:v:3 512x288     -s:v:4 512x288    -s:v:5 512x288
#-b:v:0 2100k       -b:v:1 1700k       -b:v:2 1300k       -b:v:3 900k        -b:v:4 450k       -b:v:5 120k
#-minrate:v:0 2000k -minrate:v:1 1600k -minrate:v:2 1200k -minrate:v:3 800k  -minrate:v:4 400k -minrate:v:5 100k
#-maxrate:v:0 2200k -maxrate:v:1 1800k -maxrate:v:2 1400k -maxrate:v:3 1000k -maxrate:v:4 500k -maxrate:v:5 150k
#-bufsize:v:0 2100k -bufsize:v:1 1700k -bufsize:v:2 1300k -bufsize:v:3 900k  -bufsize:v:4 450k -bufsize:v:5 120k
#-c:v libx264 -pix_fmt yuv420p -preset slow -profile:v main
#"
#        -filter_complex '[0:v]yadif,split=3[out1][out2]'
#               -filter_complex aresample=44100[au1];[au1]adelay=170\|170[au2];[au2:a]split=3[a1][a2][a3]

HLS_OPTS="-hls_time 4 -hls_list_size 11 -hls_start_number_source epoch -hls_flags delete_segments"
H264_OPTS="-c:v libx264  -preset slow -profile:v main -x264opts keyint=100:min-keyint=100:scenecut=-1 "
VIDEO_OPTIONS="
        -c:a libfdk_aac
        -map 0:v -s:v 1280x720 -b:v 2100k -minrate:v 2000k -maxrate:v 2200k -bufsize:v 2100k -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=48000 -b:a 128k
                ${H264_OPTS} ${HLS_OPTS}
                ${CH_DIR}/live2100k.m3u8
        -map 0:v -s:v 1024x576 -b:v 1700k -minrate:v 1600k -maxrate:v 1800k -bufsize:v 1700k -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=44100 -b:a 128k
                ${H264_OPTS} ${HLS_OPTS}
                ${CH_DIR}/live1700k.m3u8
        -map 0:v -s:v 720x404  -b:v 1300k -minrate:v 1200k -maxrate:v 1400k -bufsize:v 1300k -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=44100 -b:a 96k
                ${H264_OPTS} ${HLS_OPTS}
                ${CH_DIR}/live1300k.m3u8
        -map 0:v -s:v 512x288  -b:v 900k  -minrate:v 800k  -maxrate:v 1000k -bufsize:v 900k  -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=44100 -b:a 64k
                ${H264_OPTS}  ${HLS_OPTS}
                ${CH_DIR}/live900k.m3u8
        -map 0:v -s:v 512x288  -b:v 450k  -minrate:v 400k  -maxrate:v 500k  -bufsize:v 450k  -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=44100 -b:a 64k
                ${H264_OPTS}  ${HLS_OPTS}
                ${CH_DIR}/live450k.m3u8
        -map 0:v -s:v 512x288  -b:v 120k  -minrate:v 100k  -maxrate:v 150k  -bufsize:v 120k  -pix_fmt yuv420p -vf yadif
                -map 0:a -af aresample=32000 -b:a 32k
                ${H264_OPTS}  ${HLS_OPTS}
                ${CH_DIR}/live120k.m3u8
"
# -filter_complex "aresample=48000[au1];[au1]adelay=170|170;yadif=0:-1:0[spl];[spl]split=6"

#-f tee "[f=ismv:movflags=isml+frag_keyframe]{0}/Streams(Encoder1)"|"[select=\'v:0\',\'a:0\':f=mpegts]udp://226.84.84.1:5500"

ENC_OPTS="${FFMPEG_OPTIONS} ${VIDEO_OPTIONS}"

set -x

/usr/local/bin/ffmpeg.wtk -loglevel info -thread_queue_size 1024 \
        -f decklink -i 'DeckLink 4K Extreme@2' -audio_input embedded -video_input sdi \
        ${FFMPEG_OPTS} ${ENC_OPTS}
