Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created November 20, 2023 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isurfer21/3badae6da8cb9b13ab6971a5d4bb2087 to your computer and use it in GitHub Desktop.
Save isurfer21/3badae6da8cb9b13ab6971a5d4bb2087 to your computer and use it in GitHub Desktop.
A CLI wrapper app to use ffmpeg to remove the clip from the given video file based on begin & end timestamp
#!/bin/bash
# Check the number of arguments
if [ $# -ne 4 ]; then
echo "Usage:"
echo " ${0##*/} <input> <output> <start> <end>"
echo "Arguments:"
echo " input the name of the input video file"
echo " output the name of the output video file"
echo " start the start time of the clip to be removed in hh:mm:ss format"
echo " end the end time of the clip to be removed in hh:mm:ss format"
exit 1
fi
# Assign the arguments to variables
input=$1
output=$2
start_hms=$3
end_hms=$4
# Check the format of the arguments
if [[ ! -f $input ]]; then
echo "Invalid input file: $input"
exit 2
fi
if [[ ! $start_hms =~ ^[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
echo "Invalid start time: $start_hms"
exit 3
fi
if [[ ! $end_hms =~ ^[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
echo "Invalid end time: $end_hms"
exit 4
fi
# Convert the hour:minute:second format to seconds
start_s=$(echo $start_hms | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
end_s=$(echo $end_hms | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
# Calculate the duration of the clip to be removed
duration=$(bc <<< "$end_s - $start_s")
# Use ffmpeg to remove the clip and save the output
ffmpeg -i $input -filter_complex "[0:v]trim=0:$start_s,setpts=PTS-STARTPTS[v1]; [0:v]trim=$end_s,setpts=PTS-STARTPTS[v2]; [0:a]atrim=0:$start_s,asetpts=PTS-STARTPTS[a1]; [0:a]atrim=$end_s,asetpts=PTS-STARTPTS[a2]; [v1][a1][v2][a2]concat=n=2:v=1:a=1[out]" -map "[out]" $output
# Print a message to indicate the completion of the task
echo "The clip from $start_hms to $end_hms has been removed from $input and saved as $output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment