#!/bin/sh

###
### Copyright (C) MINZKN.COM
### All rights reserved.
### Author: JAEHYUK CHO <mailto:minzkn@minzkn.com>
###

show_help()
{
	echo "usage: ${0} [<options>] <infile [...]>"
	echo "       -h, --help             : help"
	echo "       -t, --threads <n>      : threads (0[default])"
	echo "       -v, --vcodec <name>    : video codec (copy, h264[default], h264_nvenc, hevc, ...)"
	echo "       -a, --acodec <name>    : audio codec (copy, aac[default], ...)"
	echo "       -s, --scodec <name>    : subtitle codec (none, copy, mov_text[default], ...)"
	echo "       -f, --format <name>    : output format (mp4[default], mkv, avi, ...)"
	echo "       -c, --cmd              : only cmd view"
	echo
}

if [ -z "${1}" ]
then
	show_help
	exit 1
fi

if [ -x "$(dirname ${0})/ffmpeg" ]
then
	# Same script location
	g_ffmpeg_cmd=$(dirname ${0})/ffmpeg
elif [ -x "/var/packages/VideoStation/target/bin/ffmpeg" ]
then
	# Synology NAS VideoStation
	g_ffmpeg_cmd=/var/packages/VideoStation/target/bin/ffmpeg
else
	# default location
	g_ffmpeg_cmd=ffmpeg
fi
#g_ffmpeg_cmd=echo

g_only_cmd=0
g_threads=0
#g_video_codec_name=h264_nvenc
#g_video_codec_name=h264
g_video_codec_name=libx264
g_audio_codec_name=aac
g_audio_bitrate=192k
g_audio_channel=2
g_subtitle_codec_name=mov_text
g_output_format_name=mp4

