#!/bin/bash

# by Christoph , St.Gallen Mai 2024

# Debian 12.x  Debian Bookworm

# hts_render for tvheadend or /home/user/Videos
# Tipp: install first the script and start it plain, to get the help for tvheadend, 
# /usr/local/bin/hts_render \"%f\" mv  wrong, but in help output it will be  /usr/local/bin/hts_render "%f" mv  (this you need then)

# Installation: copy: cp hts_render Script to /usr/local/bin/ , then chmod 755 /usr/local/bin/hts_render, and fix the 
#	the Variables like input path output path etc...
#	ffmpeg must be installed, is recommend/needed.

# The Suffix: i use mkv records, some people like ts more (together with timeshift), i don't know, i never tested.
# 	Maybee with only small PC's you need the ts....?

# Some privat Notice, i keep it
#ffprobe input.mkv 2>&1 | egrep 'Stream #' | wc -l
#fprobe input.mkv 2>&1 | egrep 'SUMMARY'
#`cpulimit -l 650 -- ffmpeg -i input.mkv -map 0:0 -map 0:1 -map 0:2 -map 0:3 -vcodec libx264 -c:a copy -c:s copy output.mkv`


hts_path=/home/hts_recs
hts_smallout_path=/home/hts_small
nameend="-small"
suffix=mkv
delay_start=15
# ffmpeg Quality
# bigger files <-- ultrafast superfast veryfast faster fast medium (medium default preset) slow slower veryslow --> smaler files
preset=medium

# Functions
check_map_recode() {
map_count=`ffprobe "$file" 2>&1 | egrep 'Stream #' | wc -l`
map_count=$(($map_count-1))
outfile=`basename -- "$file" ".${file##*.}"`
show_info
#exit 0
render_map0.$map_count
}

show_info() {
echo "=================================================================================="
echo -n " Working Directory: ";pwd
echo " Input path for ff: $hts_path"
echo " Output Path: $hts_smallout_path"
echo " Input file: $file"
echo " Output file: $outfile$nameend.$suffix"
echo " Audio/SUB Streams inside: $map_count  (+1 Videostream)"
echo " Using Function: render_map0.$map_count" 
echo "=================================================================================="
}

help_cmd() {
scp_name=`basename $0`
echo -e "
 No Input File..........
 
 $scp_name commands:
 ======================

 Single Files:  $scp_name [inputfile] [cp/mv/rm]      
 Fuel Folder (all Video Files): $scp_name ff [cp/mv/rm]

 Optional [cp/mv/rm]: 
 cp: copy the movie back to Input Path, overwrite, let the -small video in the Output path stay.
	OutputPath/Video-small  (using inputfile name) -->  InputPath/Video
 mv: copy the movie back to Input Path, overwrite, no -small video in Output path.
	OutputPath/Video-small  (using inputfile name) -->  InputPath/Video
 rm: remove the Input Video File, only the -small Video left in the Output path.

 Installation: copy $scp_name Script to /usr/local/bin/ , then chmod 755 /usr/local/bin/$scp_name, and fix the 
	the Variables like input path output path etc...
	ffmpeg must be installed, is recommend/needed.

 tvheadend: Configuration -> Record -> Post Proccesor Command:   /usr/local/bin/hts_render \"%f\" mv   (SAVE!)
	In my Example: (very importend the directory rights !!)
	The Input Path is given by tvheadend:
	drwxrwxr-x  2 hts   video  4096  4. Mai 03:28 hts_recs
	The Output Path i create as root or sudo:  mkdir /home/hts_small;chown -R hts:video /home/hts_small
	drwxrwxr-x  2 hts   video  4096  4. Mai 03:28 hts_small
	Done, now it shut work.
	I use mv, so i overwrite the original video with smaler size, i don't keep the original size.

 Rename this script with a copy of:
	You can copy this script cp /usr/local/bin/hts_render /usr/local/bin/video_render 
	Maybee set the rights, normal they are also copyied, otherwise chmod 755 /usr/local/bin/video_render
	Set the Input/Output Path to:  /home/user/Videos/a_big  and /home/user/Videos/a_small  (as idee)
	So you can use it as normal Video Renderer in your home.

"
}

render_map0.0() {
ffmpeg -i "$file" -map 0:0 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.1() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.2() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.3() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.4() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.5() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.6() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -map 0:6 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.7() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -map 0:6 -map 0:7 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.8() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -map 0:6 -map 0:7 -map 0:8 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}

render_map0.9() {
ffmpeg -i "$file" -map 0:0 -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -map 0:6 -map 0:7 -map 0:8 -map 0:9 -vcodec libx264 -preset $preset -c:a copy -c:s copy "$hts_smallout_path"/"$outfile""$nameend"."$suffix"
}


#Here we go...

if [ "$1" = "ff" ]; then
	cd "$hts_path"
	for i in *;do
		if [ -f "$i" ]; then
			sleep $delay_start
			file=`pwd`/"$i"
			check_map_recode
			if [ "$2" = "mv" ];then
				mv "$hts_smallout_path"/"$outfile""$nameend"."$suffix" "$file"
			fi
			if [ "$2" = "cp" ];then
				cp "$hts_smallout_path"/"$outfile""$nameend"."$suffix" "$file"
			fi
			if [ "$2" = "rm" ];then
				rm "$file"
			fi
		fi
	sync
	done
exit 0
fi

if [ -n "$1" ]; then
	if [ -f "$1" ]; then
		sleep $delay_start
		file="$1"
		check_map_recode
		if [ "$2" = "mv" ];then
			mv "$hts_smallout_path"/"$outfile""$nameend"."$suffix" "$file"
		fi
		if [ "$2" = "cp" ];then
			cp "$hts_smallout_path"/"$outfile""$nameend"."$suffix" "$file"
		fi
		if [ "$2" = "rm" ];then
			rm "$file"
		fi
		sync
		exit 0
	fi
fi
help_cmd
exit 0


