handbrake_for_plex.sh

shellcheck.sh
This commit is contained in:
2025-03-06 14:24:48 +01:00
parent 0e6f7c5196
commit 947698f00b

View File

@@ -59,24 +59,26 @@ fi
logsize=$(wc -c <"$logfile") logsize=$(wc -c <"$logfile")
if [ $logsize -ge 1000 ]; then if [ "$logsize" -ge 1000 ]; then
cp "$logfile" "$logfile.old" cp "$logfile" "$logfile.old"
truncate -s 0 "$logfile" truncate -s 0 "$logfile"
fi fi
#Move to trash after conversion #Move to trash after conversion
trash=true trash=true
command -v jq >/dev/null 2>&1 || {
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; } 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 fzf_bin=0
if (! type fzf >/dev/null 2>&1); then if (! type fzf >/dev/null 2>&1); then
echo -e "Install ${bold}fzf${reset} for a better experience !" echo -e "Install ${bold}fzf${reset} for a better experience !"
echo -e "${italic}brew install fzf${reset}" echo -e "${italic}brew install fzf${reset}"
fzf_bin=0 fzf_bin=0
else { else
{
fzf_bin=1 fzf_bin=1
fzf_args=( fzf_args=(
--height=8 --height=8
@@ -150,19 +152,19 @@ update_handbrake() {
echo "Downloading $url" echo "Downloading $url"
dest=$HOME/Downloads dest=$HOME/Downloads
filename=$(basename $url) filename=$(basename "$url")
curl -L -O --output-dir $dest "$url" curl -L -O --output-dir "$dest" "$url"
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
vol=$(hdiutil mount $dest/$filename | tail -1 | awk '{print $3}') vol=$(hdiutil mount "$dest"/"$filename" | tail -1 | awk '{print $3}')
app=$vol/HandBrakeCLI app=$vol/HandBrakeCLI
install_folder=$HOME/.local/bin/ install_folder=$HOME/.local/bin/
if [ -f "$app" ]; then if [ -f "$app" ]; then
echo "Copying $filename to $install_folder" echo "Copying $filename to $install_folder"
cp "$app" $install_folder cp "$app" "$install_folder"
fi fi
x="$install_folder""HandBrakeCLI" x="$install_folder""HandBrakeCLI"
@@ -175,8 +177,8 @@ update_handbrake() {
fi fi
fi fi
cd $HOME cd "$HOME" || exit
hdiutil unmount $vol hdiutil unmount "$vol"
fi fi
fi fi
@@ -189,7 +191,7 @@ installation() {
#long_path=`pwd`"/"`basename "$0"` #long_path=`pwd`"/"`basename "$0"`
long_path=$(realpath "$0") long_path=$(realpath "$0")
echo -e "${bold}Installing "`basename "$0"`"${reset}" echo -e "${bold}Installing $(basename "$0")${reset}"
echo echo
fzf_install_paths=("/usr/local/bin" "$HOME/.local/bin" "$HOME") fzf_install_paths=("/usr/local/bin" "$HOME/.local/bin" "$HOME")
@@ -225,8 +227,7 @@ installation() {
notification() { notification() {
#path_img=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) #path_img=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ "$3" -eq 0 ]; if [ "$3" -eq 0 ]; then
then
sound="Glass" sound="Glass"
#image="$path_img/success.png" #image="$path_img/success.png"
#image="$path_img/HandBrake.icns" #image="$path_img/HandBrake.icns"
@@ -249,15 +250,18 @@ while getopts "h?i:o:p:z" opt; do
case "$opt" in case "$opt" in
h | \?) h | \?)
showHelp showHelp
exit 0
;; ;;
i) input_path="$OPTARG" i)
input_path="$OPTARG"
;; ;;
o) output_path="$OPTARG" o)
output_path="$OPTARG"
;; ;;
p) profile="$OPTARG" p)
profile="$OPTARG"
;; ;;
z) installation z)
installation
exit 0 exit 0
;; ;;
esac esac
@@ -267,40 +271,41 @@ done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
# Update handbrake_for_plex.sh from GitHub
#Github="https://github.com/HandBrake/HandBrake"
Github="https://github.com/HandBrake/HandBrake"
# Public Repo: # Public Repo:
#RawUrl="https://raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh" #RawUrl="https://raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh"
# Private Repo: # Private Repo:
RawUrl="https://x-access-token:$GITHUB_TOKEN@raw.githubusercontent.com/HandBrake/HandBrake/main/bashbirds.sh" #RawUrl="https://x-access-token:$GITHUB_TOKEN@raw.githubusercontent.com/HandBrake/HandBrake/main/bashbirds.sh"
### Check if there's a new release of the script: ### Check if there's a new release of the script:
LatestRelease="$(curl -s -r 0-50 $RawUrl | sed -n "/VERSION/s/VERSION=//p" | tr -d '"')" #LatestRelease="$(curl -s -r 0-50 $RawUrl | sed -n "/VERSION/s/VERSION=//p" | tr -d '"')"
#LatestChanges="$(curl -s -r 0-2000 $RawUrl | sed -n "/ChangeNotes/s/# ChangeNotes: //p")" #LatestChanges="$(curl -s -r 0-2000 $RawUrl | sed -n "/ChangeNotes/s/# ChangeNotes: //p")"
LatestChanges="$(curl -s -r 0-2000 $RawUrl | grep "^### ChangeNotes:" | sed 's/### ChangeNotes://g')" #LatestChanges="$(curl -s -r 0-2000 $RawUrl | grep "^### ChangeNotes:" | sed 's/### ChangeNotes://g')"
### ###
echo "_ _ ____ _ _ ___ ___ ____ ____ _ _ ____ ____ ____ ____ ___ _ ____ _ _ "; echo "_ _ ____ _ _ ___ ___ ____ ____ _ _ ____ ____ ____ ____ ___ _ ____ _ _ "
echo "|__| |__| |\ | | \ |__] |__/ |__| |_/ |___ |___ | | |__/ |__] | |___ \/ "; echo "|__| |__| |\ | | \ |__] |__/ |__| |_/ |___ |___ | | |__/ |__] | |___ \/ "
echo "| | | | | \| |__/ |__] | \ | | | \_ |___ | |__| | \ | |___ |___ _/\_ "; echo "| | | | | \| |__/ |__] | \ | | | \_ |___ | |__| | \ | |___ |___ _/\_ "
echo " "; echo " "
echo " "; echo " "
echo " "; echo " "
gh_hbc_repo="HandBrake/HandBrake" gh_hbc_repo="HandBrake/HandBrake"
last_hbc_release=$(curl --silent "https://api.github.com/repos/${gh_hbc_repo}/releases/latest" | grep -Po "(?<=\"tag_name\": \").*(?=\")") last_hbc_release=$(curl --silent "https://api.github.com/repos/${gh_hbc_repo}/releases/latest" | grep -Po "(?<=\"tag_name\": \").*(?=\")")
last_hbc_release="1.9.3"
version_brew=$(ls /opt/homebrew/Cellar/handbrake 2>/dev/null) version_brew=$(ls /opt/homebrew/Cellar/handbrake 2>/dev/null)
version_brew="1.9.1"
cli=$(which HandBrakeCLI) # /Users/bruno/.local/bin/HandBrakeCLI cli=$(which HandBrakeCLI) # /Users/bruno/.local/bin/HandBrakeCLI
version_cli=$("$cli" --version 2>/dev/null | awk '{print $2}') # HandBrake 1.9.2 version_cli=$("$cli" --version 2>/dev/null | awk '{print $2}') # HandBrake 1.9.2
#echo "$version_brew - $version_cli"
if [ "$version_brew" != "" ]; then if [ "$version_brew" != "" ]; then
if [ "$version_cli" != "" ]; then if [ "$version_cli" != "" ]; then
if [ "$(printf '%s\n' "$version_cli" "$version_brew" | sort -V | head -n1)" = "$version_cli" ]; then if [ "$(printf '%s\n' "$version_cli" "$version_brew" | sort -V | head -n1)" = "$version_cli" ]; then
HANDBRAKE_CLI="/opt/homebrew/Cellar/handbrake/$version/bin/HandBrakeCLI" HANDBRAKE_CLI="/opt/homebrew/Cellar/handbrake/$version_brew/bin/HandBrakeCLI"
HB_VERSION="$version_brew" HB_VERSION="$version_brew"
else else
HANDBRAKE_CLI="$cli" HANDBRAKE_CLI="$cli"
@@ -310,7 +315,7 @@ if [ "$version_brew" != "" ]; then
fi fi
else else
HANDBRAKE_CLI="/opt/homebrew/Cellar/handbrake/$version/bin/HandBrakeCLI" HANDBRAKE_CLI="/opt/homebrew/Cellar/handbrake/$version_brew/bin/HandBrakeCLI"
HB_VERSION="$version_brew" HB_VERSION="$version_brew"
fi fi
else else
@@ -347,8 +352,7 @@ REGEX="([sS]([0-9]{2,}|[X]{2,})[eE]([0-9]{2,}|[Y]{2,}))"
REGEX2=".[0-9]{4}" # année 2019 mais avec un caractère avant (chaine ne commence pas par 2019) REGEX2=".[0-9]{4}" # année 2019 mais avec un caractère avant (chaine ne commence pas par 2019)
movies=() movies=()
for FILE in "${SRC}"/**/*.{mkv,avi,mp4,m4v} for FILE in "${SRC}"/**/*.{mkv,avi,mp4,m4v}; do
do
# Get file size # Get file size
FILESIZE=$(stat -c%s "$FILE") FILESIZE=$(stat -c%s "$FILE")
@@ -358,8 +362,7 @@ do
done done
nb_movies=${#movies[@]} nb_movies=${#movies[@]}
for FILE in "${movies[@]}" for FILE in "${movies[@]}"; do
do
filename=$(basename "$FILE") filename=$(basename "$FILE")
#extension=${filename##*.} #extension=${filename##*.}
filename=${filename%.*} filename=${filename%.*}
@@ -409,7 +412,6 @@ END_COMMENT
#continue #continue
fi fi
if ! command -v mediainfo &>/dev/null; then if ! command -v mediainfo &>/dev/null; then
echo -e "${bold}mediainfo${reset} could not be found !\n" echo -e "${bold}mediainfo${reset} could not be found !\n"
echo -e "You should install ${bold}mediainfo${reset}:\n" echo -e "You should install ${bold}mediainfo${reset}:\n"
@@ -421,8 +423,7 @@ END_COMMENT
infos=$(mediainfo "$FILE" --output=JSON | jq '.media.track') infos=$(mediainfo "$FILE" --output=JSON | jq '.media.track')
while read row while read row; do
do
type=$(echo "$row" | jq -r '.["@type"]') type=$(echo "$row" | jq -r '.["@type"]')
if [[ "$type" == "General" ]]; then if [[ "$type" == "General" ]]; then
@@ -446,7 +447,7 @@ END_COMMENT
fmt=$(echo "$video" | jq -j '.Format | select( . != null )') fmt=$(echo "$video" | jq -j '.Format | select( . != null )')
if [ $fmt != "JPEG" ]; then if [ "$fmt" != "JPEG" ]; then
format_video=$(echo "$video" | jq -j '.Format | select( . != null )') format_video=$(echo "$video" | jq -j '.Format | select( . != null )')
format_profile=$(echo "$video" | jq -j '.Format_Profile | select( . != null )') format_profile=$(echo "$video" | jq -j '.Format_Profile | select( . != null )')
format_level=$(echo "$video" | jq -j '.Format_Level | select( . != null )') format_level=$(echo "$video" | jq -j '.Format_Level | select( . != null )')
@@ -489,7 +490,6 @@ END_COMMENT
fi fi
done < <(echo "$infos" | jq -c '.[]') done < <(echo "$infos" | jq -c '.[]')
# Menu # Menu
#menu=$(echo "$infos" | jq -j '.[] | .Menu' | sed '1d;$d') #menu=$(echo "$infos" | jq -j '.[] | .Menu' | sed '1d;$d')
@@ -498,8 +498,7 @@ END_COMMENT
m=() m=()
n=1 n=1
while IFS= read -r line while IFS= read -r line; do
do
# L'apparition # L'apparition
# "_00_07_57_920": "en:00:07:57.920", # "_00_07_57_920": "en:00:07:57.920",
@@ -518,7 +517,6 @@ END_COMMENT
# t: "_00_07_57_920" # t: "_00_07_57_920"
# c: "en:00:07:57.920", # c: "en:00:07:57.920",
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.*//') 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 | awk -F ":" '{print $2}' | sed 's/,$//' | sed 's/^/ /')
cc=$(echo "$c" | tr -d '"' | xargs | sed 's/,$//' | sed 's/^/ /' | xargs) cc=$(echo "$c" | tr -d '"' | xargs | sed 's/,$//' | sed 's/^/ /' | xargs)
@@ -569,8 +567,7 @@ END_COMMENT
if [ "$nb_text" -ge 1 ]; then if [ "$nb_text" -ge 1 ]; then
for val in ${!ft[@]} for val in "${!ft[@]}"; do
do
index=${val} index=${val}
((index++)) ((index++))
@@ -583,11 +580,9 @@ END_COMMENT
done done
fi fi
if [ "$nb_menu" -ge 1 ]; then if [ "$nb_menu" -ge 1 ]; then
echo -e "\n${bold}Menu:${reset}" echo -e "\n${bold}Menu:${reset}"
for elem in "${m[@]}"; for elem in "${m[@]}"; do
do
#echo "${elem}" #echo "${elem}"
# en:00:07:57.920 : 00h07mm57 # en:00:07:57.920 : 00h07mm57
@@ -599,7 +594,6 @@ END_COMMENT
fi # if mediainfo installed fi # if mediainfo installed
file_export="$DEST"/"$new_name" file_export="$DEST"/"$new_name"
if [ ! -f "$file_export" ]; then if [ ! -f "$file_export" ]; then
@@ -617,15 +611,15 @@ END_COMMENT
echo -e "$z" echo -e "$z"
$HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" 2>>$logfile $HANDBRAKE_CLI -i "$FILE" -o "$file_export" "$PRESET" 2>>$logfile
fi fi
result=$?
echo echo
result=$?
if [ "$result" = 0 ]; then if [ "$result" = 0 ]; then
echo -e "${green}$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 notification "HandBrake for Plex" "$new_name available in $DEST" $result
fi fi
# Open DEST file in an app # Open DEST file in an app
if [ "$APP" != "" ]; then if [ "$APP" != "" ]; then
if [[ $(command -v mdfind) == "" ]]; then if [[ $(command -v mdfind) == "" ]]; then
@@ -657,7 +651,7 @@ END_COMMENT
gio trash "$FILE" >/dev/null gio trash "$FILE" >/dev/null
fi fi
[ "$(echo "${parent_folder}/"*)" = "${parent_folder}/*" ] && rm -rf "${parent_folder}"; [ "$(echo "${parent_folder}/"*)" = "${parent_folder}/*" ] && rm -rf "${parent_folder}"
echo "" echo ""
@@ -666,7 +660,6 @@ END_COMMENT
((already_transcoded += 1)) ((already_transcoded += 1))
fi fi
FILESIZE= FILESIZE=
done done
@@ -678,5 +671,3 @@ if [ "$transcoded" -eq 0 ]; then
else else
echo -e "\n${green}${transcoded} files successfully transcoded !${reset}" echo -e "\n${green}${transcoded} files successfully transcoded !${reset}"
fi fi