Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active January 14, 2025 15:19
Show Gist options
  • Save mark05e/1334db59feca9c0904da08adcff35e8e to your computer and use it in GitHub Desktop.
Save mark05e/1334db59feca9c0904da08adcff35e8e to your computer and use it in GitHub Desktop.
Audio Conversion script for low-bandwidth, telephony-related audio conversions

Audio Conversion v2 Powershell Script

This PowerShell script automates audio file conversion using FFmpeg and provides functionalities for managing output locations and overwriting existing files.

Features:

  • Converts audio files to a specified format using FFmpeg.
  • Creates a subfolder named audioconversion_timestamp in the directory containing the input audio file(s).
  • Converts each input audio file within the newly created subfolder.
  • Allows customization of audio parameters (sampling rate, channels, bitrate, codec) through the Execute-FFmpeg function (assumed to be defined in the same folder)
  • Provides the option to overwrite existing output files using the -Overwrite parameter in the Execute-FFmpeg function (default: $false).

Requirements:

  • PowerShell
  • FFmpeg (installed and accessible on the system)

How to Use:

  1. Save the script as Run-AudioConversion2.ps1, Execute-FFmpeg.ps1 and Run-AudioConversion2.cmd.

  2. Modify the Execute-FFmpeg function (in a separate file) to define the desired audio conversion parameters and FFmpeg command structure.

  3. Open a PowerShell window and navigate to the directory containing the script.

  4. Run the script, specifying the paths to your audio files as arguments:

    .\Run-AudioConversion2.ps1 "C:\path\to\audio1.mp3" "C:\path\to\audio2.wav" "C:\path\to\audio3.flac"

Notes:

  • This script assumes the Execute-FFmpeg function is defined in the same folder.
  • Modify the Execute-FFmpeg function to match your specific audio conversion needs and FFmpeg command structure.
  • The script will overwrite existing output files by default. Use the -Overwrite:$false parameter in the Run-AudioConversion2.ps1 script to prevent overwriting.

Default FFMPEG Configuration:

The Execute-FFmpeg function has the following default settings:

  • Audio Rate: 8000 Hz
  • Audio Channels: 1 (Mono)
  • Audio Bitrate: 8 kbps
  • Audio Codec: "pcm_mulaw"

This configuration is suitable for low-bandwidth, telephony-related audio conversions. You can adjust these settings by passing different values for AudioRate, AudioChannels, AudioBitrate, and AudioCodec to the Execute-FFmpeg function.

Additional Considerations:

  • Implement error handling for more robust operation.
  • Consider adding features like user input for audio parameters or allowing users to select the output folder.
<#
.SYNOPSIS
Executes the FFmpeg command to convert an input file to a specified audio format.
.DESCRIPTION
This function converts a given input file to the specified audio format using the FFmpeg command-line tool.
It allows customization of audio parameters such as sampling rate, channels, bitrate, and codec.
.PARAMETER InputFile
The path to the input file.
.PARAMETER OutputFile
The path to the desired output file.
.PARAMETER AudioRate
The desired audio sampling rate in Hz. Defaults to 8000 Hz.
.PARAMETER AudioChannels
The number of audio channels. Defaults to 1 (mono).
.PARAMETER AudioBitrate
The desired audio bitrate in kbps. Defaults to 8 kbps.
.PARAMETER AudioCodec
The desired audio codec. Defaults to "pcm_mulaw".
.PARAMETER Overwrite
Specifies whether to overwrite the output file if it already exists.
Defaults to $false.
.EXAMPLE
Execute-FFmpeg -InputFile "input.mp4" -OutputFile "output.wav"
Execute-FFmpeg -InputFile "myvideo.avi" -OutputFile "audio.mp3" -AudioCodec "libmp3lame" -Overwrite:$false
.NOTES
- Requires FFmpeg to be installed on the system.
- The function outputs the FFmpeg command to the console before execution.
#>
function Execute-FFmpeg {
param (
[string]$InputFile,
[string]$OutputFile,
[int]$AudioRate = 8000,
[int]$AudioChannels = 1,
[int]$AudioBitrate = 8,
[string]$AudioCodec = "pcm_mulaw",
[switch]$Overwrite = $false
)
# Check if the output file already exists
if (-not $Overwrite -and (Test-Path $OutputFile)) {
Write-Warning "Output file already exists and Overwrite is set to $false. Skipping."
return
}
# Build the FFmpeg command
$FFmpegCommand = "ffmpeg -y -i `"$InputFile`" -ar $AudioRate -ac $AudioChannels -ab $AudioBitrate -acodec $AudioCodec `"$OutputFile`""
# Execute the FFmpeg command
Write-Host $FFmpegCommand
Invoke-Expression $FFmpegCommand
}
@echo off
setlocal enabledelayedexpansion
:: Get the name of the batch file without the extension
set "batchFileName=%~n0"
:: Build the PowerShell script file name by appending ".ps1" extension
set "psScriptFileName=%batchFileName%.ps1"
:: Check if the corresponding PowerShell script file exists
if not exist "%psScriptFileName%" (
echo The PowerShell script "%psScriptFileName%" does not exist.
timeout 10
exit /b 1
)
:: Build the PowerShell command with passed arguments
set "psCommand=powershell -ExecutionPolicy Bypass -File "%psScriptFileName%""
:: Add all the batch file arguments to the PowerShell command
:process_args
if "%~1"=="" (
goto execute_ps_command
) else (
set "psCommand=!psCommand! %1"
shift
goto process_args
)
:execute_ps_command
:: Execute the PowerShell command
%psCommand%
:: Exit the batch script
exit /b 0
<#
.SYNOPSIS
Creates a subfolder named "audioconversion_{timestamp}" in the directory
containing the input audio files and converts each audio file to the specified output format
within the newly created subfolder.
.DESCRIPTION
This script takes one or more audio file paths as input.
It creates a single subfolder named "audioconversion_{timestamp}" within the
directory containing the first input file.
Then, it iterates through each input file,
executes the `Execute-FFmpeg` function (assumed to be defined elsewhere)
to convert each audio file to the specified output format,
and saves the converted files within the newly created subfolder.
.PARAMETER FilePath
One or more paths to the input audio files.
.EXAMPLE
.\Convert-Audio.ps1 "C:\path\to\audio1.mp3" "C:\path\to\audio2.mp3" "C:\path\to\audio3.wav"
#>
. .\Execute-FFmpeg.ps1
function Convert-Audio {
param(
[string[]]$FilePath
)
# Get the directory of the first input file
$SourceDirectory = Split-Path -Parent $FilePath[0]
# Create the timestamp for the subfolder name
$Timestamp = (Get-Date).ToString("yyyyMMdd_hhmmtt")
# Create the subfolder for converted files
$SubfolderName = "audioconversion_$Timestamp"
$DestinationDirectory = Join-Path -Path $SourceDirectory -ChildPath $SubfolderName
New-Item -ItemType Directory -Path $DestinationDirectory -Force
# Loop through each input file
foreach ($file in $FilePath) {
# Construct the output file path
$FileName = Split-Path -Leaf $file
$OutputFilePath = Join-Path -Path $DestinationDirectory -ChildPath $FileName
# Execute the FFmpeg conversion (assuming Execute-FFmpeg function exists)
Execute-FFmpeg -InputFile $file -OutputFile $OutputFilePath -Overwrite:$true
Write-Host "Audio file converted successfully to: $OutputFilePath"
}
}
# Usage:
Convert-Audio $args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment