#!/usr/bin/env bash # A fast video trim that might result in some weird seek timings, e.g. video # player might show negative seconds at the start of the vid or the playback # time might oscillate a bit. Use trim-video for accurate trimming at the cost # of a much slower processing time. 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 == "" ]]; then printf "${BOLD}${RED}Usage: trim-video-fast ${NORMAL}\n" exit 1 fi extension="${filename##*.}" filename="${filename%.*}" output="${output_name}.$extension" 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" # You might have issues if the file has multiple video streams or embedded subtitles. The -map 0 arg is typically given # when copying a video stream, but I'm not sure if it's appropriate to use here. ffmpeg -y -stats -loglevel level+error $timing_args -i "$filename.$extension" -c copy "$output" printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"