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,885

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.