Skip to content

Instantly share code, notes, and snippets.

@MarkKoz
Created May 25, 2017 00:43
Show Gist options
  • Save MarkKoz/e9a6b4f30c3f230f96f525b631ee8d94 to your computer and use it in GitHub Desktop.
Save MarkKoz/e9a6b4f30c3f230f96f525b631ee8d94 to your computer and use it in GitHub Desktop.
A PS script which uses ffmpeg to convert all audio files of a specified extension in a directory to another codec.
class Files {
hidden [String] $iExt
hidden [String] $oCodec
hidden [String] $oExt
hidden [String] $iDir
hidden [String] $oDir
hidden [System.Object] $files
Files ([String] $iExt, [String] $oCodec, [String] $oExt) {
$this.iExt = $iExt
$this.oCodec = $oCodec
$this.oExt = $oExt
$this.iDir = $PSScriptRoot
$this.oDir = "$($this.iDir)\$oCodec"
$this.files = Get-ChildItem -Path "$($this.iDir)\*" -Include "*.$($this.iExt)"
}
Files ([String] $iExt, [String] $oCodec, [String] $oExt, [String] $iDir) {
$this.iExt = $iExt
$this.oCodec = $oCodec
$this.oExt = $oExt
$this.iDir = $iDir
$this.oDir = "$($this.iDir)\$oCodec"
$this.files = Get-ChildItem -Path "$($this.iDir)\*" -Include "*.$($this.iExt)"
}
Files ([String] $iExt, [String] $oCodec, [String] $oExt, [String] $iDir, [String] $oDir) {
$this.iExt = $iExt
$this.oCodec = $oCodec
$this.oExt = $oExt
$this.iDir = $iDir
$this.oDir = $oDir
$this.files = Get-ChildItem -Path "$($this.iDir)\*" -Include "*.$($this.iExt)"
}
[String] GetInExt() {
return $this.iExt
}
[String] GetOutCodec() {
return $this.oCodec
}
[String] GetOutExt() {
return $this.oExt
}
[String] GetInDir() {
return $this.iDir
}
[String] GetOutDir() {
return $this.oDir
}
[System.Object] Get() {
return $this.files
}
[Void] Convert() {
New-Item -ItemType Directory -Force -Path $this.oDir | Out-Null
foreach ($file in $this.files) {
Write-Host "`nConverting file " -NoNewline
Write-Host ($file.Name) -NoNewline -ForegroundColor Cyan
Write-Host " to $($this.oCodec)."
ffmpeg -hide_banner -loglevel warning -i $file.FullName -c:a $this.oCodec "$($this.oDir)\$($file.BaseName).$($this.oExt)"
}
}
}
function Prompt-Properties([Files] [ref] $files){
$title = "Audio Conversion"
$message = "`nDo you want to perform a conversion with the following settings?"
$message += "`n Input directory path: $($files.GetInDir())"
$message += "`n Input file extension: $($files.GetInExt())"
$message += "`n Output codec: $($files.GetOutCodec())"
$message += "`n Output directory path: $($files.GetOutDir())"
$message += "`n Output file extension: $($files.GetOutExt())"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Converts all files with file extension $($files.GetInExt()) in directory $($files.GetInDir()) to file extension $($files.GetOutExt()) and audio codec $($files.GetOutCodec()) and outputs them to directory $($files.GetOutDir())."
$set = New-Object System.Management.Automation.Host.ChoiceDescription "&Set", `
"Prompts for each setting to be set (input directory path, input file extension, output codec, output directory path, and output file extension)."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Does not convert any files and exits the script."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $set, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result) {
0 {
return $files
}
1 {
Write-Host "`nIf a value is not set, the default will be used."
$iDir = Read-Host -Prompt "`nInput directory path"
$iExt = Read-Host -Prompt "Input file extension"
$oCodec = Read-Host -Prompt "Output audio codec"
$oDir = Read-Host -Prompt "Output directory path"
$oExt = Read-Host -Prompt "Output file extension"
if ($iExt -eq '') {
$iExt = $files.GetInExt()
}
if ($oCodec -eq '') {
$oCodec = $files.GetOutCodec()
}
if ($oExt -eq '') {
$oExt = $files.GetOutExt()
}
if ($iDir -eq '' -and !($oDir -eq '')) {
return [Files]::new($iExt, $oCodec, $oExt, $files.GetInDir(), $oDir)
}
if (!($iDir -eq '') -and $oDir -eq '') {
return [Files]::new($iExt, $oCodec, $oExt, $iDir)
}
if (!($iDir -eq '') -and !($oDir -eq '')) {
return [Files]::new($iExt, $oCodec, $oExt, $iDir, $oDir)
}
return [Files]::new($iExt, $oCodec, $oExt)
}
2 {
exit
}
}
}
function Prompt-Convert([Files] [ref] $files) {
$title = "Found $($files.Get().Count) $($files.GetInExt()) files eligible for conversion:"
foreach ($file in $files.Get()) {
$title += "`n $($file.Name)"
}
$message = "`nDo you want to convert the files above to $($files.GetOutCodec())?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Converts the files listed above to file extension $($files.GetOutExt()) and audio codec $($files.GetOutCodec())."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Retains the current audio codec and file extension for each file listed above."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
0 {
$files.Convert()
Read-Host -Prompt "`nFinished converting $($files.Get().Count) files. Press [Enter] to exit"
}
1 {
Read-Host -Prompt "`nNo files were converted. Press [Enter] to exit"
}
}
}
[Files] $filesDefault = [Files]::new('wav', 'flac', 'flac')
[Files] $filesConvert = Prompt-Properties ([ref] $filesDefault)
Prompt-Convert ([ref] $filesConvert)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment