diff --git a/handbrake_for_plex.sh b/handbrake_for_plex.sh index f323cee..98b4f3a 100755 --- a/handbrake_for_plex.sh +++ b/handbrake_for_plex.sh @@ -65,7 +65,7 @@ fi #Move to trash after conversion -trash=false +trash=true 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; } @@ -240,6 +240,7 @@ echo -e "HandBrake Profile: ${italic}$PRESET${reset}" #ls "$SRC"/**/*.{mkv,avi,mp4,m4v} count=0 + # Series: SxxEyy REGEX="([sS]([0-9]{2,}|[X]{2,})[eE]([0-9]{2,}|[Y]{2,}))" # Films: @@ -263,6 +264,12 @@ do #extension=${filename##*.} filename=${filename%.*} + declare -a ft=() + declare -a ct=() + declare -a tt=() + declare -a lt=() + declare -a dt=() + : <<'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") @@ -314,129 +321,79 @@ END_COMMENT 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 + + 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 "$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) + elif [[ "$type" == "Video" ]]; then video="$row" #echo "$video" | jq + + fmt=$(echo "$video" | jq -j '.Format | select( . != null )') + + if [ $fmt != "JPEG" ]; then + 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 )') + fi + elif [[ "$type" == "Audio" ]]; then audio="$row" #echo "$audio" | jq + + format_audio=$(echo "$audio" | jq -j '.Format | select( . != null )') + format_addition=$(echo "$audio" | jq -j '.Format_AdditionalFeatures | select( . != null )') + format_commercial=$(echo "$audio" | jq -j '.Format_Commercial_IfAny | select( . != null )') + 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 )') + default_audio=$(echo "$audio" | jq -j '.Default | select( . != null )') + elif [[ "$type" == "Text" ]]; then text="$row" - echo "$text" | jq + #echo "$text" | jq + + format_text=$(echo "$text" | jq -j '.Format | select( . != null )') + codec_text=$(echo "$text" | jq -j '.CodecID | select( . != null )') + title_text=$(echo "$text" | jq -j '.Title | select( . != null )') + language_text=$(echo "$text" | jq -j '.Language | select( . != null )') + default_text=$(echo "$text" | jq -j '.Default | select( . != null )') + + ft+=("$format_text") + ct+=("$codec_text") + tt+=("$title_text") + lt+=("$language_text") + dt+=("$default_text") + elif [[ "$type" == "Menu" ]]; then menu="$row" - echo "$menu" | jq + #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') - #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) + + echo -e "${red}nb_audio: $nb_audio${reset}" + echo -e "${red}nb_text: $nb_text${reset}" + echo -e "${red}nb_menu: $nb_menu${reset}" - 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 "$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 "$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 "$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 "$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 @@ -482,7 +439,8 @@ END_COMMENT fi done <<< "$menus" - echo -e "\n\n${bold}(${red}$(( count + 1 ))${reset}${bold} / $nb_movies) $filename${reset}" + ((count++)) + echo -e "\n\n${bold}(${red}$count${reset}${bold} / $nb_movies) $filename${reset}" echo -e "\n${bold}General:${reset}" printf " %-20s %-35s \n" "Format de fichier:" "${format_fichier}" @@ -499,18 +457,37 @@ END_COMMENT echo -e "\n${bold}Audio:${reset}" printf " %-20s %-35s \n" "Format audio:" "${format_audio}" printf " %-20s %-35s \n" "Format additionnel:" "${format_addition}" + printf " %-20s %-35s \n" "Format commercial:" "${format_commercial}" printf " %-20s %-35s \n" "Codec:" "${codec_audio}" printf " %-20s %-35s \n" "Titre:" "${title_audio}" printf " %-20s %-35s \n" "Language:" "${language_audio}" printf " %-20s %-35s \n" "Defaut:" "${default_audio}" + #if [ "$nb_text" -ge 1 ]; then + # echo -e "\n${bold}Text:${reset}" + # printf " %-20s %-35s \n" "Titre:" "${title_text}" + # printf " %-20s %-35s \n" "Format:" "${format_text}" + # printf " %-20s %-35s \n" "Codec:" "${codec_text}" + # printf " %-20s %-35s \n" "Language:" "${language_text}" + # printf " %-20s %-35s \n" "Defaut:" "${default_text}" + #fi + if [ "$nb_text" -ge 1 ]; then - echo -e "\n${bold}Text:${reset}" - printf " %-20s %-35s \n" "Format text:" "${format_text}" - printf " %-20s %-35s \n" "Codec:" "${codec_text}" - printf " %-20s %-35s \n" "Language:" "${language_text}" - printf " %-20s %-35s \n" "Defaut:" "${default_text}" + + for val in ${!ft[@]} + do + index=${val} + ((index++)) + + echo -e "\n${bold}Text $index:${reset}" + printf " %-20s %-35s \n" "Titre:" "${tt[$val]}" + printf " %-20s %-35s \n" "Format:" "${ft[$val]}" + printf " %-20s %-35s \n" "Codec:" "${ct[$val]}" + printf " %-20s %-35s \n" "Language:" "${lt[$val]}" + printf " %-20s %-35s \n" "Defaut:" "${dt[$val]}" + done fi + if [ "$nb_menu" -ge 1 ]; then echo -e "\n${bold}Menu:${reset}" @@ -534,8 +511,12 @@ END_COMMENT #echo -e "\n${yellow}Convert $FILE${reset} ${bold}->${reset} ${green}$file_export${reset}" z="\n${yellow}Convert $FILE${reset} ${bold}->${reset} ${green}$file_export${reset}" - if [ "$nb_text" -ge 1 ] && [[ $SUBTITLE_LIST =~ ${language_text} ]]; then - echo -e "$z (with ${bold}${language_text}${reset} subtitle track)" + #echo -e "${red}language_text: ${language_text}${reset}" # fr-FR (killers of...) fr-FR (yannick) + #echo -e "${red}lt: ${lt[@]}${reset}" # fr fr-FR (killers of...) fr-FR (yannick) + #echo -e "${red}SUBTITLE_LIST: $SUBTITLE_LIST${reset}" # eng,fre + + if [ "$nb_text" -ge 1 ]; then + echo -e "$z (with ${bold}${language_text}${reset} subtitle track)" # with fr-FR $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" --subtitle-lang-list "$SUBTITLE_LIST" --all-subtitles 2>> $logfile else echo -e "$z" @@ -545,7 +526,7 @@ END_COMMENT result=$? if [ "$result" = 0 ]; then - echo -e "${red}$new_name available in $DEST${reset}" + echo -e "${green}$new_name available in $DEST${reset}" notification "HandBrake for Plex" "$new_name available in $DEST" $result fi @@ -555,7 +536,10 @@ END_COMMENT if [[ $(command -v mdfind) == "" ]]; then echo -e "\n${red}mdfind not find ! mdfind is a part macOS.${reset}" else - x=$(mdfind -name "$APP" kind:application) + #x=$(mdfind -name "$APP" kind:application) + # /usr/bin/mdfind $@ 2> >(grep --invert-match ' \[UserQueryParser\] ' >&2) + x=$(mdfind -name "$APP" kind:application 2> /dev/null) + if [ "$x" != "" ]; then if [ -f "$file_export" ]; then echo -e "\n${bold}Opening $file_export in $APP...${reset}" @@ -581,22 +565,23 @@ END_COMMENT [ "$(echo "${parent_folder}/"*)" = "${parent_folder}/*" ] && rm -rf "${parent_folder}"; echo "" - ((count++)) else echo -e "\n${green}$file_export already transcoded !${reset}\n" - + ((already_transcoded+=1)) fi + FILESIZE= done +transcoded=$((count-already_transcoded)) -if [ "$count" -eq 0 ]; then +if [ "$transcoded" -eq 0 ]; then echo -e "\n${red}No file to transcode !${reset}" else - echo -e "\n${green}${count} files successfully transcoded !${reset}" + echo -e "\n${green}${transcoded} files successfully transcoded !${reset}" fi