Advertisement
MaxDjently

Ziggle Wump Media Compressor 0.2-beta.10.04.2024

Oct 3rd, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 40.33 KB | Source Code | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash
  2.  
  3. # ATTENTION:
  4.  
  5. # Copying and pasting the script can introduce formatting that is improper to bash.  If the script doesn't run, you may need to use the dos2unix command to fix it.
  6.  
  7. # ex.  dos2unix ziggle_wump.sh
  8.  
  9. # --------------------
  10. #     Licence
  11. # --------------------
  12.  
  13. # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  14.  
  15. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16.  
  17. # You should have received a copy of the GNU General Public License along with  this program. If not, see <https://www.gnu.org/licenses/>.
  18.  
  19. # -----------------
  20. #     About
  21. # -----------------
  22.  
  23. # This script is for Termux on Android and is not associated with the apps it uses.
  24.  
  25. # Copyright Joshua Hansen and contributors: Microsoft Co-Pilot, OpenAI ChatGPT and Google Gemini.
  26.  
  27. # Shout out to Webernets https://youtu.be/0aeCfDKLfbs?si=58y58eWSiXurcHpj who gave me the inspiration for this, the one who did the heavy lifting figuring out the command line options. And this video helping me figure out how to do it in Handbrake, but I needed it all in a one-line command line option and this was it, I had to write it myself.
  28.  
  29. # -----------------------
  30. #   Instructions
  31. # -----------------------
  32.  
  33. instructions="Ziggle Wump: The Simple FFmpeg Command Line Companion Script for Termux on Android
  34.  
  35. Disclaimer
  36.  
  37. * This script does not allow conversion of encrypted files, typically indicating copyrighted material.
  38.  
  39. * The script itself does not violate copyright, but user actions might. The script cannot prevent users from circumventing copyright protection, nor is it the script's responsibility to do so. The onus lies with the copyright holder to encrypt their media.
  40.  
  41. * It is not recommended to use the script to violate copyright law. US copyright law allows for spaceshifting and fair use of copyrighted material.
  42. Note
  43.  
  44. * Some phones, like the Galaxy S24, have battery-saving features that can impact encoding. Ensure Termux is in focus (full screen or split-screen) while encoding, and keep the screen on during the process.
  45.  
  46. * For more information and potential fixes for specific phones, visit https://dontkillmyapp.com/.
  47.  
  48. Getting Started
  49.  
  50. * Download or Copy: Download or copy this script to a file and rename it if desired (e.g., ziggle_wump.sh). Placing it in your Movies folder makes it easily accessible on both Android and Termux, although you can also put it in /data/data/com.termux/files/usr/bin to use it system-wide.
  51.  
  52. * Install Termux and Termux:Widget (Optional): Grab Termux from F-Droid if you haven't already. Termux:Widget is optional for a more user-friendly interface.
  53.  
  54. * Obtain Storage Access: Run termux-setup-storage and allow access.
  55.  
  56. * Run the Script: Execute the script using bash ./ziggle_wump.sh [options]. (FIRST TIME RUN: Use bash ./ziggle_wump.sh -d to install dependencies.)
  57.  
  58. Termux:Widget (Optional):
  59.  
  60. * Install Termux:Widget and grant it permissions.
  61.  
  62. * Place the widget on your Home Screen.
  63.  
  64. * Run bash ziggle_wump.sh -i to install the script to /data/data/com.termux/files/usr/bin and create a widget in $HOME/.shortcuts.
  65.  
  66. * Reload the Widget to see 'Ziggle Wump Media Compressor.sh.'
  67.  
  68. Options:
  69.  
  70. * -r resolution: Sets a custom resolution height while preserving aspect ratio for both videos and images (e.g., bash ziggle_wump.sh -r 720).
  71.  
  72. * -d: Checks and upgrades dependencies.
  73.  
  74. * -y: Automatically confirms prompts.
  75.  
  76. * -o output_fps: Sets a custom output video frames per second. (e.g., bash ziggle_wump.sh -o 60).
  77.  
  78. * -b max_video_bitrate: Sets a custom maximum video bitrate in kilobits per second. (e.g., bash ziggle_wump.sh -b 2000).
  79.  
  80. * -a avg_audio_bitrate: Sets a custom average audio bitrate in kilobits per second for both videos and audio files. (e.g., bash ziggle_wump.sh -a 128).
  81.  
  82. * -p preset: Sets encoding speed preset for videos. (0=slowest, 8=fastest) (e.g., bash ziggle_wump.sh -p 4).
  83.  
  84. * -P video_encoding_profile Sets the profile found in profiles.conf
  85.  
  86. * -L Lists all profiles found in profiles.conf
  87.  
  88. * -i: Installs the script to /data/data/com.termux/files/usr/bin/zwmc. Installs widget to ~/.shortcuts.
  89.  
  90. * -u: Uninstalls the script from /data/data/com.termux/files/usr/bin/zwmc.  Removes widget.
  91.  
  92. * -m: Shows the menu for setting options.
  93.  
  94. * -h, --help: Displays help message."
  95.  
  96. # -----------------------------
  97. #        Variables
  98. # -----------------------------
  99.  
  100. # Supported File Types
  101. video_ext="mp4 mkv avi mov flv wmv webm mts 3gp mpeg ogv rmvb m4v f4v vob ts m2ts asf swf m2v divx xvid mpg mpe m1v dvr-ms mxf gxf bink mng nsv"
  102. audio_ext="mp3 aac wav flac ogg m4a wma ac3 eac3 opus amr aiff alac caf dts mka mp2 ra tta voc"
  103. img_ext="jpg jpeg png bmp tiff gif webp heic heif jxl ppm pgm pbm pam tga sgi pcx ras xbm xpm ico dds exr hdr svg"
  104.  
  105. # Define an array named 'profiles' containing various video encoding profiles
  106. profiles=(
  107.     # Profile format: "Profile_Name Height Frame_Rate Video_Bit_Rate Audio_Bit_Rate CRF Preset"
  108.  
  109.     "2_hour_CD 600 24 600 64 20 2"
  110.     "2_hour_DVD 1200 24 3500 128 18 2"
  111.     "2_hour_DL_DVD 1440 24 7000 192 18 2"
  112.     "4_hour_CD 480 24 250 64 25 2"
  113.     "4_hour_DVD 900 24 1750 96 23 2"
  114.     "4_hour_DL_DVD 1080 24 3500 128 23 2"
  115.     "6_hour_CD 360 24 140 64 28 4"
  116.     "6_hour_DVD 720 24 850 64 28 4"
  117.     "6_hour_DL_DVD 900 24 1700 96 28 4"
  118.     "8_hour_DVD 600 24 700 64 26 4"
  119.     "8_hour_DL_DVD 720 24 1400 96 26 4"
  120.     "12_hour_DVD 480 24 470 64 27 4"
  121.     "12_hour_DL_DVD 600 24 940 96 27 4"
  122.     "16_hour_DVD 360 20 350 64 28 4"
  123.     "16_hour_DL_DVD 480 20 700 64 28 4"
  124.     "24_hour_DVD 240 15 235 64 28 4"
  125.     "24_hour_DL_DVD 360 15 470 64 28 4"
  126.     "2160p_Video 2160 60 10000 256 18 2"
  127.     "1440p_Video 1440 60 6000 192 20 2"
  128.     "1080p_Video 1080 60 4000 128 23 3"
  129.     "900p_Video 900 60 3000 128 24 3"
  130.     "720p_Video 720 30 2500 96 24 3"
  131.     "600p_Video 600 30 2000 96 25 3"
  132.     "480p_Video 480 30 1500 64 26 3"
  133.     "360p_Video 360 30 1000 64 28 3"
  134.     "240p_Video 240 30 500 48 30 4"
  135.     "160p_Video 160 30 300 48 32 5"
  136.     "10Mbps_Streaming 2160 60 9500 256 18 2"
  137.     "5Mbps_Streaming 1440 60 4800 192 20 2"
  138.     "3Mbps_Streaming 1080 60 2700 128 24 3"
  139.     "1.5Mbps_Streaming 720 30 1300 96 24 3"
  140.     "1Mbps_Streaming 480 30 900 64 26 3"
  141.     "500kbps_Streaming 360 15 400 48 30 3"
  142.     "128kbps_Streaming 240 15 110 32 32 5"
  143.     "56kbps_Streaming 128 12 30 16 34 5"
  144.     "90s_game_console 240 15 200 48 30 4"
  145.     "Cinema_Quality 2160 24 7000 192 18 2"
  146.     "Desktop_High-End 2160 60 10000 256 20 2"
  147.     "Desktop_Low-End 1080 30 1500 96 24 3"
  148.     "Desktop_Mid-Range 1440 60 3500 128 22 2"
  149.     "DVD_Quality 480 24 1000 64 28 3"
  150.     "Low_Bandwidth_Stream 270 15 100 48 32 5"
  151.     "Low_Res_Mobile 360 24 300 48 28 4"
  152.     "Mobile_High-End 1080 60 1500 96 24 3"
  153.     "Mobile_Low_End 360 15 150 48 32 5"
  154.     "Mobile_Mid-Range 720 30 800 64 26 3"
  155.     "Modern_Laptop 1080 60 1500 96 24 3"
  156.     "Old_Smartphone 360 24 300 64 28 4"
  157.     "Retro_Computer_16-bit 320 30 150 32 32 5"
  158.     "Retro_Computer_8-bit 240 15 100 32 32 5"
  159.     "Retro_PC 480 15 200 64 30 4"
  160.     "Tablet_Profile 720 30 800 64 26 3"
  161.     "VHS_Quality 240 30 250 64 30 4"
  162. )
  163. default_profile="2_hour_CD"
  164.  
  165. # Define an array of presets from slowest to fastest
  166. presets=("veryslow" "slower" "slow" "medium" "fast" "faster" "veryfast" "superfast" "ultrafast")
  167. preset=slow # Default Speed Preset
  168. profile="" # custom Ziggle Wump profile.
  169.  
  170. # Full Log file path
  171. log_file="/storage/emulated/0/Movies/VideoDrop/convert.log"
  172.  
  173. # Full directory paths
  174. video_drop_dir="/storage/emulated/0/Movies/VideoDrop"
  175. video_processing_dir="/storage/emulated/0/Movies/VideoProcessing"
  176. output_dir_base="/storage/emulated/0/Movies/VideoConverted"
  177.  
  178. # combine audio and video extension variables to pass to find command.
  179. filetypes=""
  180. for ext in $audio_ext $video_ext $img_ext; do
  181.     filetypes="$filetypes -iname '*.$ext' -o"
  182. done
  183. # Remove the trailing ' -o'
  184. filetypes="${filetypes% -o}"
  185.  
  186. # Define a lock file
  187. LOCKFILE="/data/data/com.termux/files/usr/var/lock/zwmc.lock"
  188. LOCKDIR=$(dirname "$LOCKFILE")
  189.  
  190. # Ensure the lock directory exists
  191. mkdir -p "$LOCKDIR"
  192.  
  193. # Cleanup function to remove the lock file
  194. cleanup() {
  195.     rm -f "$LOCKFILE"
  196. }
  197.  
  198. # Set the trap to call cleanup on script exit
  199. trap cleanup EXIT
  200.  
  201. # Try to acquire the lock
  202. exec 200>"$LOCKFILE"
  203. flock -n 200 || { echo "Script is already running"; exit 1; }
  204.  
  205. # The lock file will be removed when the script exits, thanks to the trap
  206.  
  207. # --------------------------------------------------
  208. #    Setting up files and folders
  209. # --------------------------------------------------
  210.  
  211. width=$(tput cols)
  212.  
  213. # Create necessary directories if they don't exist
  214. mkdir -p "$video_drop_dir" "$video_processing_dir" "$output_dir_base"
  215.  
  216. # Check if log file exists
  217. if [ -f "$log_file" ]; then
  218.     echo "Old log file found.  Deleting..."
  219.     rm "$log_file"
  220.     echo "File deleted."
  221. else
  222.     echo "Log File does not exist.  The script will create $log_file"
  223. fi
  224.  
  225. echo ""
  226. echo "$instructions" | tee -a "$log_file" | fmt -w $width
  227.  
  228. echo "Generating profiles.conf in $video_drop_dir..." | tee -a "$log_file"
  229.  
  230. # Define the instructions
  231. profile_instructions="# This file contains various video encoding profiles.  Each profile specifies the resolution, frame rate, video bitrate, audio bitrate, CRF (Constant Rate Factor), and preset for encoding.  These profiles are configured for x265 (HEVC) video codec and Opus audio codec. You can modify these profiles as needed for your specific use case.
  232. "
  233.  
  234. # Check if profiles.conf exists, if not, create it
  235. if [ ! -f "$video_drop_dir/profiles.conf" ]; then
  236.     echo "$profile_instructions" > "$video_drop_dir/profiles.conf"
  237. else
  238.     # Add instructions if not present in profiles.conf
  239.     if ! grep -q "# This file contains various video encoding profiles." "$video_drop_dir/profiles.conf"; then
  240.         sed -i "1i $profile_instructions" "$video_drop_dir/profiles.conf"
  241.     fi
  242. fi
  243.  
  244. # Function to check if a profile exists
  245. profile_exists() {
  246.     local profile_name=$1
  247.     grep -q "^\[$profile_name\]" "$video_drop_dir/profiles.conf"
  248. }
  249.  
  250. # Add profiles if they don't exist
  251. for profile in "${profiles[@]}"; do
  252.     IFS=' ' read -r -a profile_params <<< "$profile"
  253.     profile_name=${profile_params[0]}
  254.    
  255.     if ! profile_exists "$profile_name"; then
  256.         echo "[${profile_name}]" >> "$video_drop_dir/profiles.conf"
  257.         echo "resolution=${profile_params[1]}" >> "$video_drop_dir/profiles.conf"
  258.         echo "frame_rate=${profile_params[2]}" >> "$video_drop_dir/profiles.conf"
  259.         echo "video_bitrate=${profile_params[3]}" >> "$video_drop_dir/profiles.conf"
  260.         echo "audio_bitrate=${profile_params[4]}" >> "$video_drop_dir/profiles.conf"
  261.         echo "crf=${profile_params[5]}" >> "$video_drop_dir/profiles.conf"
  262.         echo "preset=${profile_params[6]}" >> "$video_drop_dir/profiles.conf"
  263.         echo "" >> "$video_drop_dir/profiles.conf"
  264.     fi
  265. done
  266.  
  267. # Function to read profile from profiles.conf
  268. read_profile() {
  269.     local profile_name=$1
  270.     resolution=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/resolution/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  271.     output_fps=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/frame_rate/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  272.     max_video_bitrate=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/video_bitrate/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  273.     avg_audio_bitrate=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/audio_bitrate/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  274.     crf=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/crf/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  275.     preset=$(awk -F "=" "/^\[$profile_name\]/ {a=1} a==1 && \$1~/preset/ {print \$2; exit}" $video_drop_dir/profiles.conf)
  276.     preset=${presets[$preset]}
  277. }
  278.  
  279. # Function to list profiles from profiles.conf
  280. list_profiles() {
  281.     awk -F "=" '/^\[.*\]/ {gsub(/[\[\]]/, "", $1); print $1}' $video_drop_dir/profiles.conf
  282. }
  283.  
  284. # --------------------------------
  285. #     Command Flags
  286. # --------------------------------
  287.  
  288. read_profile "$default_profile"
  289.  
  290. show_menu() {
  291.     auto_yes=true  # Turn on auto_yes when the menu is shown
  292.     while true; do
  293.         deps_status="Off"
  294.         if [ "$check_deps" = true ]; then
  295.             deps_status="On"
  296.         fi
  297.  
  298.         menu_choice=$(dialog --menu "Ziggle Wump Media Compressor Chooser" 17 50 11 \
  299.             1 "Set Max Resolution ($resolution)" \
  300.             2 "Set Max Output FPS ($output_fps)" \
  301.             3 "Set Max Video Bitrate ($max_video_bitrate)" \
  302.             4 "Set Average Audio Bitrate ($avg_audio_bitrate)" \
  303.             5 "Set Constant Rate Factor ($crf)" \
  304.             6 "Set Encoding Speed ($preset)" \
  305.             7 "Check Dependencies ($deps_status)" \
  306.             8 "Install Script" \
  307.             9 "Uninstall Script" \
  308.             10 "Set Conversion Profile" \
  309.             11 "Compress Files in $(basename $video_drop_dir)" 2>&1 >/dev/tty)
  310.  
  311.         case $menu_choice in
  312.             1)
  313.                 new_resolution=$(dialog --inputbox "Enter resolution maximum height in pixels ($resolution):" 8 40 "$resolution" 2>&1 >/dev/tty)
  314.                 if [ $? -eq 0 ]; then
  315.                     resolution="$new_resolution"
  316.                 fi
  317.                 ;;
  318.             2)
  319.                 new_output_fps=$(dialog --inputbox "Enter maximum output FPS ($output_fps):" 8 40 "$output_fps" 2>&1 >/dev/tty)
  320.                 if [ $? -eq 0 ]; then
  321.                     output_fps="$new_output_fps"
  322.                 fi
  323.                 ;;
  324.             3)
  325.                 new_max_video_bitrate=$(dialog --inputbox "Enter maximum video bitrate ($max_video_bitrate):" 8 40 "$max_video_bitrate" 2>&1 >/dev/tty)
  326.                 if [ $? -eq 0 ]; then
  327.                     max_video_bitrate="$new_max_video_bitrate"
  328.                 fi
  329.                 ;;
  330.             4)
  331.                 new_avg_audio_bitrate=$(dialog --inputbox "Enter average audio bitrate ($avg_audio_bitrate):" 8 40 "$avg_audio_bitrate" 2>&1 >/dev/tty)
  332.                 if [ $? -eq 0 ]; then
  333.                     avg_audio_bitrate="$new_avg_audio_bitrate"
  334.                 fi
  335.                 ;;
  336.             5)
  337.                 dialog --msgbox "CRF (Constant Rate Factor) is a quality setting for video encoding. It ranges from 0 (lossless) to 51 (worst quality). Lower values mean better quality and larger files, while higher values mean lower quality and smaller files." 10 50
  338.                 new_crf=$(dialog --inputbox "Enter CRF value (0-51) ($crf):" 8 40 "$crf" 2>&1 >/dev/tty)
  339.                 if [ $? -eq 0 ]; then
  340.                     echo "User entered CRF: $new_crf" >&2  # Debug message
  341.                     if [ "$new_crf" -ge 0 ] && [ "$new_crf" -le 51 ]; then
  342.                         crf="$new_crf"
  343.                         echo "CRF set to: $crf" >&2  # Debug message
  344.                     else
  345.                         dialog --msgbox "Invalid CRF value. Please enter a value between 0 and 51." 6 40
  346.                         echo "Invalid CRF value entered: $new_crf" >&2  # Debug message
  347.                     fi
  348.                 fi
  349.                 ;;
  350.             6)
  351.                 dialog --msgbox "Choose an x265 speed preset. The preset determines the encoding speed and compression efficiency. Choose a preset that best fits your needs. Slower presets will provide better compression and quality but take longer to encode." 10 50
  352.  
  353.                 # Your original preset selection dialog
  354.                 new_preset=$(dialog --menu "Choose x265 Speed Preset" 15 50 9 \
  355.                     ultrafast "8" \
  356.                     superfast "7" \
  357.                     veryfast "6" \
  358.                     faster "5" \
  359.                     fast "4" \
  360.                     medium "3" \
  361.                     slow "2" \
  362.                     slower "1" \
  363.                     veryslow "0" 2>&1 >/dev/tty)
  364.  
  365.                 # Check if a new preset was selected
  366.                 if [ $? -eq 0 ]; then
  367.                     preset=$new_preset
  368.                 fi
  369.                 ;;
  370.             7)
  371.                 while true; do
  372.                     if [ "$check_deps" = true ]; then
  373.                         check_deps=false
  374.                     else
  375.                         check_deps=true
  376.                     fi
  377.                     deps_status="Off"
  378.                     if [ "$check_deps" = true ]; then
  379.                         deps_status="On"
  380.                     fi
  381.                     dialog --msgbox "Dependencies are now $deps_status" 6 40
  382.                     break
  383.                 done
  384.                 ;;
  385.             8)
  386.                 clear
  387.                 install_script
  388.                 ;;
  389.             9)
  390.                 clear
  391.                 uninstall_script
  392.                 ;;
  393.             10)
  394.                 profile_list=$(list_profiles)
  395.                 selected_profile=$(dialog --menu "Choose Profile" 15 50 9 $(echo "$profile_list" | awk '{print NR, $0}') 2>&1 >/dev/tty)
  396.                 if [ $? -eq 0 ]; then
  397.                     read_profile "$(echo "$profile_list" | sed -n "${selected_profile}p")"
  398.                 fi
  399.                 ;;
  400.             11)
  401.                 break
  402.                 ;;
  403.             *)
  404.                 tput sgr0  # Reset terminal colors
  405.                 clear
  406.                 exit 0
  407.                 ;;
  408.         esac
  409.     done
  410. }
  411.  
  412. # Function to display help message
  413. show_help() {
  414.     width=$(tput cols)
  415.     script_name=$(basename "$0")
  416.     echo "Place your media files in $video_drop_dir." | tee -a "$log_file" | fmt -w $width
  417.     echo "Usage: bash $script_name [-r resolution] [-d] [-y] [-o output_fps] [-b max_video_bitrate] [-a avg_audio_bitrate] [-p preset] [-i] [-u] [-m] [-P] [-L] [-h|--help]" | fmt -w $width
  418.     echo "  -r resolution ex. bash $script_name -r 720 Custom resolution height preserving aspect ratio" | fmt -w $width
  419.     echo "  -d Check and upgrade dependencies." | fmt -w $width
  420.     echo "  -y Automatically say yes to prompts." | fmt -w $width
  421.     echo "  -o output_fps Set custom output FPS. ex. bash $script_name -o 60" | fmt -w $width
  422.     echo "  -b max_video_bitrate Set custom max video bitrate in kilobits per second. ex. bash $script_name -b 2000" | fmt -w $width
  423.     echo "  -a avg_audio_bitrate Set custom average audio bitrate in kilobits per second. ex. bash $script_name -a 128" | fmt -w $width
  424.     echo "  -p preset Set encoding speed preset for x265 (0=slowest, 8=fastest). ex. bash $script_name -p 4" | fmt -w $width
  425.     echo "  -i Install the script to /data/data/com.termux/files/usr/bin/zwmc" | fmt -w $width
  426.     echo "  -u Uninstall the script from /data/data/com.termux/files/usr/bin/zwmc" | fmt -w $width
  427.     echo "  -m Shows the menu for setting options." | fmt -w $width
  428.     echo " -P video_encoding_profile Sets the profile found in profiles.conf" | fmt -w $width
  429.     echo " -L List encoding profiles."
  430.     echo "  -h, --help Display this help message" | fmt -w $width
  431.     exit 0
  432. }
  433.  
  434. # Install Ziggle Wump to /data/data/com.termux/files/usr/bin folder and widget script to ~/.shortcuts
  435. install_script() {
  436.     if [ -f /data/data/com.termux/files/usr/bin/zwmc ]; then
  437.         current_dir=$(dirname "$0")
  438.         if [ "$current_dir" != "/data/data/com.termux/files/usr/bin" ]; then
  439.             echo "Script is installed but not running from the bin directory. Reinstalling..." | tee -a "$log_file" | fmt -w $width
  440.             cp "$(readlink -f "$0")" /data/data/com.termux/files/usr/bin/zwmc
  441.             chmod +x /data/data/com.termux/files/usr/bin/zwmc
  442.             if whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
  443.                 echo "Script reinstalled to /data/data/com.termux/files/usr/bin/zwmc"
  444.             else
  445.                 echo "Reinstallation failed" | tee -a "$log_file" | fmt -w $width
  446.             fi
  447.         else
  448.             echo "Script is already installed and running from the bin directory." | tee -a "$log_file" | fmt -w $width
  449.         fi
  450.     else
  451.         cp "$(readlink -f "$0")" /data/data/com.termux/files/usr/bin/zwmc
  452.         chmod +x /data/data/com.termux/files/usr/bin/zwmc
  453.         if whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
  454.             echo "Script installed to /data/data/com.termux/files/usr/bin/zwmc" | tee -a "$log_file" | fmt -w $width
  455.         else
  456.             echo "Installation failed" | tee -a "$log_file" | fmt -w $width
  457.         fi
  458.     fi
  459.  
  460.     # Create the shortcut script
  461.     mkdir -p $HOME/.shortcuts
  462.     echo '#!/data/data/com.termux/files/usr/bin/bash' > $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  463.     echo 'zwmc -m' >> $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  464.     echo 'read -p "Press Enter to continue.  Bye!"' >> $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  465.     echo 'case $return in' >> $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  466.     echo '    * ) echo "Exiting script."' >> $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  467.     echo 'esac' >> $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  468.     chmod +x $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  469.  
  470.     exit 0
  471. }
  472.  
  473. uninstall_script() {
  474.     if [ ! -f /data/data/com.termux/files/usr/bin/zwmc ]; then
  475.         echo "Script is not installed." | tee -a "$log_file" | fmt -w $width
  476.         exit 0
  477.     else
  478.         rm -f /data/data/com.termux/files/usr/bin/zwmc
  479.         if ! whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
  480.             echo "Script uninstalled from /data/data/com.termux/files/usr/bin/zwmc" | tee -a "$log_file" | fmt -w $width
  481.         else
  482.             echo "Uninstallation failed" | tee -a "$log_file" | fmt -w $width
  483.         fi
  484.     fi
  485.  
  486.     # Remove the shortcut script
  487.     rm -f $HOME/.shortcuts/Ziggle\ Wump\ Media\ Compressor.sh
  488.  
  489.     exit 0
  490. }
  491.  
  492. # Parse command-line options
  493. check_deps=false
  494. auto_yes=false  # Set auto_yes to false by default
  495. while getopts "r:dyho:b:a:c:iump:P:L" opt; do
  496.     case $opt in
  497.         r) resolution="$OPTARG" ;;  # Set custom resolution by height
  498.         d) check_deps=true ;;  # Check and upgrade dependencies
  499.         y) auto_yes=true ;;  # Automatically say yes to prompts
  500.         o) output_fps="$OPTARG" ;;  # Set custom output FPS
  501.         b) max_video_bitrate="$OPTARG" ;;  # Set custom max video bitrate
  502.         a) avg_audio_bitrate="$OPTARG" ;;  # Set custom average audio bitrate
  503.         c) crf="$OPTARG" ;;  # Set custom CRF value
  504.         i) install_script ;;  # Install the script
  505.         u) uninstall_script ;;  # Uninstall the script
  506.         m) auto_yes=true; show_menu ;;  # Show menu and turn on auto_yes
  507.         p) preset="${presets[$OPTARG]}" ;;  # Set preset based on number 0-8
  508.         P) profile="$OPTARG"; read_profile "$profile" ;;
  509.         L) list_profiles # List encoding profiles.
  510.         exit 0 ;;
  511.         h) show_help ;;  # Display help
  512.         *) show_help ;;  # Display help for invalid options
  513.     esac
  514. done
  515.  
  516. shift $((OPTIND -1))
  517.  
  518. # ------------------------------
  519. #     Dependencies
  520. # ------------------------------
  521.  
  522. if $check_deps; then
  523.     echo "Checking and upgrading dependencies, please wait..."
  524.     pkg update && pkg upgrade -y
  525.     if ! command -v bc &> /dev/null; then
  526.         pkg install bc -y
  527.     fi
  528.     if ! command -v ffmpeg &> /dev/null; then
  529.         pkg install ffmpeg -y
  530.     fi
  531.     if ! command -v ncurses-utils &> /dev/null; then
  532.         pkg install ncurses-utils -y
  533.     fi
  534.     echo "Update complete."
  535. fi
  536.  
  537. # ------------------------
  538. #     Start script
  539. # ------------------------
  540.  
  541. echo "Running... Press Ctrl+C to stop."
  542.  
  543. echo ""
  544. # Function to prompt the user to continue or quit
  545. prompt_continue_or_quit() {
  546.     while true; do
  547.         # Recursively find and process videos and audio
  548.         video_count=$(eval "find \"$video_drop_dir\" -type f \( $filetypes \) | wc -l")
  549.         echo "Detected $video_count compatible video and/or audio file(s) in:" | tee -a "$log_file" | fmt -w $width
  550.    
  551.         if [ "$video_count"  -eq 0 ]; then
  552.             echo "$video_drop_dir folder created. Place your videos and audio here including files in folders using your favorite file manager for Android, and then run  the script again.  
  553.  
  554. Supported File Types:
  555.  
  556. Video: $video_ext
  557.  
  558. Audio: $audio_ext
  559.  
  560. Images: $img_ext" | tee -a "$log_file" | fmt -w $width
  561.             exit 0
  562.         fi
  563.  
  564.         # Prompt text
  565.         prompt_text="$video_drop_dir. You can start encoding now. Do you wish to proceed? (Y/N): "
  566.  
  567.         # Format the prompt text
  568.         formatted_prompt=$(echo "$prompt_text" | fmt -w $width)
  569.  
  570.         if $auto_yes; then
  571.             echo "$formatted_prompt"
  572.             echo "Y"
  573.             return 0  # Automatically continue
  574.         else
  575.             # Read user input with formatted prompt
  576.             read -p "$formatted_prompt" yn
  577.             case $yn in
  578.                 [Yy]* ) return 0;;  # Continue
  579.                 [Nn]* ) echo "Exiting script."; exit 0;;  # Quit
  580.                 * ) echo "Please answer Y or N.";;
  581.             esac
  582.         fi
  583.     done
  584. }
  585.  
  586. # Function to clean up file names
  587. clean_file_names() {
  588.     eval "find \"$video_drop_dir\" -type f \( $filetypes \)" | while read -r file; do
  589.         dir=$(dirname "$file")
  590.         base=$(basename "$file")
  591.         new_base=$(echo "$base" | sed 's/[^a-zA-Z0-9 ._-]//g' | tr -s ' ')
  592.         new_file="$dir/$new_base"
  593.         if [ "$file" != "$new_file" ]; then
  594.             mv "$file" "$new_file"
  595.         fi
  596.     done
  597. }
  598.  
  599. # Clean up file names before processing
  600. clean_file_names
  601.  
  602. # Prompt the user to continue or quit?
  603. prompt_continue_or_quit
  604.  
  605. # Initialize ffmpeg_custom_options
  606. ffmpeg_custom_options="-c:v libx265 -x265-params \"deblock=-1:no-sao=1:keyint=250:aq-mode=3:psy-r=0.75:psy-rdoq=2.0:rd=4:rdoq-level=1:rect=0:strong-intra-smoothing=0\" -crf $crf -preset $preset -c:a libopus -vbr on -ac 2 -af \"loudnorm=I=-23:LRA=7:TP=-2\""
  607.  
  608. # Add avg audio bitrate if avg_audio_bitrate is set
  609. if [ -n "$avg_audio_bitrate" ]; then
  610.     ffmpeg_custom_options="$ffmpeg_custom_options -b:a ${avg_audio_bitrate}k"
  611. fi
  612.  
  613. # Add maxrate and bufsize options if max_video_bitrate is set
  614. if [ -n "$max_video_bitrate" ]; then
  615.     ffmpeg_custom_options="$ffmpeg_custom_options -maxrate ${max_video_bitrate}k -bufsize 60M"
  616. fi
  617.  
  618. # Function to check the log file for "Killed" message
  619. check_for_killed_message() {
  620.     if grep -q " Killed     " "$log_file"; then
  621.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Check log file for details." | tee -a "$log_file"
  622.         return 1
  623.     fi
  624.     return 0
  625. }
  626.  
  627. echo "Starting conversion process..." | tee -a "$log_file"
  628.  
  629. # -----------------------------
  630. #    Image Encoder
  631. # -----------------------------
  632.  
  633. width=$(tput cols)
  634.  
  635. # Function to process image files
  636. process_image() {
  637.     local image="$1"
  638.     local relative_path="${image#$video_drop_dir/}"
  639.     local dir_path=$(dirname "$relative_path")
  640.     local base_name=$(basename "$relative_path" | tr -cd '[:alnum:]._ -')
  641.     local output_dir="$output_dir_base/$dir_path"
  642.     local output_file="$output_dir/${base_name}_converted.png"
  643.  
  644.     mkdir -p "$output_dir"  # Create output directory
  645.  
  646.     echo "$(date '+%Y-%m-%d %H:%M:%S') Processing $image to $output_file" | tee -a "$log_file"
  647.  
  648.     # Get input image resolution
  649.     input_resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$image" | tr -d '[:space:],')
  650.  
  651.     if [ -n "$resolution" ]; then
  652.         if [ "$input_resolution" -gt "$resolution" ]; then
  653.             scale_filter="scale=-2:$resolution:flags=lanczos"
  654.             echo "Input resolution ($input_resolution) is higher than user-defined resolution ($resolution). Using user-defined resolution." | tee -a "$log_file"
  655.     else
  656.             echo "Input resolution ($input_resolution) is lower or equal to user-defined resolution ($resolution). Keeping original resolution." | tee -a "$log_file"
  657.         fi
  658.     else
  659.         echo "Resolution set to: original" | tee -a "$log_file"
  660.     fi
  661.  
  662.     # Construct the FFmpeg command for image processing
  663.     ffmpeg_command="ffmpeg -nostdin -loglevel error -stats -y -i \"$image\" -compression_level 100 -vf \"$scale_filter\" \"$output_file\""
  664.  
  665.     echo "$ffmpeg_command" | tee -a "$log_file"
  666.  
  667.     # Execute the FFmpeg command
  668.     eval $ffmpeg_command 2>&1 | tee -a "$log_file"
  669.  
  670.     # Check for "Killed" message in the log file
  671.     if ! check_for_killed_message; then
  672.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Exiting." | tee -a "$log_file"
  673.         exit 1
  674.     fi
  675.  
  676.     if [ $? -eq 0 ]; then
  677.       #  mv "$temp_output_file" "$output_file"  # Rename the temporary file to the final output file
  678.         mkdir -p "$video_processing_dir/$dir_path"  # Create processing directory
  679.         mv "$image" "$video_processing_dir/$relative_path"
  680.         echo ""
  681.         echo "$(date '+%Y-%m-%d %H:%M:%S') Converted $image. Your original files will be in the $video_processing_dir folder for comparison. Your new files are in $output_dir_base" | tee -a "$log_file" | fmt -w $width
  682.     else
  683.         echo ""
  684.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error processing \"$image\". Check log file for details." | tee -a "$log_file"
  685.         exit 1
  686.     fi
  687. }
  688.  
  689. # -----------------------------
  690. #    Audio Encoder
  691. # -----------------------------
  692.  
  693. width=$(tput cols)
  694.  
  695. # Function to process audio files
  696. process_audio() {
  697.     local audio="$1"
  698.     local relative_path="${audio#$video_drop_dir/}"
  699.     local dir_path=$(dirname "$relative_path")
  700.     local base_name=$(basename "$relative_path" | tr -cd '[:alnum:]._ -')
  701.     local output_dir="$output_dir_base/$dir_path"
  702.     local output_file="$output_dir/${base_name}_converted.opus"
  703.     local temp_output_file="$output_file.tmp"
  704.  
  705.     mkdir -p "$output_dir"  # Create output directory
  706.  
  707.     echo "$(date '+%Y-%m-%d %H:%M:%S') Processing $audio to $output_file" | tee -a "$log_file"
  708.  
  709.     # Construct the FFmpeg command for audio processing
  710.     local ffmpeg_command="ffmpeg -nostdin -loglevel error -stats -stats_period 1 -y -i \"$audio\" -c:a libopus -b:a ${avg_audio_bitrate}k -vbr on -ac 2 -af \"loudnorm=I=-23:LRA=7:TP=-2\" -f opus \"$temp_output_file\""
  711.  
  712.     echo "$ffmpeg_command" | tee -a "$log_file"
  713.  
  714.     # Execute the FFmpeg command
  715.     eval $ffmpeg_command 2>&1 | tee -a "$log_file"
  716.  
  717.     # Check for "Killed" message in the log file
  718.     if ! check_for_killed_message; then
  719.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Exiting." | tee -a "$log_file"
  720.         exit 1
  721.     fi
  722.  
  723.     if [ $? -eq 0 ]; then
  724.         mv "$temp_output_file" "$output_file"  # Rename the temporary file to the final output file
  725.         mkdir -p "$video_processing_dir/$dir_path"  # Create processing directory
  726.         mv "$audio" "$video_processing_dir/$relative_path"
  727.         echo ""
  728.         echo "$(date '+%Y-%m-%d %H:%M:%S') Converted $audio. Your original files will be in the $video_processing_dir folder for comparison. Your new files are in $output_dir_base" | tee -a "$log_file" | fmt -w $width
  729.     else
  730.         echo ""
  731.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error processing \"$audio\". Check log file for details." | tee -a "$log_file"
  732.         exit 1
  733.     fi
  734. }
  735.  
  736. # ------------------------------
  737. #      Video Encoder
  738. # ------------------------------
  739.  
  740. width=$(tput cols)
  741.  
  742. # Function to check the log file for "Killed" message
  743. check_for_killed_message() {
  744.     if grep -q " Killed     " "$log_file"; then
  745.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Check log file for details." | tee -a "$log_file" | fmt -w $width
  746.         exit 1
  747.     fi
  748. }
  749.  
  750. process_video() {
  751.     local video="$1"
  752.     local relative_path="${video#$video_drop_dir/}"
  753.     local dir_path=$(dirname "$relative_path")
  754.     local base_name=$(basename "$relative_path" | tr -cd '[:alnum:]._ -')
  755.     local output_dir="$output_dir_base/$dir_path"
  756.     local output_file="$output_dir/${base_name}_converted.mkv"
  757.  
  758.    
  759.     # Get input video resolution
  760.     input_resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$video" | tr -d '[:space:],')
  761.  
  762.     if [ -n "$resolution" ]; then
  763.         if [ "$input_resolution" -lt "$resolution" ]; then
  764.             local scale_filter="scale=-2:$input_resolution:flags=lanczos"
  765.             echo "Input resolution ($input_resolution) is lower than user-defined resolution ($resolution). Keeping original resolution." | tee -a "$log_file"
  766.         else
  767.             local scale_filter="scale=-2:$resolution:flags=lanczos"
  768.             echo "Input resolution ($input_resolution) is higher or equal to user-defined resolution ($resolution). Using user-defined resolution." | tee -a "$log_file"
  769.         fi
  770.     else
  771.         echo "Resolution set to: original" | tee -a "$log_file"
  772.     fi
  773.  
  774.     mkdir -p "$output_dir"  # Create output directory
  775.  
  776.     echo "$(date '+%Y-%m-%d %H:%M:%S') Processing $video to $output_file" | tee -a "$log_file"
  777.  
  778.     # Get the frame rate of the input video
  779.     input_fps=$(ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate,r_frame_rate -of csv=p=0 "$video" | awk -F'/' '{if ($2) print $1/$2; else print $1}' | bc -l)
  780.  
  781.     # Define common frame rates
  782.     common_fps=(23.976 24 25 29.97 30 50 59.94 60)
  783.  
  784.     # Function to find the nearest valid frame rate
  785.     nearest_fps() {
  786.         local fps=$1
  787.         local nearest=${common_fps[0]}
  788.         local min_diff=$(echo "scale=5; $fps - ${common_fps[0]}" | bc | awk '{print ($1 >= 0) ? $1 : -$1}')
  789.         for rate in "${common_fps[@]}"; do
  790.             local diff=$(echo "scale=5; $fps - $rate" | bc | awk '{print ($1 >= 0) ? $1 : -$1}')
  791.             if (( $(echo "$diff < $min_diff" | bc -l) )); then
  792.                 min_diff=$diff
  793.                 nearest=$rate
  794.             fi
  795.         done
  796.         echo $nearest
  797.     }
  798.  
  799.     # Round the frame rate to the nearest valid frame rate
  800.     rounded_fps=$(nearest_fps $input_fps)
  801.  
  802.     # Determine the lower frame rate
  803.     if (( $(echo "$rounded_fps > $output_fps" | bc -l) )); then
  804.         final_fps=$output_fps
  805.     else
  806.         final_fps=$rounded_fps
  807.     fi
  808.  
  809. # Set the initial video filter option
  810. video_filter_option="yadif=2,fps=$final_fps"
  811.  
  812. # Check if scale_filter is not empty
  813. if [ -n "$scale_filter" ]; then
  814.     video_filter_option="${video_filter_option},${scale_filter}"
  815. fi
  816.  
  817. echo "Video filter option: $video_filter_option" | tee -a "$log_file"
  818.  
  819. # Detect the pixel format using ffprobe
  820. input_file="$video"
  821. pix_fmt=$(ffprobe -v repeat+level+error -select_streams v:0 -show_entries stream=pix_fmt -of default=noprint_wrappers=1:nokey=1 "$input_file" | awk 'NR==1{print $1}')
  822.  
  823. echo "Pixel format: $pix_fmt"
  824.  
  825. # Validate the pixel format
  826. valid_pix_fmt=$(ffmpeg -pix_fmts | grep -w "$pix_fmt")
  827.  
  828. # Check if 10-bit encoding is selected
  829. if [[ "$pix_fmt" == *"10le"* || "$pix_fmt" == *"10be"* ]]; then
  830.     video_filter_option="$video_filter_option,deband=1thr=0.01:2thr=0.01:3thr=0.01:4thr=0.01:range=4:direction=-3.14:blur=1:coupling=0"
  831. fi
  832.  
  833. # Define a variable for probe size and analyze duration
  834. probe_analyze_opts="-probesize 2147483647 -analyzeduration 2147483647"
  835.  
  836. # Detect subtitle codecs and convert unsupported ones
  837. subtitle_codecs=$(ffprobe -v error $probe_analyze_opts -select_streams s -show_entries stream=codec_name -of csv=p=0 "$input_file")
  838. subtitle_map=""
  839. if [ -n "$subtitle_codecs" ]; then
  840.     subtitle_map="-map 0:s"  # Include all subtitle streams
  841.     i=0
  842.     while read -r codec; do
  843.         case "$codec" in
  844.             # Text-based subtitles to be copied directly
  845.             srt)
  846.                 subtitle_map="$subtitle_map -c:s:$i copy"
  847.                 ;;
  848.             # Other text-based subtitles to be converted to SRT
  849.             ass|ssa|webvtt|eia_608|eia_708|scc|sami|ttml|smi|teletext|mov_text|microdvd|subviewer)
  850.                 subtitle_map="$subtitle_map -c:s:$i srt"
  851.                 ;;
  852.             # Bitmap-based subtitles to be converted to DVD subtitles
  853.             dvdsub|pgs|vobsub|hdmv_pgs_subtitle|dvd_subtitle)
  854.                 subtitle_map="$subtitle_map -c:s:$i dvdsub"
  855.                 ;;
  856.             # Copy other compatible subtitles
  857.             *)
  858.                 subtitle_map="$subtitle_map -c:s:$i copy"
  859.                 ;;
  860.         esac
  861.         i=$((i + 1))
  862.     done <<< "$subtitle_codecs"
  863. else
  864.     echo "No subtitle streams detected." | tee -a "$log_file"
  865. fi
  866.  
  867. # Combine common and custom FFmpeg options
  868. combined_ffmpeg_options="-nostdin -loglevel error -stats -stats_period 5 $probe_analyze_opts -y -fix_sub_duration -i \"$video\" -map 0:v:0 -map 0:a:? -map_chapters 0 $ffmpeg_custom_options"
  869.  
  870. # Construct the FFmpeg command
  871. local ffmpeg_command
  872. if [ -n "$valid_pix_fmt" ]; then
  873.     if [ -n "$subtitle_map" ]; then
  874.         ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" -pix_fmt $pix_fmt $subtitle_map"
  875.     else
  876.         ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" -pix_fmt $pix_fmt"
  877.     fi
  878. else
  879.     if [ -n "$subtitle_map" ]; then
  880.         ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" $subtitle_map"
  881.     else
  882.         ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\""
  883.     fi
  884. fi
  885.  
  886. # Ensure all subtitle streams are marked as "default: off"
  887. subtitle_streams=$(ffprobe -v error $probe_analyze_opts -select_streams s -show_entries stream=index -of csv=p=0 "$input_file")
  888. if [ -n "$subtitle_streams" ]; then
  889.     for stream_index in $subtitle_streams; do
  890.         ffmpeg_command="$ffmpeg_command -disposition:s:$stream_index 0"
  891.     done
  892. fi
  893.  
  894. # Add the output file name at the end
  895. ffmpeg_command="$ffmpeg_command \"$output_file\""
  896.  
  897. echo "FFmpeg command: $ffmpeg_command" | tee -a "$log_file"
  898.  
  899. # Execute the FFmpeg command
  900. if ! eval $ffmpeg_command 2>&1 | tee -a "$log_file"; then
  901.     echo "FFmpeg command failed. Check the log for details." | tee -a "$log_file"
  902.     exit 1
  903. fi
  904.  
  905.     # Check for "Killed" message in the log file
  906.     check_for_killed_message
  907.  
  908.    if [ $? -eq 0 ]; then
  909.         mkdir -p "$video_processing_dir/$dir_path"  # Create processing directory
  910.         mv "$video" "$video_processing_dir/$relative_path"
  911.         echo ""
  912.         echo "$(date '+%Y-%m-%d %H:%M:%S') Complete.  Your original files are in $video_processing_dir.  Your new files are in $output_dir_base" | tee -a "$log_file" | fmt -w $width
  913.     else
  914.         echo ""
  915.         echo "$(date '+%Y-%m-%d %H:%M:%S') Error processing \"$video\". Check log file for details." | tee -a "$log_file"
  916.         exit 1
  917.     fi
  918. }
  919.  
  920. # --------------------------------------
  921. #    Start Batch Encoding
  922. # --------------------------------------
  923.  
  924. width=$(tput cols)
  925.  
  926. stop_processing=false
  927.  
  928. # Function to handle SIGINT (Ctrl+C)
  929. handle_sigint() {
  930.     echo ""
  931.     echo "Caught SIGINT (Ctrl+C). Exiting immediately." | fmt -w $width
  932.     stop_processing=true
  933.     exit 1  # Exit immediately without performing cleanup
  934. }
  935.  
  936. # Trap SIGINT and call the handle_sigint function
  937. trap handle_sigint SIGINT
  938.  
  939. # Process each file found
  940. eval "find \"$video_drop_dir\" -type f \( $filetypes \)" | while read -r file; do
  941.     if [ "$stop_processing" = true ]; then
  942.         echo "Stopping processing due to SIGINT."
  943.         exit 1
  944.     fi
  945.  
  946.     case "$file" in
  947.         *.mp3|*.aac|*.wav|*.flac|*.ogg|*.m4a|*.wma|*.ac3|*.eac3|*.opus|*.amr|*.aiff|*.alac|*.caf|*.dts|*.mka|*.mp2|*.ra|*.tta|*.voc)
  948.             process_audio "$file" || exit 1
  949.             ;;
  950.  
  951.         *.mp4|*.mkv|*.avi|*.mov|*.flv|*.wmv|*.webm|*.mts|*.3gp|*.mpeg|*.ogv|*.rmvb|*.m4v|*.f4v|*.vob|*.ts|*.m2ts|*.asf|*.swf|*.m2v|*.divx|*.xvid|*.mpg|*.mpe|*.m1v|*.dvr-ms|*.mxf|*.gxf|*.bink|*.mng|*.nsv)
  952.             process_video "$file" || exit 1
  953.             ;;
  954.  
  955.         *.jpg|*.jpeg|*.png|*.bmp|*.tiff|*.gif|*.webp|*.heic|*.heif|*.jxl|*.ppm|*.pgm|*.pbm|*.pam|*.tga|*.sgi|*.pcx|*.ras|*.xbm|*.xpm|*.ico|*.dds|*.exr|*.hdr|*.svg)
  956.             process_image "$file" || exit 1
  957.             ;;
  958.     esac
  959. done
  960.  
  961. # -----------------------------
  962. #     Finishing Up
  963. # -----------------------------
  964.  
  965. width=$(tput cols)
  966.  
  967. find "$video_drop_dir" -type d -empty -delete
  968. echo "removed empty folders in $video_drop_dir directory"
  969.  
  970. # final output message
  971. echo ""
  972. echo "Log file is in $video_drop_dir.  There may be unprocessed media files or other incompatible files.  Some files may need to be remuxed for compatibility." | tee -a "$log_file" | fmt -w $width
  973. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement