ffut (4970B)
1 #!/usr/bin/env bash 2 3 # Strict mode for bash; used when wanting to find errors while scripting 4 set -euo pipefail 5 IFS=$'\n\t' 6 7 RED="\033[1;31m" 8 RESET="\033[0m" 9 10 # Already defining the input file as a variable 11 # as it was being a pain to check for the input file. 12 input_file="$1" 13 14 # Usage for script 15 usage() { 16 clear 17 cat <<'EOF' 18 19 TUI for ffmpeg to cut & change codec. 20 21 Usage: ffut video_file 22 23 EOF 24 25 } 26 27 # Bash expanded [] conditions of "video/*" to [..] which 28 # is a blank string. That was why it was always giving 29 # error message. 30 check_for_vid_file() { 31 if [[ ! $(file --mime-type -b "$input_file") =~ video ]]; then 32 printf "\n%b\n" "${RED}Error${RESET}: video file not provided (got: %s)." && exit 1 33 fi 34 } 35 36 # Check for audio file for audio codec converstion 37 check_audio_file() { 38 check_input_audio_file=$(file --mime-type -b "$input_file") 39 if [[ ! $(file --mime-type -b "$input_file") =~ audio ]]; then 40 printf "\n%b\n" "${RED}Error${RESET}: audio file not provided." && exit 1 41 fi 42 } 43 44 CommandArgs=$(gum choose --limit 1 --header="Choose: cut or codec" "cut" "codec" || exit 1) 45 46 case "$CommandArgs" in 47 # Trimming videos 48 cut) 49 # Funcion call for checking video file 50 check_for_vid_file 51 # Take input from the user; what to do? 52 cut_opt=$(gum choose --limit 1 --header="Choose cut option:" "1. One point to Another" "2. One point to end of video" || exit 1) 53 case "$cut_opt" in 54 1*) 55 # First asking from where to cut and till how much 56 # then takes the input of the data and applies it to 57 # ffmpeg command. Finally, asks if original file 58 # should be deleted. 59 seek_start=$(gum input --placeholder "Start: HH:MM:SS or MM:SS or sec") 60 seek_end=$(gum input --placeholder "End: HH:MM:SS or MM:SS or sec") 61 ffmpeg -ss "$seek_start" -to "$seek_end" -i "$input_file" -c copy "$(date +"%Y%m%d_%H%M ")$input_file" || exit 1 62 gum confirm "Delete original file?" && rm "$1" 63 ;; 64 2*) 65 # Just does the same thing as above but only asks for 66 # starting point and cuts to the end of the video. 67 seek_start=$(gum input --placeholder "Start: HH:MM:SS or MM:SS or sec") 68 ffmpeg -ss "$seek_start" -i "$input_file" -c copy "$(date +"%Y%m%d_%H%M ")$input_file" 69 gum confirm "Delete original file?" && rm "$1" 70 ;; 71 *) 72 # Anything which is not a file after cut option 73 # will give out usage output. 74 usage 75 exit 1 76 ;; 77 esac 78 ;; 79 # Codec change 80 codec) 81 [[ check_for_vid_file || check_audio_file ]] 82 # Take input for changin either video or audio codec. 83 codec_opt=$(gum choose --limit 1 --header="Choose:" "video" "audio" || exit 1) 84 # On the basis of choice choose video or audio option. 85 case "$codec_opt" in 86 video) 87 video_codec_opt=$(gum choose --limit 1 --header="Choose: video format" "H.264" "H.265" "VP9 [webm]" || exit 1) 88 case "$video_codec_opt" in 89 H.264) 90 ffmpeg -i "$input_file" -c:v libx264 -crf 23 -c:a copy "$(date +"%Y%m%d_%H%M")_h264_$input_file" || exit 1 91 ;; 92 H.265) 93 ffmpeg -i "$input_file" -c:v libx265 -crf 28 -c:a copy "$(date +"%Y%m%d_%H%M")_h265_$input_file" || exit 1 94 ;; 95 "VP9 [webm]") 96 # Take the input file name, removes extension name with dot 97 # and changes codec to libvpx-vp9 and ext to webm 98 output_video_file=$(basename "$input_file" | sed 's/\.[^.]*$//') 99 output_video_file_name_webm=$(date +"%Y%m%d_%H%M")"_$output_video_file".webm 100 ffmpeg -i "$input_file" -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used -0 -c:a copy -compression_level 10 "$output_video_file_name_webm" || exit 1 101 ;; 102 *) 103 usage 104 exit 1 105 ;; 106 esac 107 ;; 108 audio) 109 check_audio_file 110 audio_codec_opt=$(gum choose --limit 1 --header="Choose:" "AAC" "OPUS" "MP3" || exit 1) 111 case "$audio_codec_opt" in 112 AAC) 113 output_audio_file=$(basename "$input_file" | sed 's/\.[^.]*$//') 114 output_audio_file_aac="$output_audio_file".m4a 115 if ffmpeg -codecs 2>/dev/null | grep -q libfdk_aac; then 116 ffmpeg -i "$input_file" -c:v copy -c:a libfdk_aac -b:a 192k "$output_audio_file_aac" 117 else 118 ffmpeg -i "$input_file" -c:v copy -c:a aac -b:a 192k "$output_audio_file_aac" 119 fi 120 ;; 121 OPUS) 122 # Basically taking audio file and changing codec to libopus 123 # and file extension to .opus 124 output_audio_file=$(basename "$input_file" | sed 's/\.[^.]*$//') 125 output_audio_file_opus="$output_audio_file".opus 126 # opus codec for best efficiency 127 ffmpeg -i "$input_file" -c:a libopus -b:a 128k -vn "$output_audio_file_opus" 128 ;; 129 MP3) 130 # Same thing changing codec to libmp3lame and file 131 # extension to .mp3 132 output_audio_file=$(basename "$input_file" | sed 's/\.[^.]*$//') 133 output_audio_file_mp3="$output_audio_file".mp3 134 # High quality mp3 conversion 135 ffmpeg -i "$input_file" -c:a libmp3lame -q:a 2 -vn "$output_audio_file_mp3" 136 ;; 137 *) 138 usage 139 exit 1 140 ;; 141 esac 142 ;; 143 esac 144 ;; 145 *) 146 usage 147 exit 1 148 ;; 149 esac