How to get the total length of multiple videos using terminal

Francesco Pastore
2 min readJan 1, 2021

If you got some videos in a folder, how can you know how much they long using the terminal? In this article, we will see a simple command to get the total length of all videos in a directory or in a subtree.

This is the solution:

mediainfo  --Inform="Video;%Duration%\n" * | awk '
BEGIN {FS=OFS="\n"} NR == 1 { total = $1; next } { total += $1 } END {
hours = int(total / 3600000);
minutes = int(total / 60000) - hours * 60;
seconds = int(total / 1000) - minutes * 60 - hours * 3600;
milliseconds = total - seconds * 1000 - minutes * 60000 - hours * 3600000;
print hours ":" minutes ":" seconds "." milliseconds

In the first part, we are using mediainfo to get the duration in milliseconds of all videos in the current subtree. There can be some problems with filenames with multiple spaces or strange characters. In these case, write them directly or temporarily renamed with fictitious names.

Be careful, subdirectories are included in the command given. Instead, if you want to select only files included in the current dir use this as the first part:

mediainfo  --Inform="Video;%Duration%\n" "\$(find . -maxdepth 1 -type f -printf "\"%f\" ")" | ...

In the second part, with an awk script, we sum all milliseconds values returned by mediainfo and converted them in hours, minutes, seconds and remaining milliseconds.

For example, in test folder I downloaded and put in it three videos from this link that they last only one minute.

Each video lasts 1 minute, so the result is 3 minutes



Francesco Pastore

An engineering student in Milan and a web developer for an IT company. Write about programming and cybersecurity topics.