PowerShell/Scripts/play-m3u.ps1
2021-04-21 10:33:38 +02:00

41 lines
1.1 KiB
PowerShell
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/pwsh
<#
.SYNTAX play-m3u.ps1 [<playlist-file>]
.DESCRIPTION plays the given playlist (in .M3U file format)
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param($Filename = "")
if ($Filename -eq "" ) { $Filename = read-host "Enter the M3U playlist filename" }
try {
$Lines = get-content $Filename
add-type -assemblyName presentationCore
$MediaPlayer = new-object system.windows.media.mediaplayer
for ([int]$i=0; $i -lt $Lines.Count; $i++) {
$Line = $Lines[$i]
if ($Line[0] -ne "#") {
$FullPath = (get-childItem "$Line").fullname
do {
$MediaPlayer.open($FullPath)
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
} until ($Milliseconds)
[int]$Minutes = $Milliseconds / 60000
[int]$Seconds = ($Milliseconds / 1000) % 60
"Playing 🎵$Line ($($Minutes):$Seconds) ..."
$MediaPlayer.Volume = 1
$MediaPlayer.play()
start-sleep -milliseconds $Milliseconds
$MediaPlayer.stop()
$MediaPlayer.close()
}
}
exit 0
} catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}