Maxime Dupré
Maxime Dupré

Reputation: 5757

How to get the duration of any stream of a mp4 container with ffmpeg?

I'm not sure, but the following command seems to do just that. Here I get the duration of the first audio stream of the mp4 file:

ffprobe -v error -select_streams a:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 -i input.mp4

However, I'm looking for the equivalent with ffmpeg (if it exists). Your answer is equally valid if it's that it's not possible to do that with ffmpeg.

Upvotes: 0

Views: 1762

Answers (2)

Gyan
Gyan

Reputation: 93329

Use

ffmpeg -i input.mp4 -c copy -map 0:v:3 -f null - 2>&1 | tail -3 | grep -oP "(?<=time=).+?\s"

0:v:3 will select the fourth video stream. If it exists, output will be a duration e.g. 00:02:57.64

If it doesn't exist, output will be empty.

Upvotes: 2

asendjasni
asendjasni

Reputation: 1084

.Using FFmpeg to get the media file duration, you can do this:

ffmpeg -i inputfile -map 0:0 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//

For further reading about the Map option.

Source.

Upvotes: 1

Related Questions