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?
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?
Mac built in:
mdls *.mp4
Tested in MacOSX 10.6.8
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
mdls
& mdfind
are built in Mac commands; they don't rely on (external) ffmpeg (installation). Your grep
relies on output of ffmpeg
.
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
mdls -name kMDItemDurationSeconds $file | awk '{ print $3 }'
solved this for me.
Apr 10, 2022 at 14:00