Converting video with DTS to AC3 using FFMpeg & PowerShell

Copper Contributor

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

11 Replies
Could you share the errors you're receiving? It would help a lot in seeing what's going wrong

@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>

 

$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...

@Harm_Veenstra 

thank you

How does script A know the source folder I.e. Convert ?

 

$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
}

 

8)8)8)

Forgot to put this line in my post, it should be the first line

$source = Get-ChildItem -path "D:\convert*" -Recurse -Include *.mkv

Well, Script A worked perfectly Thanks

 

Script B in a CMD window did nothing, sat there with a flashing cursor ???

 

does it get too complicated to create folders in the converted folder the same as the convert folder, so the files are converted into like for like folders ?

if it’s complicated I won’t trouble you, just a thought.

thanks again

The video files converted to AC3 in around 2 1/2 hours, 70 files. But it seems it may not just have been the audio, as they still won’t play on my TV, but will play on an Xbox with VLC player app. Maybe if I used the script you made, but instead of converting the audio, leave that as is and convert the MKV to AVI or MP4 maybe, that may sort out the issues, is that something that can be done easily, sorry to give you another project.
Thanks
I don't know much about converting video files, but can't you use HandBrake (https://handbrake.fr/) for it and try to find out what format your TV does play? You can add things to a queue there too for multiple files

@Harm_Veenstra 

thankyou

Should I post the script that worked for others to see ?

That's always a good idea, sharing is good
And mark my answer as solution if handbrake work out for you to mark it as solved