From 7ab0771eb9f7c3752e6a02a81f019582383919f8 Mon Sep 17 00:00:00 2001 From: Bruno21 Date: Thu, 16 Nov 2023 16:43:13 +0100 Subject: [PATCH] Bugfix --- handbrake_for_plex.sh | 327 ++++++++++++++++++++++++++++++------------ 1 file changed, 239 insertions(+), 88 deletions(-) diff --git a/handbrake_for_plex.sh b/handbrake_for_plex.sh index 06017d8..f323cee 100755 --- a/handbrake_for_plex.sh +++ b/handbrake_for_plex.sh @@ -13,6 +13,61 @@ reset="\033[0m" shopt -s globstar +# https://stackoverflow.com/questions/59895/how-can-i-get-the-directory-where-a-bash-script-is-located-from-within-the-scrip +#DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")") +#SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +#DIR_SCRIPT=$(dirname -- "$( readlink -f -- "$0"; )") + +#### +# Configuration + +# Source video folder +[[ "$input_path" == "" ]] && SRC="$HOME/Downloads" || SRC="$input_path" + +# Destination video folder +[[ "$output_path" == "" ]] && DEST="$HOME/Movies" || DEST="$output_path" + +# Extension video file +DEST_EXT=mp4 + +# Handbrake preset +[[ "$profile" == "" ]] && PRESET="Apple 720p30 Surround" || PRESET="$profile" + +# Only files larger than $MAXSIZE will be processed (> 30Mo) +MAXSIZE=30000000 +#MAXSIZE=300 + +# Open converted video file in an application +APP="Subler" + +SUBTITLE_LIST="eng,fre" + +# log +# this reduce handbrake verbosity +#logfile=/tmp/HandBrake.log +logfolder=/var/log/handbrake_for_plex +logfile="$logfolder"/HandBrake.log + +if [ ! -w "$logfile" ]; then + sudo mkdir "$logfolder" + sudo chown bruno:staff "$logfolder" + sudo touch "$logfile" + sudo chown bruno:staff "$logfile" + sudo chmod 640 "$logfile" +fi + +logsize=$(wc -c <"$logfile") + +if [ $logsize -ge 1000 ]; then + cp "$logfile" "$logfile.old" + truncate -s 0 "$logfile" +fi + + +#Move to trash after conversion +trash=false + + command -v jq >/dev/null 2>&1 || { echo -e "${bold}93mhandbrake_for_plex${reset} require ${bold}jq${reset} but it's not installed.\nRun ${italic}(brew install jq)${reset}\nAborting..." >&2; exit 1; } fzf_bin=0 @@ -95,6 +150,7 @@ installation() { else echo -e "${red}This folder does not exist !${reset}" fi + } notification() { @@ -142,41 +198,6 @@ done shift $((OPTIND-1)) -# https://stackoverflow.com/questions/59895/how-can-i-get-the-directory-where-a-bash-script-is-located-from-within-the-scrip -#DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")") -#SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -#DIR_SCRIPT=$(dirname -- "$( readlink -f -- "$0"; )") - -#### -# Configuration - -# Source video folder -[[ "$input_path" == "" ]] && SRC="$HOME/Downloads" || SRC="$input_path" - -# Destination video folder -[[ "$output_path" == "" ]] && DEST="$HOME/Movies" || DEST="$output_path" - -# Extension video file -DEST_EXT=mp4 - -# Handbrake preset -[[ "$profile" == "" ]] && PRESET="Apple 720p30 Surround" || PRESET="$profile" - -# Only files larger than $MAXSIZE will be processed (> 30Mo) -MAXSIZE=30000000 -#MAXSIZE=300 - -# Open converted video file in an application -APP="Subler" - -SUBTITLE_LIST="eng,fre" - -# log -# this reduce handbrake verbosity -logfile=/tmp/HandBrake.log - -#Move to trash after conversion -trash=true ### @@ -221,8 +242,8 @@ echo -e "HandBrake Profile: ${italic}$PRESET${reset}" count=0 # Series: SxxEyy REGEX="([sS]([0-9]{2,}|[X]{2,})[eE]([0-9]{2,}|[Y]{2,}))" -# Films: 2 -REGEX2="[0-9]{4}" +# Films: +REGEX2=".[0-9]{4}" # année 2019 mais avec un caractère avant (chaine ne commence pas par 2019) movies=() for FILE in "${SRC}"/**/*.{mkv,avi,mp4,m4v} @@ -242,7 +263,6 @@ do #extension=${filename##*.} filename=${filename%.*} - : <<'END_COMMENT' https://stackoverflow.com/questions/41231998/mediainfo-cli-command-line-interface-syntax-teaching-me-once-for-all media=$(mediainfo --Output=file:///$SCRIPT_DIR/template.txt "$FILE") @@ -251,12 +271,16 @@ do echo -e "$info\n" END_COMMENT + # Suprrime [ Torrent911.io ] + a="[${filename#*[}" + a="${a%]*}] " + filename=${filename#"$a"} # Séries if [[ $filename =~ $REGEX ]]; then MATCH="${BASH_REMATCH[1]}" # Remplace les . par des espaces - e=$(echo "${filename%$MATCH*}" | sed 's/\./\ /g' | xargs) + e=$(echo "${filename%$MATCH*}" | sed 's/\./\ /g' | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//') # Capitalise chaque mot f=( $e ) g=${f[@]^} @@ -265,14 +289,20 @@ END_COMMENT # Films elif [[ $filename =~ $REGEX2 ]]; then MATCH2="${BASH_REMATCH[0]}" - e=$(echo "${filename%$MATCH2*}" | sed 's/\./\ /g' | xargs) + MATCH2=${MATCH2:1} + + e=$(echo "${filename%$MATCH2*}" | sed 's/\./\ /g' | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//') new_name="$e ($MATCH2).$DEST_EXT" else - echo "Could not find SXXEYY pattern" - new_name="$filename" - continue - fi - + echo -e "${red}\nCould not find SXXEYY or YYYY pattern${reset}" + e=$(echo "${filename}" | sed 's/\./\ /g' | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//') + read -e -p "Title: " -i "$e" e + new_name="$e.$DEST_EXT" + #echo "new_name: $new_name" + #continue + fi + + if ! command -v mediainfo &> /dev/null; then echo -e "${bold}mediainfo${reset} could not be found !\n" echo -e "You should install ${bold}mediainfo${reset}:\n" @@ -280,70 +310,187 @@ END_COMMENT echo -e "" else - infos=$(mediainfo "$FILE" --output=JSON | jq -s 'map({ VideoCount: .media.track[0].VideoCount, AudioCount: .media.track[0].AudioCount, TextCount: .media.track[0].TextCount, MenuCount: .media.track[0].MenuCount, FormatFichier: .media.track[0].Format, FileSize: .media.track[0].FileSize, Duration: .media.track[0].Duration, FormatVideo: .media.track[1].Format, Format_Profile: .media.track[1].Format_Profile, Format_Level: .media.track[1].Format_Level, "CodecVideo": .media.track[1].CodecID, Width: .media.track[1].Width, Height: .media.track[1].Height, FormatAudio: .media.track[2].Format, FormatAdditionalFeatures: .media.track[2].Format_AdditionalFeatures, CodecAudio: .media.track[2].CodecID, TitleAudio: .media.track[2].Title, LanguageAudio: .media.track[2].Language, DefaultAudio: .media.track[2].Default, FormatText: .media.track[3].Format, CodecText: .media.track[3].CodecID, LanguageText: .media.track[3].Language, DefaultText: .media.track[3].Default, Menu: .media.track[4].extra})') + #infos=$(mediainfo "$FILE" --output=JSON | jq -s 'map({ VideoCount: .media.track[0].VideoCount, AudioCount: .media.track[0].AudioCount, TextCount: .media.track[0].TextCount, MenuCount: .media.track[0].MenuCount, FormatFichier: .media.track[0].Format, FileSize: .media.track[0].FileSize, Duration: .media.track[0].Duration, FormatVideo: .media.track[1].Format, Format_Profile: .media.track[1].Format_Profile, Format_Level: .media.track[1].Format_Level, "CodecVideo": .media.track[1].CodecID, Width: .media.track[1].Width, Height: .media.track[1].Height, FormatAudio: .media.track[2].Format, FormatAdditionalFeatures: .media.track[2].Format_AdditionalFeatures, CodecAudio: .media.track[2].CodecID, TitleAudio: .media.track[2].Title, LanguageAudio: .media.track[2].Language, DefaultAudio: .media.track[2].Default, FormatText: .media.track[3].Format, CodecText: .media.track[3].CodecID, LanguageText: .media.track[3].Language, DefaultText: .media.track[3].Default, Menu: .media.track[4].extra})') + infos=$(mediainfo "$FILE" --output=JSON | jq '.media.track') + + #echo "$infos" | jq + + while read row + do + type=$(echo "$row" | jq -r '.["@type"]') + if [[ "$type" == "General" ]]; then + general="$row" + #echo "$general" | jq + elif [[ "$type" == "Video" ]]; then + video="$row" + #echo "$video" | jq + elif [[ "$type" == "Audio" ]]; then + audio="$row" + #echo "$audio" | jq + elif [[ "$type" == "Text" ]]; then + text="$row" + echo "$text" | jq + elif [[ "$type" == "Menu" ]]; then + menu="$row" + echo "$menu" | jq + fi + done < <(echo "$infos" | jq -c '.[]') + # General - #nb_video=$(echo "$infos" | jq -j '.[] | .VideoCount') - nb_audio=$(echo "$infos" | jq -j '.[] | .AudioCount') - if [[ "$nb_audio" == "null" ]]; then nb_audio=0; fi - nb_text=$(echo "$infos" | jq -j '.[] | .TextCount') - if [[ "$nb_text" == "null" ]]; then nb_text=0; fi - nb_menu=$(echo "$infos" | jq -j '.[] | .MenuCount') + ##nb_video=$(echo "$infos" | jq -j '.[] | .VideoCount') + #nb_audio=$(echo "$infos" | jq -j '.[] | .AudioCount') + #if [[ "$nb_audio" == "null" ]]; then nb_audio=0; fi + #nb_text=$(echo "$infos" | jq -j '.[] | .TextCount') + #if [[ "$nb_text" == "null" ]]; then nb_text=0; fi + #nb_menu=$(echo "$infos" | jq -j '.[] | .MenuCount') + #if [[ "$nb_menu" == "null" ]]; then nb_menu=0; fi + #format_fichier=$(echo "$infos" | jq -j '.[] | .FormatFichier') + #filesize=$(echo "$infos" | jq -j '.[] | .FileSize' | numfmt --to=si --format "%8.2f" | xargs) + ##fsize=$(numfmt --to iec --format "%8.2f" "$filesize") + #duree=$(echo "$infos" | jq -j '.[] | .Duration' | awk -F "." '{print $1}' | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}' | xargs) + + nb_audio=$(echo "$general" | jq -j '.AudioCount') + if [[ "$nb_audio" == "null" ]]; then nb_audio=0; fi + nb_text=$(echo "$general" | jq -j '.TextCount') + if [[ "$nb_text" == "null" ]]; then nb_text=0; fi + nb_menu=$(echo "$general" | jq -j '.MenuCount') if [[ "$nb_menu" == "null" ]]; then nb_menu=0; fi - format_fichier=$(echo "$infos" | jq -j '.[] | .FormatFichier') - filesize=$(echo "$infos" | jq -j '.[] | .FileSize' | numfmt --to=si --format "%8.2f" | xargs) - #fsize=$(numfmt --to iec --format "%8.2f" "$filesize") - duree=$(echo "$infos" | jq -j '.[] | .Duration' | awk -F "." '{print $1}' | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}' | xargs) + + format_fichier=$(echo "$general" | jq -j '.Format') + filesize=$(echo "$general" | jq -j '.FileSize' | numfmt --to=si --format "%8.2f" | xargs) + duree=$(echo "$general" | jq -j '.Duration' | awk -F "." '{print $1}' | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}' | xargs) + + #echo -e "${red}nb_audio: $nb_audio${reset}" + #echo -e "${red}nb_text: $nb_text${reset}" + #echo -e "${red}nb_menu: $nb_menu${reset}" + + #echo "format_fichier: $format_fichier" + #echo "filesize: $filesize" + #echo "duree: $duree" + # Video - format_video=$(echo "$infos" | jq -j '.[] | .FormatVideo') - format_profile=$(echo "$infos" | jq -j '.[] | .Format_Profile') - format_level=$(echo "$infos" | jq -j '.[] | .Format_Level') - codec_video=$(echo "$infos" | jq -j '.[] | .CodecVideo') - width=$(echo "$infos" | jq -j '.[] | .Width') - height=$(echo "$infos" | jq -j '.[] | .Height') + #format_video=$(echo "$infos" | jq -j '.[] | .FormatVideo') + #format_profile=$(echo "$infos" | jq -j '.[] | .Format_Profile') + #format_level=$(echo "$infos" | jq -j '.[] | .Format_Level') + #codec_video=$(echo "$infos" | jq -j '.[] | .CodecVideo') + #width=$(echo "$infos" | jq -j '.[] | .Width') + #height=$(echo "$infos" | jq -j '.[] | .Height') + + format_video=$(echo "$video" | jq -j '.Format | select( . != null )') + format_profile=$(echo "$video" | jq -j '.Format_Profile | select( . != null )') + format_level=$(echo "$video" | jq -j '.Format_Level | select( . != null )') + codec_video=$(echo "$video" | jq -j '.CodecID | select( . != null )') + width=$(echo "$video" | jq -j '.Width | select( . != null )') + height=$(echo "$video" | jq -j '.Height | select( . != null )') + + #echo "format_video: $format_video" + #echo "format_profile: $format_profile" + #echo "format_level: $format_level" + #echo "codec_video: $codec_video" + #echo "width: $width" + #echo "height: $height" # Audio - format_audio=$(echo "$infos" | jq -j '.[] | .FormatAudio') - format_addition=$(echo "$infos" | jq -j '.[] | .FormatAdditionalFeatures') - if [[ "$format_addition" == "null" ]]; then format_addition=""; fi - codec_audio=$(echo "$infos" | jq -j '.[] | .CodecAudio') - title_audio=$(echo "$infos" | jq -j '.[] | .TitleAudio') - language_audio=$(echo "$infos" | jq -j '.[] | .LanguageAudio') - if [[ "$language_audio" == "null" ]]; then language_audio=""; fi - default_audio=$(echo "$infos" | jq -j '.[] | .DefaultAudio') - if [[ "$default_audio" == "null" ]]; then default_audio=""; fi + #format_audio=$(echo "$infos" | jq -j '.[] | .FormatAudio') + #format_addition=$(echo "$infos" | jq -j '.[] | .FormatAdditionalFeatures') + #if [[ "$format_addition" == "null" ]]; then format_addition=""; fi + #codec_audio=$(echo "$infos" | jq -j '.[] | .CodecAudio') + #title_audio=$(echo "$infos" | jq -j '.[] | .TitleAudio') + #language_audio=$(echo "$infos" | jq -j '.[] | .LanguageAudio') + #if [[ "$language_audio" == "null" ]]; then language_audio=""; fi + #default_audio=$(echo "$infos" | jq -j '.[] | .DefaultAudio') + #if [[ "$default_audio" == "null" ]]; then default_audio=""; fi + + format_audio=$(echo "$audio" | jq -j '.Format | select( . != null )') + format_addition=$(echo "$audio" | jq -j '.Format_AdditionalFeatures | select( . != null )') + #if [[ "$format_addition" == "null" ]]; then format_addition=""; fi + codec_audio=$(echo "$audio" | jq -j '.CodecID | select( . != null )') + title_audio=$(echo "$audio" | jq -j '.Title | select( . != null )') + language_audio=$(echo "$audio" | jq -j '.Language | select( . != null )') + #if [[ "$language_audio" == "null" ]]; then language_audio=""; fi + default_audio=$(echo "$audio" | jq -j '.Default | select( . != null )') + #if [[ "$default_audio" == "null" ]]; then default_audio=""; fi + + #echo "format_audio: $format_audio" + #echo "format_addition: $format_addition" + #echo "codec_audio: $codec_audio" + #echo "title_audio: $title_audio" + #echo "language_audio: $language_audio" + #echo "default_audio: $default_audio" # Text - format_text=$(echo "$infos" | jq -j '.[] | .FormatText') - codec_text=$(echo "$infos" | jq -j '.[] | .CodecText') - language_text=$(echo "$infos" | jq -j '.[] | .LanguageText') - default_text=$(echo "$infos" | jq -j '.[] | .DefaultText') + #format_text=$(echo "$infos" | jq -j '.[] | .FormatText') + #codec_text=$(echo "$infos" | jq -j '.[] | .CodecText') + #language_text=$(echo "$infos" | jq -j '.[] | .LanguageText') + #default_text=$(echo "$infos" | jq -j '.[] | .DefaultText') + format_text=$(echo "$text" | jq -j '.FormatText | select( . != null )') + codec_text=$(echo "$text" | jq -j '.CodecText | select( . != null )') + language_text=$(echo "$text" | jq -j '.LanguageText | select( . != null )') + default_text=$(echo "$text" | jq -j '.DefaultText | select( . != null )') + + if [ "$nb_text" -ge 1 ]; then + echo "format_text: $format_text" + echo "codec_text: $codec_text" + echo "language_text: $language_text" + echo "default_text: $default_text" + fi + # Menu - menu=$(echo "$infos" | jq -j '.[] | .Menu' | sed '1d;$d') + + #menu=$(echo "$infos" | jq -j '.[] | .Menu' | sed '1d;$d') + menus=$(echo "$menu" | jq -j '.extra' | sed '1d;$d') + #echo "menus: $menus" + m=() + n=1 while IFS= read -r line do - t=$(echo "$line" | awk -F": " '{print $1}') - c=$(echo "$line" | awk -F": " '{print $2}') + # L'apparition + # "_00_07_57_920": "en:00:07:57.920", + + # Kandahar + # "_00_00_00_000": "1. Studio Logo", + + REGEX1="^_(0[0-9]|1[0-9]|2[0-3])_([0-5][0-9])_([0-5][0-9])_([0-9][0-9][0-9])$" - tt=$(echo "$t" | sed -n '1 s/"//gp' | xargs | sed 's/^_//' | sed 's/_/h/1' | sed 's/_/mm/1' | sed 's/_/s/1' | sed 's/s.*//') - #cc=$(echo "$c" | tr -d '"' | xargs | awk -F ":" '{print $2}' | sed 's/,$//' | sed 's/^/ /') - cc=$(echo "$c" | tr -d '"' | xargs | sed 's/,$//' | sed 's/^/ /') + if [[ ${line:3:13} =~ $REGEX1 ]]; then + + t=$(echo "$line" | awk -F": " '{print $1}') + c=$(echo "$line" | awk -F": " '{print $2}') + #echo "t: $t" + #echo "c: $c" + # L'apparition + # t: "_00_07_57_920" + # c: "en:00:07:57.920", + - m+=("$cc : $tt") - done <<< "$menu" + tt=$(echo "$t" | sed -n '1 s/"//gp' | xargs | sed 's/^_//' | sed 's/_/h/1' | sed 's/_/mm/1' | sed 's/_/s/1' | sed 's/s.*//') + #cc=$(echo "$c" | tr -d '"' | xargs | awk -F ":" '{print $2}' | sed 's/,$//' | sed 's/^/ /') + cc=$(echo "$c" | tr -d '"' | xargs | sed 's/,$//' | sed 's/^/ /' | xargs) + #echo "tt: $tt" + #echo "cc: $cc" + # L'apparition + # tt: 00h07mm57 + # cc: en:00:07:57.920 + + #m+=("$n:$tt") + m+=("$n:$cc ($tt)") + ((n=n+1)) + fi + done <<< "$menus" - echo -e "\n\n${bold}(${red}$(( count + 1 ))${reset}${bold} / $nb_movies) - $filename${reset}" + echo -e "\n\n${bold}(${red}$(( count + 1 ))${reset}${bold} / $nb_movies) $filename${reset}" echo -e "\n${bold}General:${reset}" printf " %-20s %-35s \n" "Format de fichier:" "${format_fichier}" printf " %-20s %-35s \n" "Taille de fichier:" "${filesize}" - printf " %-21s %-35s \n" "Duree:" "${duree}" + printf " %-20s %-35s \n" "Duree:" "${duree}" echo -e "\n${bold}Video:${reset}" - printf " %-21s %-35s \n" "Format video:" "${format_video}" + printf " %-20s %-35s \n" "Format video:" "${format_video}" printf " %-20s %-35s \n" "Profile:" "${format_profile}" printf " %-20s %-35s \n" "Level:" "${format_level}" printf " %-20s %-35s \n" "Codec:" "${codec_video}" @@ -369,6 +516,9 @@ END_COMMENT echo -e "\n${bold}Menu:${reset}" for elem in "${m[@]}"; do + #echo "${elem}" + # en:00:07:57.920 : 00h07mm57 + c=$(echo "${elem}" | awk -F":" '{print $1}') t=$(echo "${elem}" | awk -F":" '{print $2}') printf " %-20s %-35s \n" "$c:" "${t}" @@ -386,15 +536,16 @@ END_COMMENT if [ "$nb_text" -ge 1 ] && [[ $SUBTITLE_LIST =~ ${language_text} ]]; then echo -e "$z (with ${bold}${language_text}${reset} subtitle track)" - $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" --subtitle-lang-list "$SUBTITLE_LIST" --all-subtitles 2> $logfile + $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" --subtitle-lang-list "$SUBTITLE_LIST" --all-subtitles 2>> $logfile else echo -e "$z" - $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" 2> $logfile + $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" 2>> $logfile fi echo result=$? if [ "$result" = 0 ]; then + echo -e "${red}$new_name available in $DEST${reset}" notification "HandBrake for Plex" "$new_name available in $DEST" $result fi