Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut end of multiple videos

I need a script to cut off the last 6 seconds of multiple videos. The videos do all have different length.

I can‘t find anything helpful online.

Does anyone know how to do that? thx

like image 538
Marvin von Rappard Avatar asked Apr 22 '18 09:04

Marvin von Rappard


Video Answer


1 Answers

A method that uses ffprobe to get the input duration, bc to calculate the desired output duration, and ffmpeg to perform the cutting. This method does not require the inputs to contain an audio stream, but it requires two additional tools (ffprobe and bc) instead of just ffmpeg.

Your preferred scripting language wasn't mentioned so I'll assume bash will do. In a script form as requested:

#!/bin/bash
for f in *.mp4; do
cut_duration=6
input_duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$f")
output_duration=$(bc <<< "$input_duration"-"$cut_duration")
  ffmpeg -i "$f" -map 0 -c copy -t "$output_duration" output/"$f"
done

Or as a single line:

for f in *.mp4; do ffmpeg -i "$f" -map 0 -c copy -t "$(bc <<< "$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$f")"-6)" output/"$f"; done
like image 91
llogan Avatar answered Sep 28 '22 05:09

llogan