1

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}
Vinayak
  • 10,625
  • 10
  • 54
  • 89
  • 1
    So run `ffmpeg -i $path -f caf - | fdkaac -p5 -b64 - -o $outfolder\$outname.m4a` using static files, paths, etc. rather than the dynamic variables from PS to see if the results are fine in case it's the dynamic logic that only causes this at least this would be an easy way to confirm it you've not already done this. – Vomit IT - Chunky Mess Style Aug 18 '16 at 04:51
  • @PIMP_JUICE_IT Thanks! That helped. I ran the command through Powershell and found out that it didn't really pipe the output of FFMPEG like I hoped it would. Here's the output of the same command with [CMD](http://pastebin.com/trdCH6EU) and [Powershell](http://pastebin.com/X4B1ZcP7). `fdkaac` bugs out with a `ERROR: unsupported sample rate` on Powershell. I had Task Manager open and I noticed that when I ran the command on CMD, both `ffmpeg.exe` and `fdkaac.exe` were being executed simultaneously while Powershell seemed to be running the programs sequentially. – Vinayak Aug 18 '16 at 10:50
  • I googled it and it seems this is [default Powershell behavior](http://stackoverflow.com/a/15438168/1768141) so I changed `$command` to `"ffmpeg -i \`"$path\`" -f caf - | fdkaac -p5 -b64 - -o \`"$outfolder\$name.m4a\`""` and called it with `cmd /c $command` to let CMD run the command instead and it works fine now. – Vinayak Aug 18 '16 at 10:54
  • @PIMP_JUICE_IT Feel free to add your answer and I'll accept it. I may edit it to include the bits from the StackOverflow answer that I found helpful. – Vinayak Aug 18 '16 at 19:59

1 Answers1

1

Why does this FFMPEG command when called from Powershell generate white noise?

Troubleshooting More

  • Try testing with one file from an elevated PowerShell that's run as administrator, and then pipe that over to the other command.
    • Check this one file when run from PowerShell to confirm that running it with PowerShell against just one file if it has the same effect.

It could be the other logic or how it works is causing it to output this way so you may need to break the logic it apart, test, etc. to determine at what level there's a problem with the logic.


Start With

I would start by running the below:

  • ffmpeg -i $path -f caf - | fdkaac -p5 -b64 - -o $outfolder\$outname.m4a

but use static files, paths, etc. rather than having the the dynamic variables from PS fill in those parts of the commands to see if the results are fine this way. If you've not already done so, this may be an easy way to confirm that it's the dynamic logic that only causes the issue.

Edit by OP

After troubleshooting with static paths, it turns out that Powershell didn't pipe the output of FFMPEG like I hoped it would.

Observing new processes in Task Manager, it looked like Powershell was executing each program sequentially while running the same command in a command prompt showed that fdkaac.exe and ffmpeg.exe were both running simultaneously.

While googling the behavior, I found this answer which suggests that redirection works differently in Powershell, so I changed & $command to cmd /c $command to let CMD handle the execution of the command instead of Powershell which solved the problem.

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117