Forum Discussion

Reddwarf4ever's avatar
Reddwarf4ever
Copper Contributor
Mar 13, 2022

Converting video with DTS to AC3 using FFMpeg & PowerShell

I have a number of video files with DTS audio, I need to convert to AC3 Audio, without reprocessing the video. I have been given 2 scripts, the first one A-worked perfectly yesterday but today is throwing up errors, cant see why. the second script allows the source and target directories to be different, but that hasn't worked at all.

Have the video in 7 folders, each have 8 files. it would be good to convert the audio and duplicate the folders in the `converted` folder the same as in the convert folder, but is not essential. 

i have attached the two scripts, I hope someone can help me resolve these, thanks

A

$source = Get-ChildItem -path "D:\convert*" -Recurse -Include *.mkv
$ffpath = "C:\Windows\System32\ffmpeg.exe"$DirOut = "D:\converted"
$output = Join-Path $DirOut -ChildPath ((Get-Item $source).basename + "_out.mkv")& $ffpath -i $source -map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640k $output -loglevel 8 -stats

 

B

:: convert all mkv files in a source Folder to mkv with AC3 using ffmpeg
:: eg. source T:\Old mkvs and eg.destination D:\New mkvs

:-----------------------------------------------------------------------------------------------------------------------------------
:: first edit the two Directory values for the Source and Destination "and" the path of ffmpeg
:-----------------------------------------------------------------------------------------------------------------------------------

SET Source_Files_Dir=D:\convert
SET Converted_Files_Dir=R:\Sons.of.Anarchy
SET ffmpeg=C:\Windows\System32\ffmpeg.exe

cd /d %Converted_Files_Dir%
for /F "delims=" %%a in ('dir /b /s "%Source_Files_Dir%"\*.mkv') do (
"%ffmpeg%" -i "%%a" -map 0 -an -vcodec copy -scodec copy -acodec ac3 -b:a 640k "%%~na_out.mkv" -loglevel 8 -stats
)
pause
exit

  • Could you share the errors you're receiving? It would help a lot in seeing what's going wrong
    • Reddwarf4ever's avatar
      Reddwarf4ever
      Copper Contributor

      Harm_Veenstra 

      Script A with Errors

      PS C:\WINDOWS\system32> $source = Get-ChildItem -path "D:\convert*" -Recurse -Include *.mkv
      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32> $ffpath = "C:\Windows\System32\ffmpeg.exe"
      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32> $DirOut = "D:\converted"
      PS C:\WINDOWS\system32> $output = Join-Path $DirOut -ChildPath ((Get-Item $source).basename + "_out.mkv")
      Join-Path : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ChildPath'. Specified
      method is not supported.
      At line:1 char:40
      + ... in-Path $DirOut -ChildPath ((Get-Item $source).basename + "_out.mkv")
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [Join-Path], ParameterBindingException
      + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.JoinPathCommand

      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32>
      PS C:\WINDOWS\system32> & $ffpath -i $source -map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640k $output -loglevel 8 -stats

       

       

      Script B with errors

      PS C:\WINDOWS\system32> :: convert all mkv files in a source Folder to mkv with AC3 using ffmpeg
      >> :: eg. source T:\Old mkvs and eg.destination D:\New mkvs
      >>
      >> :-----------------------------------------------------------------------------------------------------------------------------------
      >> :: first edit the two Directory values for the Source and Destination "and" the path of ffmpeg
      >> :-----------------------------------------------------------------------------------------------------------------------------------
      >>
      >> SET Source_Files_Dir=D:\convert
      >> SET Converted_Files_Dir=R:\Sons.of.Anarchy
      >> SET ffmpeg=C:\Windows\System32\ffmpeg.exe
      >>
      >> cd /d %Converted_Files_Dir%
      >> for /F "delims=" %%a in ('dir /b /s "%Source_Files_Dir%"\*.mkv') do (
      >> "%ffmpeg%" -i "%%a" -map 0 -an -vcodec copy -scodec copy -acodec ac3 -b:a 640k "%%~na_out.mkv" -loglevel 8 -stats
      >> )
      >> pause
      >> exit
      At line:13 char:4
      + for /F "delims=" %%a in ('dir /b /s "%Source_Files_Dir%"\*.mkv') do (
      + ~
      Missing opening '(' after keyword 'for'.
      At line:14 char:12
      + "%ffmpeg%" -i "%%a" -map 0 -an -vcodec copy -scodec copy -acodec ac3 ...
      + ~~
      Unexpected token '-i' in expression or statement.
      At line:14 char:15
      + "%ffmpeg%" -i "%%a" -map 0 -an -vcodec copy -scodec copy -acodec ac3 ...
      + ~~~~~
      Unexpected token '"%%a"' in expression or statement.
      At line:14 char:14
      + "%ffmpeg%" -i "%%a" -map 0 -an -vcodec copy -scodec copy -acodec ac3 ...
      + ~
      Missing closing ')' in expression.
      At line:15 char:1
      + )
      + ~
      Unexpected token ')' in expression or statement.
      + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

      PS C:\WINDOWS\system32>

       

      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP
        $output = Join-Path $DirOut -ChildPath ((Get-Item $source).basename + "_out.mkv") fails when there are multiple files found ($source variable) Perhaps you should try this:

        $ffpath = "C:\Windows\System32\ffmpeg.exe"
        $DirOut = "D:\converted"
        foreach ($sourcefile in $source) {
        $output = Join-Path $DirOut -ChildPath ((Get-Item $sourcefile).basename + "_out.mkv")
        & $ffpath -i $sourcefile -map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640k $output -loglevel 8 -stats
        }

        The second script is not PowerShell but batch/cmd, you should save the script in a .cmd file and try to run that...

Resources