From 7518d0bc3b82febd13f31fc51455d3a1620fd229 Mon Sep 17 00:00:00 2001 From: q3aql Date: Fri, 10 Mar 2023 00:14:18 +0100 Subject: [PATCH] Add ks-av1 & ks-av1s tools --- src/ks-av1 | 513 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ks-av1s | 512 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1025 insertions(+) create mode 100755 src/ks-av1 create mode 100755 src/ks-av1s diff --git a/src/ks-av1 b/src/ks-av1 new file mode 100755 index 0000000..a75beb8 --- /dev/null +++ b/src/ks-av1 @@ -0,0 +1,513 @@ +#!/bin/bash + +############################################################### +# ks-av1 (ks-tools) - Convert video to AV1 (Codec) format # +# Date: 09-03-2023 # +# Author: q3aql # +# Contact: q3aql@duck.com # +############################################################### +VERSION="8.4-dev" +M_DATE="090323" + +# Detect version ffmpeg for old releases +ffmpeg_version=$(ffmpeg -version 2>&1 | grep version | head -1 | cut -d " " -f 3 | cut -d "." -f 1) +IFS="abcdefghijklmnopqrstyvwxzABCDEFHIJKLMNOPQRSTYVWXZ@/()" +ffmpeg_version=$(echo ${ffmpeg_version}) +IFS=$' \t\n' +unset IFS +if [ -z "${ffmpeg_version}" ] ; then + video_sync="vsync" +else + if [ ${ffmpeg_version} -lt 5 ] ; then + video_sync="vsync" + else + video_sync="fps_mode" + fi +fi + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" +configFolder=${HOME}/.ks-tools +configFile=${configFolder}/ks-av1 + +# Basic parameters creating config file. +rel_size="1280x534" +vcodec="libsvtav1" +b_vcodec="2000k" +acodec="libvorbis" +b_acodec="160k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="webm" + +# Check cygwin alias (for Windows) +if [ -f "/usr/bin/cygwin-alias.sh" ] ; then + shopt -s expand_aliases + source "/usr/bin/cygwin-alias.sh" +fi + +# Create inicial config file +mkdir -p ${configFolder} +if [ -f ${configFile} ] ; then + exist_rel_size=$(cat ${configFile} | grep "rel_size=") + exist_vcodec=$(cat ${configFile} | grep "vcodec=" | cut -c2 | cut -d "_" -f 1) + exist_b_vcodec=$(cat ${configFile} | grep "b_vcodec=") + exist_acodec=$(cat ${configFile} | grep "acodec" | cut -c2 | cut -d "_" -f 1) + exist_b_acodec=$(cat ${configFile} | grep "b_acodec=") + exist_default_lang_audio=$(cat ${configFile} | grep "default_lang_audio=") + exist_default_lang_subt=$(cat ${configFile} | grep "default_lang_subt=") + exist_v_ext=$(cat ${configFile} | grep "v_ext=") + if [ -z ${exist_rel_size} ] ; then + echo "rel_size=${rel_size}" >> ${configFile} + fi + if [ -z ${exist_vcodec} ] ; then + echo "vcodec=${vcodec}" >> ${configFile} + fi + if [ -z ${exist_b_vcodec} ] ; then + echo "b_vcodec=${b_vcodec}" >> ${configFile} + fi + if [ -z ${exist_acodec} ] ; then + echo "acodec=${acodec}" >> ${configFile} + fi + if [ -z ${exist_b_acodec} ] ; then + echo "b_acodec=${b_acodec}" >> ${configFile} + fi + if [ -z ${exist_default_lang_audio} ] ; then + echo "default_lang_audio=${default_lang_audio}" >> ${configFile} + fi + if [ -z ${exist_default_lang_subt} ] ; then + echo "default_lang_subt=${default_lang_subt}" >> ${configFile} + fi + if [ -z ${exist_v_ext} ] ; then + echo "v_ext=${v_ext}" >> ${configFile} + fi + source ${configFile} +else + echo "#!/bin/bash" > ${configFile} + echo "" >> ${configFile} + echo "rel_size=${rel_size}" >> ${configFile} + echo "vcodec=${vcodec}" >> ${configFile} + echo "b_vcodec=${b_vcodec}" >> ${configFile} + echo "acodec=${acodec}" >> ${configFile} + echo "b_acodec=${b_acodec}" >> ${configFile} + echo "default_lang_audio=${default_lang_audio}" >> ${configFile} + echo "default_lang_subt=${default_lang_subt}" >> ${configFile} + echo "v_ext=${v_ext}" >> ${configFile} + source ${configFile} +fi + +# Check if ffmpeg is installed +path_check="/usr/bin /bin /usr/local/bin ${HOME}/.local/bin" +dependencies="ffmpeg grep sed grep cut head tail tr cat" +dependencies_found="" +dependencies_not_found="" +for checkPath in ${path_check} ; do + for checkDependencies in ${dependencies} ; do + if [ -f ${checkPath}/${checkDependencies} ] ; then + dependencies_found="${dependencies_found} ${checkDependencies}" + fi + done +done +for notFound in ${dependencies} ; do + check_found_one=$(echo ${dependencies_found} | grep " ${notFound}") + check_found_two=$(echo ${dependencies_found} | grep "${notFound} ") + if_not_found="${check_found_one}${check_found_two}" + if [ -z "${if_not_found}" ] ; then + dependencies_not_found="${dependencies_not_found} ${notFound}" + fi +done +# Show if all tools are installed +if [ -z "${dependencies_not_found}" ] ; then + echo > /dev/null +else + echo "" + echo "* ks-av1 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* Some required tools are not installed:${dependencies_not_found}" + echo "* The process has been stopped" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Syntax: extractFolderOrFile +function extractFolderOrFile() { + pathToExtract="${1}/" + findFolder=0 + count=1 + nameFolder=$(echo ${pathToExtract} | cut -d "/" -f ${count}) + count=$(expr $count + 1) + while [ ${findFolder} -eq 0 ] ; do + nameFolderTemp=$(echo ${pathToExtract} | cut -d "/" -f ${count}) + if [ -z "${nameFolderTemp}" ] ; then + findFolder=1 + else + nameFolder="${nameFolderTemp}" + count=$(expr $count + 1) + fi + done + echo "${nameFolder}" +} + +# Function to remove extension from file +# Syntax: removeExtension "" +function removeExtension() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Remove extensions + sed -i 's/.avi//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mp4//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mkv//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/.mov//g" "${ksToolsSedFile}" &> /dev/null + sed -i 's/.vob//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mpg//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mpeg//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.wmv//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.ogv//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.webm//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.flv//g' "${ksToolsSedFile}" &> /dev/null + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Syntax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Syntax: ks-av1 +if [ -z "${1}" ] ; then + echo "" + echo "* ks-av1 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AV1 Codec" + echo "" + echo "+ Config: ${configFile}" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Syntax: " + echo "" + echo " $ ks-av1 " + echo "" + echo " + Example: ks-av1 /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-av1 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Load profile for codecs +profile_codec=" " +selected_codec="${vcodec}" +if [ "${selected_codec}" == "libx264" ] ; then + profile_codec=" -profile:v high " +elif [ "${selected_codec}" == "libx265" ] ; then + profile_codec=" -profile:v main " +elif [ "${selected_codec}" == "libaom-av1" ] ; then + profile_codec=" -profile:v main " +elif [ "${selected_codec}" == "libsvtav1" ] ; then + profile_codec=" -profile:v main " +else + profile_codec=" " +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-${video_sync} cfr -af aresample=async=1" +# Check if video input uses H265 (HEVC) +codec_h265=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "h265") +codec_hevc=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "hevc") +yuv420p10le=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "yuv420p10le") +hevc_main10=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "Main 10") +hevc_hdr_bt=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "bt2020") +hevc_hdr_smp=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "smpte2084") +codec_h265_hevc="${codec_h265}${codec_hevc}${yuv420p10le}${hevc_main10}" +hevc_hdr="${hevc_hdr_bt}${hevc_hdr_smp}" +if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" +else + if [ -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi +fi + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-av1 (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AV1 Codec" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Syntax: " + echo "" + echo " $ ks-av1 " + echo "" + echo " + Example: ks-av1 /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "(${default_lang_audio})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${audio_default}" ] ; then + audio_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${audio_default}" ] ; then + audio_default="0:1" + else + audio_default_patch=$(echo ${audio_default} | cut -c4) + if [ "${audio_default_patch}" == ":" ] ; then + audio_default=$(echo ${audio_default} | cut -c1-3) + else + audio_default="${audio_default}" + fi + fi + else + audio_default_patch=$(echo ${audio_default} | cut -c4) + if [ "${audio_default_patch}" == ":" ] ; then + audio_default=$(echo ${audio_default} | cut -c1-3) + else + audio_default="${audio_default}" + fi + fi + # Ask for audio + echo -n "* (Default: ${audio_default}) Type the number of audio track: " ; read audio_track + if [ -z "${audio_track}" ] ; then + audio_track="${audio_default}" + else + audio_track="${audio_track}" + fi + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + if [ -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi + fi + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + echo -n "* (Default: ${name_title_default}) Type name of title: " ; read name_title + if [ -z "${name_title}" ] ; then + name_title="${name_title_default}" + else + name_title="${name_title}" + fi + echo -n "* (Default: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "7.1") + audio_5_7_1="${audio_5_7_1}${audio_5_7_1_2}" + if [ -z "${audio_5_7_1}" ] ; then + stereo_params="-ac 2" + else + stereo_params="-ac 2 -clev 3dB -slev -6dB" + fi + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AV1 Codec" + if [ ! -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles=\"${2}.srt\"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + fi + else + echo " # Convert the file '${1}' to AV1 Codec" + if [ ! -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles=\"${2}.srt\"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + else + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AV1 Codec" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to AV1 Codec" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title} (${year_file})\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title} (${year_file})\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # Execute commands for conversion + echo "" + echo -n "* (Default: y) Do you want run the conversion? (y/n): " ; read run_commands_ffmpeg + if [ "${run_commands_ffmpeg}" == "n" ] ; then + exit + else + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + if [ ! -z "${hevc_hdr}" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles="${2}.srt"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + fi + else + if [ ! -z "${hevc_hdr}" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles="${2}.srt"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title} (${year_file})" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title} (${year_file})" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi diff --git a/src/ks-av1s b/src/ks-av1s new file mode 100755 index 0000000..0ed3c21 --- /dev/null +++ b/src/ks-av1s @@ -0,0 +1,512 @@ +#!/bin/bash + +############################################################### +# ks-av1s (ks-tools) - Convert video to AV1 Codec format (Series) # +# Date: 19-02-2023 # +# Author: q3aql # +# Contact: q3aql@duck.com # +############################################################### +VERSION="8.4-dev" +M_DATE="190223" + +# Detect version ffmpeg for old releases +ffmpeg_version=$(ffmpeg -version 2>&1 | grep version | head -1 | cut -d " " -f 3 | cut -d "." -f 1) +IFS="abcdefghijklmnopqrstyvwxzABCDEFHIJKLMNOPQRSTYVWXZ@/()" +ffmpeg_version=$(echo ${ffmpeg_version}) +IFS=$' \t\n' +unset IFS +if [ -z "${ffmpeg_version}" ] ; then + video_sync="vsync" +else + if [ ${ffmpeg_version} -lt 5 ] ; then + video_sync="vsync" + else + video_sync="fps_mode" + fi +fi + +# Global parameters +dirTemp="/tmp" +listTemp="ks-tools.list" +ksToolsTempFolder="/tmp/ks-tools" +configFolder=${HOME}/.ks-tools +configFile=${configFolder}/ks-av1s + +# Basic parameters +rel_size="720x480" +vcodec="libsvtav1" +b_vcodec="850k" +acodec="libvorbis" +b_acodec="128k" +default_lang_audio="spa" +default_lang_subt="spa" +v_ext="webm" + +# Check cygwin alias (for Windows) +if [ -f "/usr/bin/cygwin-alias.sh" ] ; then + shopt -s expand_aliases + source "/usr/bin/cygwin-alias.sh" +fi + +# Create inicial config file +mkdir -p ${configFolder} +if [ -f ${configFile} ] ; then + exist_rel_size=$(cat ${configFile} | grep "rel_size=") + exist_vcodec=$(cat ${configFile} | grep "vcodec=" | cut -c2 | cut -d "_" -f 1) + exist_b_vcodec=$(cat ${configFile} | grep "b_vcodec=") + exist_acodec=$(cat ${configFile} | grep "acodec" | cut -c2 | cut -d "_" -f 1) + exist_b_acodec=$(cat ${configFile} | grep "b_acodec=") + exist_default_lang_audio=$(cat ${configFile} | grep "default_lang_audio=") + exist_default_lang_subt=$(cat ${configFile} | grep "default_lang_subt=") + exist_v_ext=$(cat ${configFile} | grep "v_ext=") + if [ -z ${exist_rel_size} ] ; then + echo "rel_size=${rel_size}" >> ${configFile} + fi + if [ -z ${exist_vcodec} ] ; then + echo "vcodec=${vcodec}" >> ${configFile} + fi + if [ -z ${exist_b_vcodec} ] ; then + echo "b_vcodec=${b_vcodec}" >> ${configFile} + fi + if [ -z ${exist_acodec} ] ; then + echo "acodec=${acodec}" >> ${configFile} + fi + if [ -z ${exist_b_acodec} ] ; then + echo "b_acodec=${b_acodec}" >> ${configFile} + fi + if [ -z ${exist_default_lang_audio} ] ; then + echo "default_lang_audio=${default_lang_audio}" >> ${configFile} + fi + if [ -z ${exist_default_lang_subt} ] ; then + echo "default_lang_subt=${default_lang_subt}" >> ${configFile} + fi + if [ -z ${exist_v_ext} ] ; then + echo "v_ext=${v_ext}" >> ${configFile} + fi + source ${configFile} +else + echo "#!/bin/bash" > ${configFile} + echo "" >> ${configFile} + echo "rel_size=${rel_size}" >> ${configFile} + echo "vcodec=${vcodec}" >> ${configFile} + echo "b_vcodec=${b_vcodec}" >> ${configFile} + echo "v_preset=${v_preset}" >> ${configFile} + echo "acodec=${acodec}" >> ${configFile} + echo "b_acodec=${b_acodec}" >> ${configFile} + echo "default_lang_audio=${default_lang_audio}" >> ${configFile} + echo "default_lang_subt=${default_lang_subt}" >> ${configFile} + echo "v_ext=${v_ext}" >> ${configFile} + source ${configFile} +fi + +# Check if ffmpeg is installed +path_check="/usr/bin /bin /usr/local/bin ${HOME}/.local/bin" +dependencies="ffmpeg grep sed grep cut head tail tr cat" +dependencies_found="" +dependencies_not_found="" +for checkPath in ${path_check} ; do + for checkDependencies in ${dependencies} ; do + if [ -f ${checkPath}/${checkDependencies} ] ; then + dependencies_found="${dependencies_found} ${checkDependencies}" + fi + done +done +for notFound in ${dependencies} ; do + check_found_one=$(echo ${dependencies_found} | grep " ${notFound}") + check_found_two=$(echo ${dependencies_found} | grep "${notFound} ") + if_not_found="${check_found_one}${check_found_two}" + if [ -z "${if_not_found}" ] ; then + dependencies_not_found="${dependencies_not_found} ${notFound}" + fi +done +# Show if all tools are installed +if [ -z "${dependencies_not_found}" ] ; then + echo > /dev/null +else + echo "" + echo "* ks-av1s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* Some required tools are not installed:${dependencies_not_found}" + echo "* The process has been stopped" + echo "" + exit +fi + +# Funcion to show the name of file/folder from full path +# Syntax: extractFolderOrFile +function extractFolderOrFile() { + pathToExtract="${1}/" + findFolder=0 + count=1 + nameFolder=$(echo ${pathToExtract} | cut -d "/" -f ${count}) + count=$(expr $count + 1) + while [ ${findFolder} -eq 0 ] ; do + nameFolderTemp=$(echo ${pathToExtract} | cut -d "/" -f ${count}) + if [ -z "${nameFolderTemp}" ] ; then + findFolder=1 + else + nameFolder="${nameFolderTemp}" + count=$(expr $count + 1) + fi + done + echo "${nameFolder}" +} + +# Function to remove extension from file +# Syntax: removeExtension "" +function removeExtension() { + wordToConvert=${1} + ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt" + mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null + echo "${wordToConvert}" > ${ksToolsSedFile} + # Remove extensions + sed -i 's/.avi//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mp4//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mkv//g' "${ksToolsSedFile}" &> /dev/null + sed -i "s/.mov//g" "${ksToolsSedFile}" &> /dev/null + sed -i 's/.vob//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mpg//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.mpeg//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.wmv//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.ogv//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.webm//g' "${ksToolsSedFile}" &> /dev/null + sed -i 's/.flv//g' "${ksToolsSedFile}" &> /dev/null + # Show file without extension + wordToConvert=$(cat ${ksToolsSedFile}) + echo ${wordToConvert} +} + +# Function to show files with spaces. +# Syntax: showFileWithSpace +function showFileWithSpace() { + echo "${1}" > ${dirTemp}/name.tmp + sed -i 's/_/ /g' ${dirTemp}/name.tmp + DisplayName=$(cat ${dirTemp}/name.tmp) + rm -rf ${dirTemp}/name.tmp + echo ${DisplayName} +} + +# Syntax: ks-av1s +if [ -z "${1}" ] ; then + echo "" + echo "* ks-av1s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AV1 Codec (Series)" + echo "" + echo "+ Config: ${configFile}" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Syntax: " + echo "" + echo " $ ks-av1s " + echo "" + echo " + Example: ks-av1s /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +fi +if [ -f "${1}" ] ; then + echo "detected" > /dev/null +else + echo "" + echo "* ks-av1s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "* The file '${1}' does not exist!" + echo "" + exit +fi + +# Load profile for codecs +profile_codec=" " +selected_codec="${vcodec}" +if [ "${selected_codec}" == "libx264" ] ; then + profile_codec=" -profile:v high " +elif [ "${selected_codec}" == "libx265" ] ; then + profile_codec=" -profile:v main " +elif [ "${selected_codec}" == "libaom-av1" ] ; then + profile_codec=" -profile:v main " +else + profile_codec=" " +fi + +# Conversion parameters +p_ffmpeg="ffmpeg -i" +p_ffmpeg_patched="-max_muxing_queue_size 9999" +f_conversion="-${video_sync} cfr -af aresample=async=1" +# Check if video input uses H265 (HEVC) +codec_h265=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "h265") +codec_hevc=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "hevc") +yuv420p10le=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "yuv420p10le") +hevc_main10=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "Main 10") +hevc_hdr_bt=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "bt2020") +hevc_hdr_smp=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | grep "smpte2084") +codec_h265_hevc="${codec_h265}${codec_hevc}${yuv420p10le}${hevc_main10}" +hevc_hdr="${hevc_hdr_bt}${hevc_hdr_smp}" +if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" +else + if [ -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi +fi + +# Init conversion file +current_date=$(date +%Y) +if [ -z "${2}" ] ; then + echo "" + echo "* ks-av1s (ks-tools) v${VERSION} (${M_DATE})" + echo "" + echo "- Convert video file(s) to compact and efficient AV1 Codec (Series)" + echo "" + echo "+ Config:" + echo "" + echo " - Resolution: ${rel_size}" + echo " - Video codec: ${vcodec}" + echo " - Bitrate video: ${b_vcodec}" + echo " - Audio codec: ${acodec} (stereo)" + echo " - Bitrate audio: ${b_acodec}" + echo " - Default Audio: ${default_lang_audio}" + echo " - Default Subtitle: ${default_lang_subt} (forced)" + echo " - Container: ${v_ext}" + echo "" + echo "+ Syntax: " + echo "" + echo " $ ks-av1s " + echo "" + echo " + Example: ks-av1s /data/movies/Example.mkv /data/converted/Example" + echo "" + exit +else + echo "" + echo "* Information of ${1}:" + echo "" + echo "+ Video Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d "," -f 1 + echo "" + echo "+ Audio Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d "," -f 1 + echo "" + echo "+ Subtitle Tracks:" + ${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d "," -f 1 + echo "" + # Check de video track by default + video_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Video:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${video_default}" ] ; then + video_default="0:0" + else + video_default_patch=$(echo ${video_default} | cut -c4) + if [ "${video_default_patch}" == ":" ] ; then + video_default=$(echo ${video_default} | cut -c1-3) + else + video_default="${video_default}" + fi + fi + # Ask for video + echo -n "* (Default: ${video_default}) Type the number of video track: " ; read video_track + if [ -z "${video_track}" ] ; then + video_track="${video_default}" + else + video_track="${video_track}" + fi + # Check the audio track by default + audio_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "(${default_lang_audio})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${audio_default}" ] ; then + audio_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${audio_default}" ] ; then + audio_default="0:1" + else + audio_default_patch=$(echo ${audio_default} | cut -c4) + if [ "${audio_default_patch}" == ":" ] ; then + audio_default=$(echo ${audio_default} | cut -c1-3) + else + audio_default="${audio_default}" + fi + fi + else + audio_default_patch=$(echo ${audio_default} | cut -c4) + if [ "${audio_default_patch}" == ":" ] ; then + audio_default=$(echo ${audio_default} | cut -c1-3) + else + audio_default="${audio_default}" + fi + fi + # Ask for audio + echo -n "* (Default: ${audio_default}) Type the number of audio track: " ; read audio_track + if [ -z "${audio_track}" ] ; then + audio_track="${audio_default}" + else + audio_track="${audio_track}" + fi + # Check the subtitle track by default + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | grep "(forced)" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | grep "(${default_lang_subt})" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Subtitle:" | cut -d " " -f 3 | cut -c2-5 | cut -d "(" -f 1 | cut -d "[" -f 1 | head -1) + if [ -z "${subtitle_default}" ] ; then + subtitle_default="0:3" + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + else + subtitle_default_patch=$(echo ${subtitle_default} | cut -c4) + if [ "${subtitle_default_patch}" == ":" ] ; then + subtitle_default=$(echo ${subtitle_default} | cut -c1-3) + else + subtitle_default="${subtitle_default}" + fi + fi + # Ask for subtitle + echo -n "* (Default: n) Do you want include subtitles? (y/n): " ; read subtitles_y_n + if [ "${subtitles_y_n}" == "y" ] ; then + echo -n "* (Default: ${subtitle_default}) Type the number of subtitle track: " ; read subtitle_track + if [ -z "${subtitle_track}" ] ; then + subtitle_track="${subtitle_default}" + else + subtitle_track="${subtitle_track}" + fi + fi + echo -n "* (Default: ${rel_size}) Type the resolution: " ; read resolution + if [ -z "${resolution}" ] ; then + resolution="${rel_size}" + else + rel_size="${resolution}" + if [ -z "${codec_h265_hevc}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + if [ -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec}${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + else + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + fi + fi + fi + echo -n "* (Default: n) Do you want apply '-max_muxing_queue_size 9999' patch? (y/n): " ; read patch_thread + if [ "${patch_thread}" == "y" ] ; then + patch_thread="y" + else + patch_thread="n" + fi + echo "" + echo "* METADATA configuration:" + echo "" + # Prepare name title by default + FullPath="${1}" + name_title_default=$(extractFolderOrFile "${FullPath}") + name_title_default=$(removeExtension "${name_title_default}") + name_title_default=$(showFileWithSpace "${name_title_default}") + # Ask name title, year and genre + echo -n "* (Default: ${name_title_default}) Type name of title: " ; read name_title + if [ -z "${name_title}" ] ; then + name_title="${name_title_default}" + else + name_title="${name_title}" + fi + echo -n "* (Default: ${current_date}) Type the year: " ; read year_file + if [ -z "${year_file}" ] ; then + year_file="${current_date}" + else + year_file="${year_file}" + fi + echo -n "* (Default: Unknown) Type the genre: " ; read genre_file + if [ -z "${genre_file}" ] ; then + genre_file="Unknown" + else + genre_file="${genre_file}" + fi + + # Check audio 5.1 or 7.1 + audio_5_7_1=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "5.1") + audio_5_7_1_2=$(${p_ffmpeg} "${1}" 2>&1 | grep Stream | tr -s " " | grep "Audio:" | grep "${audio_track}" | grep -i "7.1") + audio_5_7_1="${audio_5_7_1}${audio_5_7_1_2}" + if [ -z "${audio_5_7_1}" ] ; then + stereo_params="-ac 2" + else + stereo_params="-ac 2 -clev 3dB -slev -6dB" + fi + # Show commands for conversion + echo "" + echo "* COMMANDS THAT WILL BE EXECUTED:" + echo "" + if [ "${subtitles_y_n}" == "y" ] ; then + echo " # Extract subtitles from file ${1}" + echo " ${p_ffmpeg} \"${1}\" -map ${subtitle_track} \"${2}.srt\"" + echo "" + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AV1 Codec" + if [ ! -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles=\"${2}.srt\"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + fi + else + echo " # Convert the file '${1}' to AV1 Codec" + if [ ! -z "${hevc_hdr}" ] ; then + p_conversion="-s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles=\"${2}.srt\"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec}" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + else + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles=\"${2}.srt\" ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + else + if [ "${patch_thread}" == "y" ] ; then + echo " # Convert the file '${1}' to AV1 Codec" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" ${p_ffmpeg_patched} \"${2}.${v_ext}\"" + else + echo " # Convert the file '${1}' to AV1 Codec" + echo " ${p_ffmpeg} \"${1}\" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title=\"${name_title}\" -metadata date=\"${year_file}\" -metadata genre=\"${genre_file}\" -metadata:s:v:0 title=\"${name_title}\" -metadata:s:a:0 title=\"${acodec} Stereo Audio (${b_acodec})\" \"${2}.${v_ext}\"" + fi + fi + # Execute commands for conversion + echo "" + echo -n "* (Default: y) Do you want run the conversion? (y/n): " ; read run_commands_ffmpeg + if [ "${run_commands_ffmpeg}" == "n" ] ; then + exit + else + if [ "${subtitles_y_n}" == "y" ] ; then + ${p_ffmpeg} "${1}" -map ${subtitle_track} "${2}.srt" + if [ "${patch_thread}" == "y" ] ; then + if [ ! -z "${hevc_hdr}" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles="${2}.srt"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + fi + else + if [ ! -z "${hevc_hdr}" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -s ${rel_size} -c:v ${vcodec} -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,subtitles="${2}.srt"${profile_codec}-pix_fmt yuv420p -b:v ${b_vcodec} -c:a ${acodec} -b:a ${b_acodec} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} -vf subtitles="${2}.srt" ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + else + if [ "${patch_thread}" == "y" ] ; then + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" ${p_ffmpeg_patched} "${2}.${v_ext}" + else + ${p_ffmpeg} "${1}" ${f_conversion} -map ${video_track} -map ${audio_track} ${p_conversion} ${stereo_params} -metadata title="${name_title}" -metadata date="${year_file}" -metadata genre="${genre_file}" -metadata:s:v:0 title="${name_title}" -metadata:s:a:0 title="${acodec} Stereo Audio (${b_acodec})" "${2}.${v_ext}" + fi + fi + fi +fi