microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
.github/skills/experimental/powerpoint/scripts/Invoke-EmbedAudio.ps1
162lines · modecode
| 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | #Requires -Version 7.0 |
| 5 | |
| 6 | <# |
| 7 | .SYNOPSIS |
| 8 | Embed WAV audio files into a PowerPoint deck. |
| 9 | |
| 10 | .DESCRIPTION |
| 11 | Wrapper script that manages the Python virtual environment and invokes |
| 12 | embed_audio.py to embed per-slide WAV files into a PPTX presentation. |
| 13 | |
| 14 | .PARAMETER InputPath |
| 15 | Input PPTX file path. |
| 16 | |
| 17 | .PARAMETER AudioDir |
| 18 | Directory containing slide-NNN.wav files. |
| 19 | |
| 20 | .PARAMETER OutputPath |
| 21 | Output PPTX file path. |
| 22 | |
| 23 | .PARAMETER Slides |
| 24 | Comma-separated slide numbers to embed audio on (optional). |
| 25 | |
| 26 | .PARAMETER SkipVenvSetup |
| 27 | Skip virtual environment setup. |
| 28 | |
| 29 | .EXAMPLE |
| 30 | ./Invoke-EmbedAudio.ps1 -InputPath deck.pptx -AudioDir voice-over/ -OutputPath out.pptx |
| 31 | |
| 32 | .NOTES |
| 33 | Part of the powerpoint skill. Manages uv virtual environment setup |
| 34 | and delegates to embed_audio.py for WAV embedding into PPTX slides. |
| 35 | #> |
| 36 | |
| 37 | [CmdletBinding()] |
| 38 | param( |
| 39 | [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$InputPath, |
| 40 | [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$AudioDir, |
| 41 | [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$OutputPath, |
| 42 | [Parameter(Mandatory = $false)][string]$Slides, |
| 43 | [Parameter(Mandatory = $false)][switch]$SkipVenvSetup |
| 44 | ) |
| 45 | |
| 46 | $ErrorActionPreference = 'Stop' |
| 47 | |
| 48 | #region Environment Setup |
| 49 | |
| 50 | $ScriptDir = $PSScriptRoot |
| 51 | $SkillRoot = Split-Path -Parent $ScriptDir |
| 52 | $VenvDir = Join-Path $SkillRoot '.venv' |
| 53 | |
| 54 | #endregion Environment Setup |
| 55 | |
| 56 | #region Environment Functions |
| 57 | |
| 58 | function Test-UvAvailability { |
| 59 | <# |
| 60 | .SYNOPSIS |
| 61 | Verifies uv is available on PATH. |
| 62 | .OUTPUTS |
| 63 | [string] The resolved uv command path. |
| 64 | #> |
| 65 | [CmdletBinding()] |
| 66 | [OutputType([string])] |
| 67 | param() |
| 68 | |
| 69 | $resolved = Get-Command 'uv' -ErrorAction SilentlyContinue |
| 70 | if ($resolved) { |
| 71 | return $resolved.Source |
| 72 | } |
| 73 | throw 'uv is required but was not found on PATH. Install with: curl -LsSf https://astral.sh/uv/install.sh | sh' |
| 74 | } |
| 75 | |
| 76 | function Initialize-PythonEnvironment { |
| 77 | <# |
| 78 | .SYNOPSIS |
| 79 | Syncs the Python virtual environment and dependencies via uv. |
| 80 | #> |
| 81 | [CmdletBinding()] |
| 82 | [OutputType([void])] |
| 83 | param() |
| 84 | |
| 85 | & uv sync --directory $SkillRoot |
| 86 | if ($LASTEXITCODE -ne 0) { |
| 87 | throw 'Failed to sync Python environment via uv.' |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function Get-VenvPythonPath { |
| 92 | <# |
| 93 | .SYNOPSIS |
| 94 | Returns the path to the venv Python executable. |
| 95 | .OUTPUTS |
| 96 | [string] Absolute path to the venv python binary. |
| 97 | #> |
| 98 | [CmdletBinding()] |
| 99 | [OutputType([string])] |
| 100 | param() |
| 101 | |
| 102 | if ($IsWindows) { |
| 103 | return Join-Path $VenvDir 'Scripts/python.exe' |
| 104 | } |
| 105 | return Join-Path $VenvDir 'bin/python' |
| 106 | } |
| 107 | |
| 108 | #endregion Environment Functions |
| 109 | |
| 110 | #region Script Execution |
| 111 | |
| 112 | function Invoke-EmbedAudio { |
| 113 | <# |
| 114 | .SYNOPSIS |
| 115 | Runs embed_audio.py with the provided parameters. |
| 116 | #> |
| 117 | [CmdletBinding()] |
| 118 | [OutputType([void])] |
| 119 | param() |
| 120 | |
| 121 | $python = Get-VenvPythonPath |
| 122 | $script = Join-Path $ScriptDir 'embed_audio.py' |
| 123 | |
| 124 | $arguments = @( |
| 125 | $script, |
| 126 | '--input', $InputPath, |
| 127 | '--audio-dir', $AudioDir, |
| 128 | '--output', $OutputPath |
| 129 | ) |
| 130 | if ($Slides) { |
| 131 | $arguments += '--slides' |
| 132 | $arguments += $Slides |
| 133 | } |
| 134 | if ($VerbosePreference -eq 'Continue') { |
| 135 | $arguments += '-v' |
| 136 | } |
| 137 | |
| 138 | & $python @arguments |
| 139 | if ($LASTEXITCODE -ne 0) { |
| 140 | throw "embed_audio.py failed with exit code $LASTEXITCODE." |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #endregion Script Execution |
| 145 | |
| 146 | #region Main |
| 147 | |
| 148 | if ($MyInvocation.InvocationName -ne '.') { |
| 149 | try { |
| 150 | if (-not $SkipVenvSetup) { |
| 151 | Test-UvAvailability | Out-Null |
| 152 | Initialize-PythonEnvironment |
| 153 | } |
| 154 | Invoke-EmbedAudio |
| 155 | } |
| 156 | catch { |
| 157 | Write-Error -ErrorAction Continue "Invoke-EmbedAudio failed: $($_.Exception.Message)" |
| 158 | exit 1 |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #endregion Main |
| 163 | |