Get duration of video file from command line?

7

0

In Windows, is there a way to get the duration (e.g. in seconds) of an AVI file from the command line?

I'm fine with using 3rd party tools - the more common, the better.

Cristi Diaconescu

Posted 2013-05-20T11:19:11.440

Reputation: 2 810

It's not possible to retrieve the duration of a transport stream what you can do is estimate the duration. Such as — Duration of a MPEG-TS video file is not possible retrieve (even you can't estimate) if you don't iterate all the packets or estimate the bitrate of it. – Quadcubic – 2019-12-24T12:48:49.367

Answers

8

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.

gronostaj

Posted 2013-05-20T11:19:11.440

Reputation: 33 047

Just found out about this tool on my own :). For the reference, here's the command line that outputs duration in millisesconds: MediaInfo.exe --Output=Video;%Duration% file.avi BTW Running MediaInfo.exe --info-parameters to see what it can output is a bit overwhelming :) – Cristi Diaconescu – 2013-05-20T11:45:45.823

2

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

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

phuclv

Posted 2013-05-20T11:19:11.440

Reputation: 14 930