]> git.ardis.lu - ArdisUtilities/commitdiff
Add Get-SizeDurationRatio
authorArdis Lu <ardisglu@gmail.com>
Sat, 25 Dec 2021 04:10:16 +0000 (20:10 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sat, 25 Dec 2021 04:10:16 +0000 (20:10 -0800)
ArdisUtilities/ArdisUtilities.psd1
ArdisUtilities/ArdisUtilities.psm1

index 1db513b97aeb2434c26f05ab4f5ef84c3bd4f0db..e2573c4f20aa7d6ce90f64a91fc6bdb59ca58ce9 100644 (file)
@@ -4,7 +4,7 @@
   GUID              = '57a6bea3-0ea5-44b3-95d2-71d122772f1c'
   Author            = 'Ardis Lu'
   Description       = 'My personal utility functions.'
-  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash')
+  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio')
   CmdletsToExport   = @()
   VariablesToExport = @()
   AliasesToExport   = @()
index 099faf1e752d875949959e6ee212a271e9aab774..a270a5e76b94f3e912970033ee4aa167b1989f7c 100644 (file)
@@ -194,3 +194,109 @@ function Get-StringHash {
     }
   }
 }
+
+class SizeDurationRatio {
+  [String]$Path
+  [String]$Name
+  [Double]$Size
+  [Double]$Duration
+  [Double]$Ratio
+}
+
+$SizeDurationRatioDefaultDisplay = @{
+  TypeName                  = 'SizeDurationRatio'
+  DefaultDisplayPropertySet = 'Name', 'Size', 'Duration', 'Ratio'
+}
+Update-TypeData @SizeDurationRatioDefaultDisplay
+
+function Get-SizeDurationRatio {
+  <#
+  .SYNOPSIS
+    Calculates a file's size (in KB) divided by its duration (in seconds).
+
+  .DESCRIPTION
+    Creates a new Shell Folder object from the file's parent folder path. Then creates a FolderItem
+    object for the file. Finally, uses the FolderItem's extended properties to fetch the duration
+    (if it exists) and calculate the ratio.
+
+  .PARAMETER Path
+    The path to the file to calculate the size/duration ratio for.
+
+  .EXAMPLE
+    PS> Get-SizeDurationRatio ./example-video.mp4 
+
+    Name                          Size Duration            Ratio
+    ----                          ---- --------            -----
+    example-video.mp4 195746.022460938      736 265.959269648013
+
+  .EXAMPLE
+    PS> @('./example-audio.mp3', './example-video.mp4') | Get-SizeDurationRatio
+
+    Name                          Size Duration            Ratio
+    ----                          ---- --------            -----
+    example-audio.mp3     5165.4140625      132 39.1319247159091
+    example-video.mp4 195746.022460938      736 265.959269648013
+
+  .EXAMPLE
+    PS> Get-SizeDurationRatio
+
+    cmdlet Get-SizeDurationRatio at command pipeline position 1
+    Supply values for the following parameters:
+    Path: ./example-video.mp4
+
+    Name                          Size Duration            Ratio
+    ----                          ---- --------            -----
+    example-video.mp4 195746.022460938      736 265.959269648013
+
+  .EXAMPLE
+    PS> Get-SizeDurationRatio ./example-document.txt
+
+    Get-SizeDurationRatio: example-document.txt does not have a duration.
+
+  .EXAMPLE
+    PS> @('./example-audio.mp3', './example-video.mp4', './example-document.txt') | Get-SizeDurationRatio
+
+    Name                          Size Duration            Ratio
+    ----                          ---- --------            -----
+    example-audio.mp3     5165.4140625      132 39.1319247159091
+    example-video.mp4 195746.022460938      736 265.959269648013
+    Get-SizeDurationRatio: example-document.txt does not have a duration.
+  #>
+
+  [OutputType([SizeDurationRatio])]
+  [CmdletBinding()]
+  param(
+    [Parameter(Mandatory, ValueFromPipeline)]
+    [ValidateScript({ Test-Path -LiteralPath $_ }, ErrorMessage = "'{0}' is not a valid path.")]
+    [String]$Path
+  )
+
+  process {
+    $resolvedPath = Resolve-Path $Path
+    $folderPath = Split-Path $resolvedPath
+    $fileName = Split-Path $resolvedPath -Leaf
+
+    $folderObject = (New-Object -ComObject Shell.Application).NameSpace($folderPath)
+    $fileObject = $folderObject.ParseName($fileName)
+
+    $durationString = $folderObject.GetDetailsOf($fileObject, 27)
+
+    # If the file does not have a duration, GetDetailsOf returns an empty string
+    if ('' -eq $durationString) {
+      $PSCmdlet.WriteError((New-Object System.Management.Automation.ErrorRecord "$fileName does not have a duration.", $null, 'InvalidData', $null))
+    }
+    else {
+      $size = $fileObject.Size / 1KB
+      $duration = [System.TimeSpan]::Parse($durationString).TotalSeconds
+      $ratio = $size / $duration
+  
+      [SizeDurationRatio] @{
+        'Path'     = $resolvedPath
+        'Name'     = $fileName
+        'Size'     = $size
+        'Duration' = $duration
+        'Ratio'    = $ratio
+      }
+    }
+  }
+}
This page took 0.037694 seconds and 4 git commands to generate.