Adding duration info to video filenames using Mediainfo and Powershell

Categories
Contrivances Mucking About With Things Powershell The Diary of Lupin Pooter
Animated GIF of the command in action.
Animated GIF showing Windows Explorer and a terminal window and the command being used and a video filename being updated with its duration.

First, here’s a one-liner you can employ on Windows to add duration information to video filenames. It uses Powershell and assumes you’ve installed the command line version of MediaInfo for Windows. The 64-bit CLI download is what I’m using (I’ve also got the GUI version, Universal installer (32/64 bit), available from the same page). MP4s are what I needed to rename, but you can substitute, for example, MOV or add multiple file extensions.

(Get-ChildItem -Recurse -File -Filter *.mp4) | Rename-Item -NewName { $_.BaseName + '-duration-' + (mediainfo --Output="General;%Duration/String3%" $_).toString().replace(":","-").replace(".","_") + $_.Extension }

The animation above shows the command’s effect on a file originally named pigeons-in-the-rain.mp4. It’s 27 seconds and 434 milliseconds long and gets renamed to pigeons-in-the-rain-duration-00-00-27_434. Here’s the video in question:

Shaky phonecam video of some pigeons perambulating on a rainy day.

Here we stand, in the year of our Lord, 2024, and afaik there’s no existing way to display any of the properties of interest of a given filetype (e.g. wordcount for documents, dimensions of static image files, etc.) as an icon overlay for files of that stripe.

Left: what a video file looks like for me in Windows Explorer if the View is set to 'Extra large icons'. 'Large icons' and 'Medium icons' modes also give thumbnails, but 'Small icons' does not. Right: quick mockup of the sort of overlay I wish were available.
Left: what a video file looks like for me in Windows Explorer if the View is set to Extra large icons.
Large icons and Medium icons modes also give thumbnails, but Small icons does not.
Right: quick mockup of the sort of overlay I wish were available.

My specific scenario is browsing directories of photos and videos in icon mode, wherein the files show as thumbnails of the picture or (in the case of videos) of the initial frames, so that I have an idea of the subject matter but wanting to know the length of the videos. The image directly above this paragraph shows, side by side, what I currently see in Windows Explorer when I’ve set the View to Extra large icons and browse a directory containing a video file and, to its right, what I wish I could see: the same thing but with duration info in a readable overlay. The traffic cone overlay is due to my having VLC installed and using it as the default player for MP4 files.

Windows Explorer will let you see video file duration information, but only if you give up on seeing a thumbnail of the first frame.
Windows Explorer will let you see video file duration information, but only if you give up on seeing a thumbnail of the first frame.

It is possible to see durations for video files in Windows Explorer, but not in an icon view that displays thumbnails of first frames. You can switch the View mode to Details, then right-click on the horizontal bar containing the labels for the columns of information about each file. Then, navigate down to More… at the bottom of the pop-up and click on that. A Choose Details box will appear. In that little window, scroll through the very long alphabetically-arranged list of available extra column types until you find Length. Check that checkbox and hit the OK button on the bottom of the Choose Details box and you get the lengths (i.e. durations). After you’ve added a Length column for a folder, it shows up in the list of columns available when you right-click on the labels bar, as in the image above.

The snippet I’m sharing in this post gets me close enough to what I want for my purposes. The modified file name including the duration value is visible in Extra large icons viewing mode and, if the original filenames are long enough that the displayed filenames under the thumbnails get truncated, I can reorder the new filename parts in the command to put the duration values at the front.

Filetype-specific details in icon overlays seems like the sort of feature that really ought to have become standard as an option (realistically, switched off by default but easily enable-able) years ago. Perhaps it is and I’ve missed it or it’s been added to more-recent-than-I’ve-used releases of Apple’s desktop OS or of a Linux file manager or there are Windows add-ons that provide this functionality and I just haven’t been able to find them. If so, I’d be interested in knowing more.

I’m not a Powershell aficionado or command-line wunderkind, so use this one-liner at your own risk. It works for me but, as always, YMMV. I cobbled it together through trial and error concurrent with repeated searches of online fora and blogs and it could undoubtedly be greatly improved… by somebody else.

P.S. A Short Note about mediainfo --Output="General;%Duration/StringN%"

It occurs to me that I ought to mention that mediainfo will yield video file durations in a variety of formats and that the replace()operations in my snippet are a consequence of the one that I chose to use.

Here’s the output of the mediainfo part of the command (run on a longer video), with different versions of the Duration string specified:

mediainfo --Output="General;%Duration/String1%" y.mp4  (yields 1 h 7 min 31 s 796 ms)
mediainfo --Output="General;%Duration/String2%" y.mp4  (yields 1 h 7 min)
mediainfo --Output="General;%Duration/String3%" y.mp4  (yields 01:07:31.796)
mediainfo --Output="General;%Duration/String4%" y.mp4  (yields 01:07:32;01)
mediainfo --Output="General;%Duration/String5%" y.mp4  (yields 01:07:31.796 (01:07:32;01))

The third (N=3) version of the duration output is what I’m using.