Trim audio file using start and stop times

I have an FFmpeg command to trim audio:

ffmpeg -ss 01:43:46 -t 00:00:44.30 -i input.mp3 output.mp3

The problem I have with this command is that option -t requires a duration (in seconds) from 01:43:46. I want to trim audio using start/stop times, e.g. between 01:43:46 and 00:01:45.02.

Is this possible?

Asked By: whitewings


If you have PHP installed, make it a script:

TimeDiff.php contents:

// Create datetime objcects
$dt1 = new DateTime($argv[1]);
$dt2 = new DateTime($argv[2]);

// Conver difference to seconds
$dt3 = $dt2->format('U') - $dt1->format('U');

// echo $dt3."n";
$h = (int)($dt3 / 3600);
$dt3 %= 3600;
$m = (int)($dt3 / 60);
$dt3 %= 60;
$s = $dt3;

// Dump as H:M:S
echo $h . ":" . $m . ":" . $s;

?> contents:


OFFSET=`php TimeDiff.php "$START" "$STOP"`

echo "Disecting $INFILE starting from $START to $STOP (duration $OFFSET)"
ffmpeg -ss "$START" -t "$OFFSET" -i "$INFILE" "$OUTFILE"


./ [input.mp3] [startchop] [stopchop] [output.mp3]

Where [startchop] and [stopchop] are both absolute timestamps from the beginning of the track.

NB: Script(s) may need tweaking depending on platform version etc…

Answered By: Litch

ffmpeg seems to have a new option -to in the documentation:

-to position (input/output)
Stop writing the output or reading the input at position. position
must be a time duration specification, see (ffmpeg-utils)the Time
duration section in the ffmpeg-utils(1) manual.

-to and -t are mutually exclusive and -t has priority.

Sample command with two time formats

ffmpeg -i file.mkv -ss 20 -to 40 -c copy file-2.mkv
ffmpeg -i file.mkv -ss 00:00:20 -to 00:00:40 -c copy file-2.mkv

This should create a copy (file-2.mkv) of file.mkv from the 20 second mark to the 40 second mark.

Answered By: Miati
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.