#!/bin/bash
TV_GROUP=$1
TV_CHANNEL=$2
TV_INPUT_PORT=$3
TV_INPUT_HTTP=$4
TV_FILE_LOCATION="/storage/dockerCommands/jellyfin/config/data/tv/"$TV_GROUP"/"$TV_CHANNEL"/"$TV_CHANNEL".m3u8"
TV_LINE_SED='2>&1 | stdbuf -oL sed ''s/^/[Tv-'$TV_GROUP'-'$TV_CHANNEL'''] /'

rm /storage/dockerCommands/jellyfin/config/data/tv/$TV_GROUP/$TV_CHANNEL/*

###Software transcoding
#/storage/dockerCommands/jellyfin/config/data/tv/ffmpeg-master-latest-linux64-gpl/bin/ffmpeg \
#-noautorotate \
#-err_detect ignore_err \
#-fflags +genpts+discardcorrupt+igndts \
#-i $TV_INPUT_PORT \
#-map 0:a:0 \
#-map 0:v:0 \
#-f hls \
#-b:v 8000k \
#-hls_time 15 \
#-hls_list_size 4 \
#-hls_flags delete_segments \
#$TV_FILE_LOCATION

###Hardware transcoding
#/storage/ffmpeg/ffmpeg-8.0/ffmpeg \
#-hwaccel vaapi \
#-vaapi_device /dev/dri/renderD128 \
#-noautorotate \
#-err_detect ignore_err \
#-fflags +discardcorrupt \
#-i $TV_INPUT_PORT \
#-vf hwupload \
#-c:v h264_vaapi \
#-map 0:a:0 \
#-map 0:v:0 \
#-f hls \
#-b:v 8000k \
#-b:a 128k \
#-hls_time 30 \
#-hls_list_size 3 \
#-hls_flags delete_segments \
#$TV_FILE_LOCATION

###NVIDIA transcoding
ffmpeg -hwaccel cuda \
-hwaccel_output_format cuda \
-noautorotate -err_detect ignore_err \
-fflags +discardcorrupt \
-i $TV_INPUT_PORT \
-vf yadif_cuda \
-map 0:a:0 -map 0:v:0 \
-c:v h264_nvenc \
-c:a libfdk_aac \
-b:a 128k \
-b:v 3000k \
-hls_segment_type fmp4 \
-f hls -hls_time 2 \
-hls_list_size 10 \
-hls_flags +delete_segments \
$TV_FILE_LOCATION \
2>&1 | stdbuf -oL sed 's/^/[Tv-'$TV_GROUP'-'$TV_CHANNEL'] /'

#ffmpeg -hwaccel cuda -hwaccel_output_format cuda -noautorotate -err_detect ignore_err -fflags +discardcorrupt \
#-i $TV_INPUT_PORT \
#-vf yadif_cuda \
#-map 0:a:0 -map 0:v:0 \
#-c:v h264_nvenc \
#-c:a libfdk_aac \
#-b:a 384k \
#-b:v 6000k \
#-f hls -hls_time 6 -hls_list_size 4 -hls_flags +delete_segments \
#$TV_FILE_LOCATION \
#$TV_LINE_SED

#ffmpeg \
#  -hwaccel vaapi \
#  -vaapi_device /dev/dri/renderD129 \
#  -noautorotate \
#  -err_detect ignore_err \
#  -fflags +discardcorrupt \
#  -i rtp://10.1.1.1:$TV_PORT \
#  -vf hwupload \
#  -map 0:a:0 \
#  -map 0:v:0 \
#  -c:v h264_vaapi \
#  -c:a libfdk_aac \
#  -b:a 384k \
#  -b:v 6000k \
#  -f hls \
#  -hls_time 6 \
#  -hls_list_size 4 \
#  -hls_flags +delete_segments \
#  $TV_FILE_LOCATION \
#  $TV_LINE_SED

