Updated play-m3u.ps1 and play-mp3.ps1

This commit is contained in:
Markus Fleschutz 2024-11-21 08:07:03 +01:00
parent c54e4b0184
commit de784e4cdc
2 changed files with 19 additions and 27 deletions

View File

@ -1,12 +1,14 @@
<# <#
.SYNOPSIS .SYNOPSIS
Plays a playlist (.M3U format) Plays a .M3U playlist
.DESCRIPTION .DESCRIPTION
This PowerShell script plays the given playlist (in .M3U file format) This PowerShell script plays the given playlist (in .M3U file format)
.PARAMETER filename .PARAMETER filename
Specifies the path to the playlist Specifies the path to the playlist
.EXAMPLE .EXAMPLE
PS> ./play-m3u C:\MyPlaylist.m3u PS> ./play-m3u.ps1 C:\MyPlaylist.m3u
Playing '01 Sandy beach - strong waves.mp3' (02:54) ...
...
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -16,32 +18,20 @@
param([string]$filename = "") param([string]$filename = "")
try { try {
if ($filename -eq "" ) { $filename = read-host "Enter the M3U playlist filename" } if ($filename -eq "" ) { $filename = Read-Host "Enter the path to the .M3U playlist file" }
if (-not(test-path "$filename" -pathType leaf)) { throw "Can't access playlist file: $filename" } if (-not(Test-Path "$filename" -pathType leaf)) { throw "Can't access playlist file: $filename" }
$Lines = get-content $filename $lines = Get-Content $filename
add-type -assemblyName presentationCore Add-Type -assemblyName presentationCore
$MediaPlayer = new-object system.windows.media.mediaplayer $MediaPlayer = New-Object system.windows.media.mediaplayer
for ([int]$i=0; $i -lt $Lines.Count; $i++) { foreach ($line in $lines) {
$Line = $Lines[$i] if ($line[0] -eq "#") { continue }
if ($Line[0] -eq "#") { continue } if (-not(Test-Path "$line" -pathType leaf)) { throw "Can't access audio file: $line" }
if (-not(test-path "$Line" -pathType leaf)) { throw "Can't access audio file: $Line" } $fullPath = (Get-ChildItem "$line").fullname
$FullPath = (get-childItem "$Line").fullname
$filename = (get-item "$FullPath").name & "$PSScriptRoot/play-mp3.ps1" $fullPath
do {
$MediaPlayer.open("$FullPath")
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
} until ($Milliseconds)
[int]$Minutes = $Milliseconds / 60000
[int]$Seconds = ($Milliseconds / 1000) % 60
"Playing 🎵$filename ($($Minutes.ToString('00')):$($Seconds.ToString('00'))) ..."
$MediaPlayer.Volume = 1
$MediaPlayer.play()
start-sleep -milliseconds $Milliseconds
$MediaPlayer.stop()
$MediaPlayer.close()
} }
exit 0 # success exit 0 # success
} catch { } catch {

View File

@ -7,6 +7,7 @@
Specifies the file path to the .MP3 file Specifies the file path to the .MP3 file
.EXAMPLE .EXAMPLE
PS> ./play-mp3.ps1 C:\thunder.mp3 PS> ./play-mp3.ps1 C:\thunder.mp3
Playing 'thunder.mp3' (00:03) ...
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -33,16 +34,17 @@ try {
[int]$minutes = $milliseconds / 60000 [int]$minutes = $milliseconds / 60000
[int]$seconds = ($milliseconds / 1000) % 60 [int]$seconds = ($milliseconds / 1000) % 60
Write-Host " ▶️" -noNewline -foregroundColor green Write-Host " ▶️" -noNewline -foregroundColor green
Write-Host "Playing $filename for $($minutes.ToString('00')):$($seconds.ToString('00'))s..." Write-Host "Playing '$filename' ($($minutes.ToString('00')):$($seconds.ToString('00'))) ..."
$previousTitle = $host.ui.RawUI.WindowTitle $previousTitle = $host.ui.RawUI.WindowTitle
$host.ui.RawUI.WindowTitle = "▶️ $filename" $host.ui.RawUI.WindowTitle = "▶️ $filename"
$mediaPlayer.Volume = 1 $mediaPlayer.Volume = 1
$mediaPlayer.play() $mediaPlayer.play()
Start-Sleep -milliseconds $milliseconds Start-Sleep -milliseconds $milliseconds
$mediaPlayer.stop() $mediaPlayer.stop()
$mediaPlayer.close() $mediaPlayer.close()
$host.ui.RawUI.WindowTitle = $previousTitle $host.ui.RawUI.WindowTitle = $previousTitle
exit 0 # success exit 0 # success
} catch { } catch {