Get duration of video file from command line?
MediaInfo is a great tool for extracting such information, there's both a GUI and CLI version. Here is a summary of its basic command line options, you'll probably want to pass it just the filename or include a -f
switch for more details.
You can use the following PowerShell script
$path = $Args[0]
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
$shellfolder.GetDetailsOf($shellfile, 27);
Save it as a *.ps1 file, e.g. duration.ps1
. Call it like duration.ps1 path\to\video\file
It'll give time in hh:mm:ss
form. If you want it as seconds then change the last line to [timespan]::Parse($shellfolder.GetDetailsOf($shellfile, 27)).TotalSeconds
This works for any types that has duration that Windows can read. I made it from the below sources
- Get MP3/MP4 File’s MetaData
- Use PowerShell to Find Metadata from Photograph Files
Of course because it uses Shell.Application, you can use any WScript languages to achieve the same purpose. For example here's a hybrid bat-jscript version, just save it as a normal *.bat file and pass the video file path to it:
@if (@CodeSection == @Batch) @then
@echo off
cscript //e:jscript //nologo "%~f0" %1
exit /b
@end
// JScript Section
var filepath = WScript.Arguments.Item(0);
var slash = filepath.lastIndexOf('\\');
var folder = filepath.substring(0, slash);
var file = filepath.substring(slash + 1);
var shell = new ActiveXObject("Shell.Application");
var shellFolder = shell.NameSpace(folder);
shellfile = shellFolder.ParseName(file);
var duration = shellFolder.GetDetailsOf(shellfile, 27);
WScript.Echo(duration);
You'll also need to split time and multiple the hours/minutes to get seconds, but that's trivial