8

Is there a way to get any media file (.avi, .mp4, .mp3, etc.) length using Mac's Terminal?

I there is a need to install a package or a library can you please specify which, and how to install them?

2 Answers 2

11

You can use ffmpeg or ffprobe to get the duration. You can install ffmpeg easily using Homebrew:

brew install ffmpeg

And then you can get the duration using this command:

ffmpeg -i input 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//

See the answers here for more details: How to get length of video file from console?

2
  • Is it possible to sum up lengths of multiple files? Jul 26, 2014 at 20:30
  • You can create a bash variable and increase it as you read the length of the files. Jul 26, 2014 at 20:35
11

Mac built in:

mdls *.mp4

Tested in MacOSX 10.6.8

7
  • 7
    This seems to be downvoted, and as-written it's not as helpful as it could be, but with some extra pipes it can be great, and doesn't require installing ffmpeg: mdls *.mp4 | grep Duration | awk '{ print $3 }'. To tally up the lengths of all files in a directory you can tack on | paste -s -d+ - | bc. Sep 16, 2020 at 17:30
  • @DaveCeddia: There's never been any votes; see history. mdls & mdfind are built in Mac commands; they don't rely on (external) ffmpeg (installation). Your grep relies on output of ffmpeg.
    – Zimba
    Sep 17, 2020 at 17:40
  • Ah sorry, was thinking of a different voting system. My grep only relies on the output of mdls though... unless the output of mdls is different when ffmpeg is installed? I wouldn't think so? When I run mdls on a file it has a line that includes the string "Duration" (kMDItemDurationSeconds). Sep 17, 2020 at 23:19
  • 2
    @DaveCeddia Or you can mdls -name kMDItemFSName -name Duration 1.mp4
    – est
    Apr 3, 2021 at 15:59
  • @est Fully agree, your solution is more explicit, and avoids a dependency on grep. Using mdls -name kMDItemDurationSeconds $file | awk '{ print $3 }' solved this for me. Apr 10, 2022 at 14:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.