do_transcoding()
{
	local s_cmdline
	local s_infile
	local s_infile_dirname
	local s_infile_filename
	local s_infile_name
	local s_infile_ext
	local s_outfile_ext
	local s_outfile_filename
	local s_outfile_dirname
	local s_outfile

	s_infile=${1}
	s_infile_dirname=$(dirname "${s_infile}")
	s_infile_filename=$(basename "${s_infile}")
	s_infile_name=${s_infile_filename%.*}
	s_infile_ext=${s_infile_filename##*.}

	s_outfile_ext=${g_output_format_name}
	if [ "${s_infile_ext}" = "${s_outfile_ext}" ]
	then
		s_outfile_filename=_${s_infile_name}.${s_outfile_ext}
	else
		s_outfile_filename=${s_infile_name}.${s_outfile_ext}
	fi
	s_outfile_dirname=${s_infile_dirname}
	s_outfile=${s_outfile_dirname}/${s_outfile_filename}

	echo "Transcoding... (\"${s_infile}\"[${s_infile_ext}] to \"${s_outfile}\"[${s_outfile_ext}] codec=${g_video_codec_name}/${g_audio_codec_name}/${g_subtitle_codec_name})"
	
	s_cmdline=${g_ffmpeg_cmd}
	s_cmdline="${s_cmdline} -hide_banner"
	s_cmdline="${s_cmdline} -loglevel warning"
	#s_cmdline="${s_cmdline} -progress -"
	s_cmdline="${s_cmdline} -stats"
	#s_cmdline="${s_cmdline} -y"
	#s_cmdline="${s_cmdline} -correct_ts_overflow 0"
	#s_cmdline="${s_cmdline} -hwaccel_output_format cuda"
	s_cmdline="${s_cmdline} -i \"${s_infile}\""
	s_cmdline="${s_cmdline} -threads \"${g_threads}\""
	s_cmdline="${s_cmdline} -vsync \"vfr\""
	s_cmdline="${s_cmdline} -c:v \"${g_video_codec_name}\""
	case "${g_video_codec_name}" in
		copy)
		;;
		libx264)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"veryslow\""
			s_cmdline="${s_cmdline} -profile:v \"high10\""
			#s_cmdline="${s_cmdline} -level:v auto"
			s_cmdline="${s_cmdline} -pix_fmt \"yuv420p\""
		;;
		h264)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"veryslow\""
			s_cmdline="${s_cmdline} -profile:v \"high10\""
			#s_cmdline="${s_cmdline} -level:v auto"
			s_cmdline="${s_cmdline} -pix_fmt \"yuv420p\""
		;;
		h264_nvenc)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"medium\""
			s_cmdline="${s_cmdline} -profile:v \"high\""
			#s_cmdline="${s_cmdline} -level:v auto"
			s_cmdline="${s_cmdline} -pix_fmt \"yuv420p\""
		;;
		hevc_nvenc)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"veryslow\""
			s_cmdline="${s_cmdline} -profile:v \"high10\""
			#s_cmdline="${s_cmdline} -level:v auto"
			#s_cmdline="${s_cmdline} -pix_fmt \"yuv420p\""
		;;
		h264_qsv)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"veryslow\""
			s_cmdline="${s_cmdline} -profile:v \"high\""
			#s_cmdline="${s_cmdline} -level:v auto"
			s_cmdline="${s_cmdline} -pix_fmt \"nv12\""
		;;
		hevc_qsv)
			#s_cmdline="${s_cmdline} -b:v 1313505"
			s_cmdline="${s_cmdline} -preset \"veryslow\""
			s_cmdline="${s_cmdline} -profile:v \"high10\""
			#s_cmdline="${s_cmdline} -level:v auto"
			#s_cmdline="${s_cmdline} -pix_fmt \"yuv420p\""
		;;
	esac
	s_cmdline="${s_cmdline} -c:a \"${g_audio_codec_name}\""
	case "${g_audio_codec_name}" in
		copy)
		;;
		*)
			s_cmdline="${s_cmdline} -ab \"${g_audio_bitrate}\""
			s_cmdline="${s_cmdline} -ac \"${g_audio_channel}\""
		;;
	esac
	case "${g_subtitle_codec_name}" in
		none)
		;;
		copy)
			s_cmdline="${s_cmdline} -c:s \"${g_subtitle_codec_name}\""
		;;
		*)
			s_cmdline="${s_cmdline} -c:s \"${g_subtitle_codec_name}\""
		;;
	esac
	s_cmdline="${s_cmdline} -f \"${g_output_format_name}\""
	s_cmdline="${s_cmdline} -max_muxing_queue_size 1024"
	case "${g_subtitle_codec_name}" in
		none)
		;;
		*)
			s_cmdline="${s_cmdline} -map 0"
		;;
	esac
	#s_cmdline="${s_cmdline} -map 0:m:language:kor"
	#s_cmdline="${s_cmdline} -map_metadata -1"
	#s_cmdline="${s_cmdline} -metadata:s:s:0 language=kor"
	s_cmdline="${s_cmdline} \"${s_outfile}\""
	echo "CMD: \$ ${s_cmdline}"
	if [ "${g_only_cmd}" -eq 0 ]
	then
		eval ${s_cmdline}
	fi

	s_result=${?}
	echo "done. => ${s_result} (\"${s_infile}\"[${s_infile_ext}] to \"${s_outfile}\"[${s_outfile_ext}])"

	return ${s_result}
}

while [ -n "${1}" ]
do
	if [ -f "${1}" ]
	then
		do_transcoding "${1}"
		shift
		continue
	fi
	case "${1}" in
		-h|--help)
			show_help
			exit 1
		;;
		-t|--thread|--threads)
			shift
			g_threads=${1}
			echo "Threads : \"${g_threads}\""
			shift
		;;
		-v|--vcodec)
			shift
			g_video_codec_name=${1}
			echo "Selecting video codec : \"${g_video_codec_name}\""
			shift
		;;
		-a|--acodec)
			shift
			g_audio_codec_name=${1}
			echo "Selecting audio codec : \"${g_audio_codec_name}\""
			shift
		;;
		-s|--scodec)
			shift
			g_subtitle_codec_name=${1}
			echo "Selecting subtitle codec : \"${g_subtitle_codec_name}\""
			shift
		;;
		-f|--format)
			shift
			g_output_format_name=${1}
			echo "Selecting output format : \"${g_output_format_name}\""
			shift
		;;
		-c|--cmd)
			shift
			g_only_cmd=1
			echo "only cmd show"
		;;
		*)
			echo "not found ! (\"${1}\")"
			exit 1
		;;
	esac
done

# End of transcoding.sh
