From e61493647843466bd7f8291c09e4057a4bb39f8b Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Sun, 7 May 2023 16:11:49 -0400 Subject: [PATCH] Add a trim video script that is more accurate --- bin/trim-accurate-video | 48 +++++++++++++++++++++++++++++++++++++++++ bin/trim-video | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 bin/trim-accurate-video diff --git a/bin/trim-accurate-video b/bin/trim-accurate-video new file mode 100644 index 0000000..6bd29c6 --- /dev/null +++ b/bin/trim-accurate-video @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# Reencodes the video with a more accurate length. + +if which tput >/dev/null 2>&1; then + ncolors=$(tput colors) +fi +if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + BLUE="$(tput setaf 4)" + MAGENTA="$(tput setaf 5)" + CYAN="$(tput setaf 6)" + BOLD="$(tput bold)" + NORMAL="$(tput sgr0)" +else + RED="" + GREEN="" + YELLOW="" + BLUE="" + MAGENTA="" + CYAN="" + BOLD="" + NORMAL="" +fi + +filename=$(basename -- "$1") +output_name="$2" +start_time="$3" +end_time="$4" + +if [[ $filename == "" || $output_name == "" || $start_time == "" || $end_time == "" ]]; then + printf "${BOLD}${RED}Usage: $0 ${NORMAL}\n" + exit 1 +fi + +extension="${filename##*.}" +filename="${filename%.*}" +output="${output_name}.$extension" +timing_args="-ss $start_time -to $end_time" + +printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | start: $start_time | end: $end_time${NORMAL}\n" + +ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename.$extension" -c:v libx264 -c:a copy "$output" + +printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n" + diff --git a/bin/trim-video b/bin/trim-video index 723fecb..7d54049 100644 --- a/bin/trim-video +++ b/bin/trim-video @@ -40,7 +40,7 @@ timing_args="-ss $start_time -to $end_time" printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | start: $start_time | end: $end_time${NORMAL}\n" -ffmpeg -y -stats -loglevel level+error $timing_args -i "$filename.$extension" -c:a copy -c:v copy "$output" +ffmpeg -y -stats -loglevel level+error $timing_args -i "$filename.$extension" -c copy "$output" printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"