Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/data/data/com.termux/files/usr/bin/bash
- # ATTENTION:
- # 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.
- # ex. dos2unix ziggle_wump.sh
- # --------------------
- # Licence
- # --------------------
- # 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.
- # 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.
- # You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
- # -----------------
- # About
- # -----------------
- # This script is for Termux on Android and is not associated with the apps it uses.
- # Copyright Joshua Hansen and contributors: Microsoft Co-Pilot, OpenAI ChatGPT and Google Gemini.
- # 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.
- # -----------------------
- # Instructions
- # -----------------------
- instructions="#Ziggle Wump: The Simple FFmpeg Command Line Companion Script for Termux on Android
- # Disclaimer
- * This script does not allow conversion of encrypted files. Encrypted content typically indicates copyrighted material.
- * The script itself does not violate copyright, but the user's 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.
- * It is not recommended to use the script to violate copyright law. US copyright law allows for spaceshifting and fair use of copyrighted material.
- # Note
- * Some phones, like the Galaxy S24, have battery-saving features that can impact the encoding process and result in partially encoded files. Ensure Termux is in focus (full screen or split-screen) while encoding, and keep the screen on during the process.
- * For more information and potential fixes for specific phones, visit https://dontkillmyapp.com/.
- # Getting Started
- * 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 /usr/bin to use it system-wide.
- * Make it Executable: Run chmod +x ziggle_wump.sh to make the script executable.
- * Run the Script: Execute the script using bash ./ziggle_wump.sh [options].
- # Options
- * -r resolution: Sets a custom resolution height while preserving aspect ratio (e.g., bash ziggle_wump.sh -r 720).
- * -d: Checks and upgrades dependencies.
- * -y: Automatically confirms prompts.
- * -o output_fps: Sets a custom output FPS (e.g., bash ziggle_wump.sh -o 60).
- * -b max_video_bitrate: Sets a custom maximum video bitrate in kilobits per second (e.g., bash ziggle_wump.sh -b 2000).
- * -a avg_audio_bitrate: Sets a custom average audio bitrate in kilobits per second (e.g., bash ziggle_wump.sh -a 128).
- * -i: Installs the script to /data/data/com.termux/files/usr/bin/zwmc.
- * -u: Uninstalls the script from /data/data/com.termux/files/usr/bin/zwmc.
- * -m: Shows the menu for setting options.
- * -h, --help: Displays help message."
- # -----------------------------
- # Variables
- # -----------------------------
- # Supported File Types
- 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"
- audio_ext="mp3 aac wav flac ogg m4a wma ac3 eac3 opus amr aiff alac caf dts mka mp2 ra tta voc"
- # Maximum resolution by height in pixels maintaining aspect ratio. Either the user defined value or the input video original value will be selected
- resolution=600
- max_video_bitrate=688 # In Kilobits per second.
- # Set the audio bitrate for both videos and audio files.
- avg_audio_bitrate=96 # In Kilobits per second.
- output_fps=24 # Maximum output FPS. Original or user input, whichever is lower.
- # Full Log file path
- log_file="$HOME/storage/shared/Movies/VideoDrop/convert.log"
- # Shortened log file path for terminal output
- log_file_echo=$(echo "$log_file" | sed 's|/data/data/com.termux/files/home|.../home|g')
- # Full directory paths
- video_drop_dir="$HOME/storage/shared/Movies/VideoDrop"
- video_processing_dir="$HOME/storage/shared/Movies/VideoProcessing"
- output_dir_base="$HOME/storage/shared/Movies/VideoConverted"
- # Shortened directory paths for terminal output
- video_drop_dir_echo=$(echo "$video_drop_dir" | sed 's|/data/data/com.termux/files/home|.../home|g')
- video_processing_dir_echo=$(echo "$video_processing_dir" | sed 's|/data/data/com.termux/files/home|.../home|g')
- output_dir_base_echo=$(echo "$output_dir_base" | sed 's|/data/data/com.termux/files/home|.../home|g')
- # combine audio and video extension variables to pass to find command.
- filetypes=""
- for ext in $audio_ext $video_ext; do
- filetypes="$filetypes -iname '*.$ext' -o"
- done
- # Remove the trailing ' -o'
- filetypes="${filetypes% -o}"
- # --------------------------------
- # Command Flags
- # --------------------------------
- show_menu() {
- auto_yes=true # Turn on auto_yes when the menu is shown
- while true; do
- deps_status="Off"
- if [ "$check_deps" = true ]; then
- deps_status="On"
- fi
- menu_choice=$(dialog --menu "Choose an option:" 15 50 8 \
- 1 "Set Resolution Height ($resolution)" \
- 2 "Set Output FPS ($output_fps)" \
- 3 "Set Max Video Bitrate ($max_video_bitrate)" \
- 4 "Set Average Audio Bitrate ($avg_audio_bitrate)" \
- 5 "Check Dependencies ($deps_status)" \
- 6 "Install Script" \
- 7 "Uninstall Script" \
- 8 "Compress Files" 2>&1 >/dev/tty)
- case $menu_choice in
- 1)
- resolution=$(dialog --inputbox "Enter resolution height ($resolution):" 8 40 "$resolution" 2>&1 >/dev/tty)
- ;;
- 2)
- output_fps=$(dialog --inputbox "Enter output FPS ($output_fps):" 8 40 "$output_fps" 2>&1 >/dev/tty)
- ;;
- 3)
- max_video_bitrate=$(dialog --inputbox "Enter max video bitrate ($max_video_bitrate):" 8 40 "$max_video_bitrate" 2>&1 >/dev/tty)
- ;;
- 4)
- avg_audio_bitrate=$(dialog --inputbox "Enter average audio bitrate ($avg_audio_bitrate):" 8 40 "$avg_audio_bitrate" 2>&1 >/dev/tty)
- ;;
- 5)
- if [ "$check_deps" = true ]; then
- check_deps=false
- else
- check_deps=true
- fi
- ;;
- 6)
- clear
- install_script
- ;;
- 7)
- clear
- uninstall_script
- ;;
- 8)
- #auto_yes=true
- break
- ;;
- *)
- tput sgr0 # Reset terminal colors
- clear
- exit 0
- ;;
- esac
- done
- }
- # Function to display help message
- show_help() {
- width=$(tput cols)
- script_name=$(basename "$0")
- echo "Place your media files in $video_drop_dir_echo." | tee -a "$log_file" | fmt -w $width
- echo "Usage: bash $script_name [-r resolution] [-d] [-y] [-h|--help] [-o output_fps] [-b max_video_bitrate] [-a avg_audio_bitrate] [-i] [-u] [-m]" | fmt -w $width
- echo " -r resolution ex. bash $script_name -r 720 Custom resolution height preserving aspect ratio" | fmt -w $width
- echo " -d Check and upgrade dependencies." | fmt -w $width
- echo " -y Automatically say yes to prompts." | fmt -w $width
- echo " -o output_fps Set custom output FPS. ex. bash $script_name -o 60" | fmt -w $width
- echo " -b max_video_bitrate Set custom max video bitrate in kilobits per second. ex. bash $script_name -b 2000" | fmt -w $width
- echo " -a avg_audio_bitrate Set custom average audio bitrate in kilobits per second. ex. bash $script_name -a 128" | fmt -w $width
- echo " -i Install the script to /data/data/com.termux/files/usr/bin/zwmc" | fmt -w $width
- echo " -u Uninstall the script from /data/data/com.termux/files/usr/bin/zwmc" | fmt -w $width
- echo " -m: Shows the menu for setting options."
- echo " -h, --help Display this help message" | fmt -w $width
- exit 0
- }
- install_script() {
- if [ -f /data/data/com.termux/files/usr/bin/zwmc ]; then
- current_dir=$(dirname "$0")
- if [ "$current_dir" != "/data/data/com.termux/files/usr/bin" ]; then
- echo "Script is installed but not running from the bin directory. Reinstalling..."
- cp "$0" /data/data/com.termux/files/usr/bin/zwmc
- chmod +x /data/data/com.termux/files/usr/bin/zwmc
- if whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
- echo "Script reinstalled to /data/data/com.termux/files/usr/bin/zwmc"
- else
- echo "Reinstallation failed"
- fi
- else
- echo "Script is already installed and running from the bin directory."
- fi
- else
- cp "$0" /data/data/com.termux/files/usr/bin/zwmc
- chmod +x /data/data/com.termux/files/usr/bin/zwmc
- if whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
- echo "Script installed to /data/data/com.termux/files/usr/bin/zwmc"
- else
- echo "Installation failed"
- fi
- fi
- exit 0
- }
- uninstall_script() {
- if [ ! -f /data/data/com.termux/files/usr/bin/zwmc ]; then
- echo "Script is not installed."
- exit 0
- else
- rm -f /data/data/com.termux/files/usr/bin/zwmc
- if ! whereis zwmc | grep -q "/data/data/com.termux/files/usr/bin/zwmc"; then
- echo "Script uninstalled from /data/data/com.termux/files/usr/bin/zwmc"
- else
- echo "Uninstallation failed"
- fi
- fi
- exit 0
- }
- # Parse command-line options
- check_deps=false
- auto_yes=false # Set auto_yes to false by default
- while getopts "r:dyho:b:a:ium" opt; do
- case $opt in
- r) resolution="$OPTARG" ;; # Set custom resolution by height
- d) check_deps=true ;; # Check and upgrade dependencies
- y) auto_yes=true ;; # Automatically say yes to prompts
- o) output_fps="$OPTARG" ;; # Set custom output FPS
- b) max_video_bitrate="$OPTARG" ;; # Set custom max video bitrate
- a) avg_audio_bitrate="$OPTARG" ;; # Set custom average audio bitrate
- i) install_script ;; # Install the script
- u) uninstall_script ;; # Uninstall the script
- m) auto_yes=true; show_menu ;; # Show menu and turn on auto_yes
- h) show_help ;; # Display help
- *) show_help ;; # Display help for invalid options
- esac
- done
- shift $((OPTIND -1))
- # ---------------------------------
- # Dependencies
- # ---------------------------------
- if $check_deps; then
- echo "Checking and upgrading dependencies, please wait..."
- pkg update && pkg upgrade -y
- if ! command -v bc &> /dev/null; then
- pkg install bc -y
- fi
- if ! command -v ffmpeg &> /dev/null; then
- pkg install ffmpeg -y
- fi
- if ! command -v ncurses-utils &> /dev/null; then
- pkg install ncurses-utils -y
- fi
- echo "Update complete."
- fi
- # -----------------------------------------------------
- # Create Directories and log file
- # -----------------------------------------------------
- # Create necessary directories if they don't exist
- mkdir -p "$video_drop_dir" "$video_processing_dir" "$output_dir_base"
- # Change to the VideoDrop directory
- cd "$video_drop_dir" || { echo "Directory change failed"; exit 1; }
- # Check if log file exists
- if [ -f "$log_file" ]; then
- echo "Old log file found. Deleting..."
- rm "$log_file"
- echo "File deleted."
- else
- echo "Log File does not exist. The script will create $log_file_echo"
- fi
- # ------------------------
- # Start script
- # ------------------------
- width=$(tput cols)
- echo "Running... Press Ctrl+C to stop."
- echo ""
- echo "$instructions" | tee -a "$log_file" | fmt -w $width
- echo ""
- # Function to prompt the user to continue or quit
- prompt_continue_or_quit() {
- while true; do
- # Recursively find and process videos and audio
- video_count=$(eval "find \"$video_drop_dir\" -type f \( $filetypes \) | wc -l")
- echo "Detected $video_count compatible video and/or audio file(s) in:" | tee -a "$log_file" | fmt -w $width
- if [ "$video_count" -eq 0 ]; then
- echo "$video_drop_dir_echo 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.
- Supported File Types:
- Video: $video_ext
- Audio: $audio_ext" | tee -a "$log_file" | fmt -w $width
- exit 0
- fi
- # Prompt text
- prompt_text="$video_drop_dir_echo. You can start encoding now. Do you wish to proceed? (Y/N): "
- # Format the prompt text
- formatted_prompt=$(echo "$prompt_text" | fmt -w $width)
- if $auto_yes; then
- echo "$formatted_prompt"
- echo "Y"
- return 0 # Automatically continue
- else
- # Read user input with formatted prompt
- read -p "$formatted_prompt" yn
- case $yn in
- [Yy]* ) return 0;; # Continue
- [Nn]* ) echo "Exiting script."; exit 0;; # Quit
- * ) echo "Please answer Y or N.";;
- esac
- fi
- done
- }
- # Function to clean up file names
- clean_file_names() {
- eval "find \"$video_drop_dir\" -type f \( $filetypes \)" | while read -r file; do
- dir=$(dirname "$file")
- base=$(basename "$file")
- new_base=$(echo "$base" | sed 's/[^a-zA-Z0-9 ._-]//g' | tr -s ' ')
- new_file="$dir/$new_base"
- if [ "$file" != "$new_file" ]; then
- mv "$file" "$new_file"
- fi
- done
- }
- # Clean up file names before processing
- clean_file_names
- # Prompt the user to continue or quit?
- prompt_continue_or_quit
- echo "Starting conversion process..." | tee -a "$log_file"
- # Initialize ffmpeg_custom_options
- 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 32 -preset slow -c:a libopus -vbr on -ac 2 -af \"loudnorm=I=-23:LRA=7:TP=-2\""
- # Add avg audio bitrate if avg_audio_bitrate is set
- if [ -n "$avg_audio_bitrate" ]; then
- ffmpeg_custom_options="$ffmpeg_custom_options -b:a ${avg_audio_bitrate}k"
- fi
- # Add maxrate and bufsize options if max_video_bitrate is set
- if [ -n "$max_video_bitrate" ]; then
- ffmpeg_custom_options="$ffmpeg_custom_options -maxrate ${max_video_bitrate}k -bufsize 60M"
- fi
- # -----------------------------
- # Audio Encoder
- # -----------------------------
- width=$(tput cols)
- # Function to check the log file for "Killed" message
- check_for_killed_message() {
- if grep -q " Killed " "$log_file"; then
- echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Check log file for details." | tee -a "$log_file"
- return 1
- fi
- return 0
- }
- # Function to process audio files
- process_audio() {
- local audio="$1"
- local audio_echo=$(echo "$audio" | sed 's|/data/data/com.termux/files/home|/home|g')
- local relative_path="${audio#$video_drop_dir/}"
- local dir_path=$(dirname "$relative_path")
- local base_name=$(basename "$relative_path" | tr -cd '[:alnum:]._ -')
- local output_dir="$output_dir_base/$dir_path"
- local output_file="$output_dir/${base_name}_converted.opus"
- local temp_output_file="$output_file.tmp"
- mkdir -p "$output_dir" # Create output directory
- echo "$(date '+%Y-%m-%d %H:%M:%S') Processing $audio to $output_file" | tee -a "$log_file"
- # Construct the FFmpeg command for audio processing
- 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\""
- echo "$ffmpeg_command" | tee -a "$log_file"
- # Execute the FFmpeg command
- eval $ffmpeg_command 2>&1 | tee -a "$log_file"
- # Check for "Killed" message in the log file
- if ! check_for_killed_message; then
- echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Exiting." | tee -a "$log_file"
- exit 1
- fi
- if [ $? -eq 0 ]; then
- mv "$temp_output_file" "$output_file" # Rename the temporary file to the final output file
- mkdir -p "$video_processing_dir/$dir_path" # Create processing directory
- mv "$audio" "$video_processing_dir/$relative_path"
- echo ""
- echo "$(date '+%Y-%m-%d %H:%M:%S') Converted $audio_echo. Your original files will be in the $video_processing_dir_echo folder for comparison. Your new files are in $output_dir_base_echo" | tee -a "$log_file" | fmt -w $width
- else
- echo ""
- echo "$(date '+%Y-%m-%d %H:%M:%S') Error processing \"$audio\". Check log file for details." | tee -a "$log_file"
- exit 1
- fi
- }
- # ------------------------------
- # Video Encoder
- # ------------------------------
- width=$(tput cols)
- # Function to check the log file for "Killed" message
- check_for_killed_message() {
- if grep -q " Killed " "$log_file"; then
- echo "$(date '+%Y-%m-%d %H:%M:%S') Error: Process was killed. Check log file for details." | tee -a "$log_file" | fmt -w $width
- exit 1
- fi
- }
- process_video() {
- local video="$1"
- local video_echo=$(echo "$video" | sed 's|/data/data/com.termux/files/home|.../home|g')
- local relative_path="${video#$video_drop_dir/}"
- local dir_path=$(dirname "$relative_path")
- local base_name=$(basename "$relative_path" | tr -cd '[:alnum:]._ -')
- local output_dir="$output_dir_base/$dir_path"
- local output_file="$output_dir/${base_name}_converted.mkv"
- local scale_filter=""
- # Get input video resolution
- input_resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$video" | tr -d '[:space:],')
- if [ -n "$resolution" ]; then
- if [ "$input_resolution" -gt "$resolution" ]; then
- scale_filter="scale=-2:$resolution:flags=lanczos"
- echo "Input resolution ($input_resolution) is higher than user-defined resolution ($resolution). Using user-defined resolution." | tee -a "$log_file"
- else
- echo "Input resolution ($input_resolution) is lower or equal to user-defined resolution ($resolution). Keeping original resolution." | tee -a "$log_file"
- fi
- else
- echo "Resolution set to: original" | tee -a "$log_file"
- fi
- mkdir -p "$output_dir" # Create output directory
- echo "$(date '+%Y-%m-%d %H:%M:%S') Processing $video to $output_file" | tee -a "$log_file"
- # Get the frame rate of the input video
- 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)
- # Define common frame rates
- common_fps=(23.976 24 25 29.97 30 50 59.94 60)
- # Function to find the nearest valid frame rate
- nearest_fps() {
- local fps=$1
- local nearest=${common_fps[0]}
- local min_diff=$(echo "scale=5; $fps - ${common_fps[0]}" | bc | awk '{print ($1 >= 0) ? $1 : -$1}')
- for rate in "${common_fps[@]}"; do
- local diff=$(echo "scale=5; $fps - $rate" | bc | awk '{print ($1 >= 0) ? $1 : -$1}')
- if (( $(echo "$diff < $min_diff" | bc -l) )); then
- min_diff=$diff
- nearest=$rate
- fi
- done
- echo $nearest
- }
- # Round the frame rate to the nearest valid frame rate
- rounded_fps=$(nearest_fps $input_fps)
- # Determine the lower frame rate
- if (( $(echo "$rounded_fps > $output_fps" | bc -l) )); then
- final_fps=$output_fps
- else
- final_fps=$rounded_fps
- fi
- # Set the initial video filter option
- video_filter_option="yadif=2,fps=$final_fps,${scale_filter}"
- # Detect the pixel format using ffprobe
- input_file="$video"
- 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}')
- echo "Pixel format: $pix_fmt"
- # Validate the pixel format
- valid_pix_fmt=$(ffmpeg -pix_fmts | grep -w "$pix_fmt")
- # Check if 10-bit encoding is selected
- if [[ "$pix_fmt" == *"10le"* || "$pix_fmt" == *"10be"* ]]; then
- 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"
- fi
- # Define a variable for probe size and analyze duration
- probe_analyze_opts="-probesize 2147483647 -analyzeduration 2147483647"
- # Detect subtitle codecs and convert unsupported ones
- subtitle_codecs=$(ffprobe -v error $probe_analyze_opts -select_streams s -show_entries stream=codec_name -of csv=p=0 "$input_file")
- subtitle_map=""
- if [ -n "$subtitle_codecs" ]; then
- subtitle_map="-map 0:s" # Include all subtitle streams
- i=0
- while read -r codec; do
- case "$codec" in
- # Text-based subtitles to be copied directly
- srt)
- subtitle_map="$subtitle_map -c:s:$i copy"
- ;;
- # Other text-based subtitles to be converted to SRT
- ass|ssa|webvtt|eia_608|eia_708|scc|sami|ttml|smi|teletext|mov_text|microdvd|subviewer)
- subtitle_map="$subtitle_map -c:s:$i srt"
- ;;
- # Bitmap-based subtitles to be converted to DVD subtitles
- dvdsub|pgs|vobsub|hdmv_pgs_subtitle|dvd_subtitle)
- subtitle_map="$subtitle_map -c:s:$i dvdsub"
- ;;
- # Copy other compatible subtitles
- *)
- subtitle_map="$subtitle_map -c:s:$i copy"
- ;;
- esac
- i=$((i + 1))
- done <<< "$subtitle_codecs"
- else
- echo "No subtitle streams detected." | tee -a "$log_file"
- fi
- # Combine common and custom FFmpeg options
- 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"
- # Construct the FFmpeg command
- local ffmpeg_command
- if [ -n "$valid_pix_fmt" ]; then
- if [ -n "$subtitle_map" ]; then
- ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" -pix_fmt $pix_fmt $subtitle_map"
- else
- ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" -pix_fmt $pix_fmt"
- fi
- else
- if [ -n "$subtitle_map" ]; then
- ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\" $subtitle_map"
- else
- ffmpeg_command="ffmpeg $combined_ffmpeg_options -vf \"$video_filter_option\""
- fi
- fi
- # Ensure all subtitle streams are marked as "default: off"
- subtitle_streams=$(ffprobe -v error $probe_analyze_opts -select_streams s -show_entries stream=index -of csv=p=0 "$input_file")
- if [ -n "$subtitle_streams" ]; then
- for stream_index in $subtitle_streams; do
- ffmpeg_command="$ffmpeg_command -disposition:s:$stream_index 0"
- done
- fi
- # Add the output file name at the end
- ffmpeg_command="$ffmpeg_command \"$output_file\""
- echo "FFmpeg command: $ffmpeg_command" | tee -a "$log_file"
- # Execute the FFmpeg command
- if ! eval $ffmpeg_command 2>&1 | tee -a "$log_file"; then
- echo "FFmpeg command failed. Check the log for details." | tee -a "$log_file"
- exit 1
- fi
- # Check for "Killed" message in the log file
- check_for_killed_message
- if [ $? -eq 0 ]; then
- mkdir -p "$video_processing_dir/$dir_path" # Create processing directory
- mv "$video" "$video_processing_dir/$relative_path"
- echo ""
- echo "$(date '+%Y-%m-%d %H:%M:%S') Complete. Your original files are in $video_processing_dir_echo. Your new files are in $output_dir_base_echo" | tee -a "$log_file" | fmt -w $width
- else
- echo ""
- echo "$(date '+%Y-%m-%d %H:%M:%S') Error processing \"$video\". Check log file for details." | tee -a "$log_file"
- exit 1
- fi
- }
- # --------------------------------------
- # Start Batch Encoding
- # --------------------------------------
- width=$(tput cols)
- stop_processing=false
- # Function to handle SIGINT (Ctrl+C)
- handle_sigint() {
- echo ""
- echo "Caught SIGINT (Ctrl+C). Exiting immediately." | fmt -w $width
- stop_processing=true
- exit 1 # Exit immediately without performing cleanup
- }
- # Trap SIGINT and call the handle_sigint function
- trap handle_sigint SIGINT
- # Process each file found
- eval "find \"$video_drop_dir\" -type f \( $filetypes \)" | while read -r file; do
- if [ "$stop_processing" = true ]; then
- echo "Stopping processing due to SIGINT."
- exit 1
- fi
- case "$file" in
- *.mp3|*.aac|*.wav|*.flac|*.ogg|*.m4a|*.wma|*.ac3|*.eac3|*.opus|*.amr|*.aiff|*.alac|*.caf|*.dts|*.mka|*.mp2|*.ra|*.tta|*.voc)
- process_audio "$file" || exit 1
- ;;
- *.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)
- process_video "$file" || exit 1
- ;;
- esac
- done
- # -----------------------------
- # Finishing Up
- # -----------------------------
- width=$(tput cols)
- find "$PWD" -type d -empty -delete
- echo "removed empty folders in $video_drop_dir_echo directory"
- # final output message
- echo ""
- echo "Log file is in $video_drop_dir_echo. 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
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement