0
CD "C:\Input"
for %%a in ("*.*") do "C:\ffmpeg.exe" -i %%a -map 0:v -map 0:a:0 -map 0:s -c:v copy -c:a ac3 -b:a 640K "C:\Out\%%~na.mkv"
pause

if there is no space this script works fine, but if there is a space in file name, the script reads only the first word of the file and throws an error. i tried to add a double quote first %%a. but then the script does not even run.

here is an example file name: [TEST] ABC test - 99

Shahpari
  • 115
  • 2
  • 8
  • 1
    Double-quoting the first `%%a` *should* have worked - you *did* retain the space after the `-i` didn't you? Try using `%%~sa` to use the shortname. "Doesn't run" - does what? simply appears to miss the `ffmpeg` command? – Magoo Aug 25 '22 at 07:59
  • if i use double quote the first `%%a`, batch file is just open and close. yes there is a space before `-i`. – Shahpari Aug 25 '22 at 08:09
  • When you say you tried to add quotes to the first %%a do you mean this "for %%a" or this "-i %%a "? – Ricardo Bohner Aug 25 '22 at 14:28

1 Answers1

2

This can be resolved by putting quotes around the input filename:

for %%a in ("*.*") do "C:\ffmpeg.exe" -i "%%~a" -map 0:v -map 0:a:0 -map 0:s -c:v copy -c:a ac3 -b:a 640K "C:\Out\%%~na.mkv"
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Ricardo Bohner
  • 301
  • 2
  • 10
  • There can be used just `"%%a"` instead of `"%%~a"` as `for` assigns the found file name always without double quotes to the loop variable `a`. So there is no need to use `%~a` to remove perhaps existing surrounding double quotes as there are never surrounding double quotes in this case. It is possible to use `a` as loop variable in this case, but it would be better to use a different character, a character not being also a modifier, see [Issue 7: Usage of letters ADFNPSTXZadfnpstxz as loop variable](https://stackoverflow.com/a/60686543/3074564). – Mofi Aug 25 '22 at 16:40