7

I am trying to list some of my mp3 files with Powershell.

I have tried

ls C:\test | Format-List -Property Name,Length

which gives me

Name   : testfile.mp3
Length : 10058533

The length is the size of the file which I don't care for at this moment.

Knowing that I can choose duration in File Explorer for a column heading I thought to re-write the above as

ls C:\test | Format-List -Property Name,Duration

but what I get is

Name   : testfile.mp3

How can I tell Powershell to return the minutes and seconds for the duration? Or even just the seconds. Thanks in advance

2 Answers 2

11

To get the length of an mp3, probably the easiest way is to make use of what explorer uses, a Shell objekt.

I'm afraid I don't have a one-liner for you, but you can always make a script that you call.

$path = 'M:\Musikk\awesome_song.mp3'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)

write-host $shellfolder.GetDetailsOf($shellfile, 27); 

More information can be found here: http://powershell.com/cs/blogs/tobias/archive/2011/01/07/organizing-videos-and-music.aspx

Edit: The reason this method is necessary is because the length information resides with the other metadata of the file.

1
  • I wasn't asking for one-liner so no deduction there :) And yes this does give me what I am asking for now I need to loop it for multiples files :) Thanks
    – Darius
    Commented Jan 20, 2014 at 23:57
0

This script can be executed in PowerShell ISE to traverse all MP3-files in a folder:

    $folder = "M:\Musikk\"
    $output_file = "M:\Musikk\output.txt"

    Get-ChildItem -Path $folder -Filter *.mp3 | ForEach-Object {
        $file_path = $_.FullName
        
        # This does not work, it gets the length of the MP3-file in bytes.
        # note: Personally I whould expect this property to be called 'Size'.
        #$test_duration01 = $_.Length
        #Write-Host $test_duration01
        
        # This does not work, it shows an empty line
        #$test_duration02 = $_.Duration
        #Write-Host $test_duration02
        
        $shell = New-Object -COMObject Shell.Application
        
        $folder = Split-Path $file_path
        $file = Split-Path $file_path -Leaf
        $shellfolder = $shell.Namespace($folder)
        $shellfile = $shellfolder.ParseName($file)
        # Get the details for this file. We want to display the property 'Length' (=27).
        $duration = $shellfolder.GetDetailsOf($shellfile, 27);
        #Write-Host $duration
        $duration_obj = [TimeSpan]::ParseExact($duration, "hh\:mm\:ss", [System.Globalization.CultureInfo]::InvariantCulture)
        
        Add-Content -Path $output_file -Value "$file_path,$($duration_obj.TotalSeconds)"    
    }
        
    # Further reading/sources:
    #    https://stackoverflow.com/questions/22382010/what-options-are-available-for-shell32-folder-getdetailsof
    #    https://github.com/matekstatek/get-mp3-duration-in-powershell/blob/main/Get-MP3DurationFromSpecificDirectory.ps1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .