diff --git a/dotfiles/bin/trim-audio b/dotfiles/bin/trim-audio index 8441e74..aa73831 100644 --- a/dotfiles/bin/trim-audio +++ b/dotfiles/bin/trim-audio @@ -25,24 +25,33 @@ else NORMAL="" fi -filename=$(basename -- "$1") -output_name="$2" +filename="$1" +output_name_with_extension="$2" start_time="$3" end_time="$4" -if [[ $filename == "" || $output_name == "" || $start_time == "" || $end_time == "" ]]; then - printf "${BOLD}${RED}Usage: trim-audio ${NORMAL}\n" +if [[ $filename == "" || $output_name_with_extension == "" || $start_time == "" ]]; then + printf "${BOLD}${RED}Usage: trim-audio ${NORMAL}\n" exit 1 fi -extension="${filename##*.}" -filename="${filename%.*}" -output="${output_name}.$extension" -timing_args="-ss $start_time -to $end_time" +timing_args="" +if [[ $start_time != "" ]]; then + timing_args="-ss $start_time " +fi +if [[ $end_time != "" ]]; then + if [[ $start_time == "0" && $end_time == "0" ]]; then + # We treat a start and end with 0 values as no op. + timing_args="" + elif [[ $end_time != "0" ]]; then + # Handle having a start time but end time is set to 0, can just ignore it and it'll use the remainder of the video. + timing_args+="-to $end_time" + fi +fi -printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | start: $start_time | end: $end_time${NORMAL}\n" +printf "\n${YELLOW}${BOLD}Trimming '$filename' | output: $output_name_with_extension | start: $start_time | end: $end_time${NORMAL}\n" -ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename.$extension" -c copy "$output" +ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename" -map a "$output_name_with_extension" printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"