Skip to content

Instantly share code, notes, and snippets.

@ganapathichidambaram
Created April 17, 2022 13:12
Show Gist options
  • Save ganapathichidambaram/26f0ab7a25b3dae6c8be19466c4508f0 to your computer and use it in GitHub Desktop.
Save ganapathichidambaram/26f0ab7a25b3dae6c8be19466c4508f0 to your computer and use it in GitHub Desktop.
ffmpeg trim video - Last -N -Secs from Video
#!/bin/bash
### Execution of script
### ./ffmpeg-trim.sh input.mp4 last-n-sec-to-trim
# Arguments
FILE_RAW=$1
TRIM_EOF_DURATION=${2:-1.0} # Default is 1.0 second trimmed from EOF
# Prepare variables
BASE_PATH=$(dirname $(readlink -f $FILE_RAW))
FILENAME_EXT="$(basename "${FILE_RAW}")"
FILENAME_ONLY="${FILENAME_EXT%.*}"
EXT_ONLY="${FILENAME_EXT#*.}" # Or hardcode it like "mp4"
FILENAME_ONLY_PATH="${BASE_PATH}/${FILENAME_ONLY}"
# Trim EOF duration
INPUT_DURATION=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "${FILENAME_ONLY_PATH}.${EXT_ONLY}")
OUTPUT_DURATION=$(bc <<< "$INPUT_DURATION"-"$TRIM_EOF_DURATION")
ffmpeg -i "${FILENAME_ONLY_PATH}.${EXT_ONLY}" -map 0 -c copy -t "$OUTPUT_DURATION" "${FILENAME_ONLY_PATH}_Trim_${TRIM_EOF_DURATION}.${EXT_ONLY}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment