0

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.

Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72
  • 1
    What's wrong with using `ffprobe`? This type of task is what it is for. The console output of `ffmpeg` isn't meant to be machine parsed. – llogan Mar 07 '19 at 20:22
  • Nothing is wrong with `ffprobe`, I just need to use `ffmpeg`. I updated the question title to make it more explicit that I don't only want to "get the duration of any stream of a mp4" container, but that I want to do it with ffmpeg. – Maxime Dupré Mar 07 '19 at 20:26
  • That doesn't really answer my question. I still don't quite understand why you are unable or don't want to use `ffprobe`, so my answer would be "use `ffprobe`". – llogan Mar 07 '19 at 20:28
  • Why I can't use `ffprobe` is irrelevant...I don't care if it's more complicated (or even impossible) with `ffmpeg`, it's just a constraint I have. I'll take your answer as "Impossible with ffmpeg". – Maxime Dupré Mar 07 '19 at 20:39

2 Answers2

1

.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.

asendjasni
  • 963
  • 1
  • 16
  • 36
  • 1
    Where do you specify the type of stream and the index of the stream? – Maxime Dupré Mar 07 '19 at 20:34
  • @maximedupre @llogan for stream selection you can use the option `-map`. I updated the command above using this latter. – asendjasni Mar 07 '19 at 20:43
  • It does return a duration, however I'm note sure it returns the duration of the specific stream. I think it simply returns the duration of the total mp4 container (duration of all streams together) – Maxime Dupré Mar 07 '19 at 21:03
  • 1
    I wouldn't suggest it to you if I didn't test it myself. And as you said it seems that the stream selection is not working. I'll check it again and come back to you. – asendjasni Mar 07 '19 at 21:03
1

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.

Gyan
  • 85,394
  • 9
  • 169
  • 201