I'm trying to create a Powershell script that watches a folder for new MP3 files and converts them to M4A format using FFMPEG and FDK AAC encoder.
Since fdkaac.exe won't take MP3 files as input, I use this command, which pipes from ffmpeg:
ffmpeg -i $path -f caf - | fdkaac -p5 -b64 - -o $outfolder\$outname.m4a
This works just fine if I enter it into a command prompt and the resulting M4A file that's generated has no problems.
However if I run this from Powershell, the M4A file is created but contains nothing but unpleasant white noise (and very faint traces of music).
An ideas why that is so?
Here's the Powershell script if it helps:
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "E:\test"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$watcher.NotifyFilter = [IO.NotifyFilters]'FileName, Size'
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$filename = $Event.SourceEventArgs.Name
$outname = [io.path]::GetFileNameWithoutExtension($path)
$outfolder = "C:\Users\Public\Music\"
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
Add-content "E:\log.txt" -value $logline
If ($path -match '.mp3|.m4a') {
$command = "ffmpeg -i `"$path`" -f caf - | fdkaac -p5 -b64 - -o `"$outfolder\$outname.m4a`""
Add-content "E:\log.txt" -value "[Transcode] $filename"
& command
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$created = Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}