How do I get video file duration in powershell script

Here's a partial code snip. I'd like to test the duration of the original .avi to the newly created .mp4 and

display a warning if they are not within a second of each other:

The duration shows as "length" in Windows explorer

----------------------------------------------------

$movies = ls -recurse $inputpath\*.avi

$starttime = get-date

foreach($movie in $movies){

 $name = $movie.fullname
 $basename=$movie.basename

 $outname=$name.Replace(".avi",".mp4")

 #  skip if mp4 already exists   (Literalpath needed as square brackets in the filename causes problems
 if(!(test-path -literalpath "$outname")){
  write-output "$outname  Started at $now"
  handbrakecli -i "$name" -o "$outname"  --preset="Normal"   2>&1>> D:\'$Data'\convmp4.log
  $now = get-date
  write-output "$outname Done at $now">>d:\'$Data'\convmp4Done.log

  $now=get-date
  $elapsedrun=$now-$starttime
  if ($elapsedrun.Totalminutes -ge $runlimit){
    write-host "Time limit reached of $runlimit minutes at $now"
    write-output "Time limit reached of $runlimit minutes at $now">>d:\'$Data'\convmp4Done.log
      exit
      }

  }
 else{
 write-output "Skipping (already exists):  $outname $length"
 }

}

    
  • Edited by Mar778 19 hours 42 minutes ago
May 25th, 2015 7:29am

Here is an example of how to get the length of a video file:

$Folder = 'C:\Path\To\Parent\Folder'
$File = 'Video.mp4'
$LengthColumn = 27
$objShell = New-Object -ComObject Shell.Application 
$objFolder = $objShell.Namespace($Folder)
$objFile = $objFolder.ParseName($File)
$Length = $objFolder.GetDetailsOf($objFile, $LengthColumn)

  • Marked as answer by Mar778 17 hours 32 minutes ago
Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 8:40am

Thanks, that works!
May 25th, 2015 9:40am

You're welcome.

You have probably figured this out on your own, but you can test for difference in duration and display warning like so:

$Difference = ([timespan]$Length1).Subtract([timespan]$Length2).Duration().TotalSeconds
if ($Difference -gt 1) {
    Write-Warning "Difference in seconds is $Difference"
}


Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 1:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics