Trimming the front and end of a video using Linux

I wanted to trim a bunch of videos that I had and wanted an easy and quick way to accomplish this task. I ended up piecing together a small script in order to cut the videos. There are two values you need to change in order to set the time in seconds you want to cut at the start of the script. The following package are needed to run the script

  • ffmpeg
  • bc

Also, be sure to create a sub-folder named “output” under whichever directory you choose to run the script in.


for f in *.mp4; do

	#durations to cut video in seconds

	#trim 8 second off front of video (ss -8)
	ffmpeg -i "$f" -vcodec copy -acodec copy -ss "$front" ./output/front_"$f"
	#calculate total video size
	input_duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$f")
	#calculate what needs to be cut off end
	output_duration=$(bc <<< "$input_duration"-"$back")
	#trim the end
	ffmpeg -i ./output/front_"$f" -map 0 -c copy -t "$output_duration" ./output/end_"$f"
	#clean up temp file from front trim
	rm ./output/front_"$f"
	#remove metadata
	ffmpeg -i ./output/end_"$f" -map_metadata -1 -c:v copy -c:a copy ./output/"$f"
	#clean up file from end trim
	rm ./output/end_"$f"