I am trying to do a basic hardsub work with FFmpeg in a Powershell script. Here is the code:

$subfile = ".\test\sub.srt"

ffmpeg -i in.mp4 -c:v h264 -vf "subtitles=${subfile}:'force_style=fontsize=20'" out.mp4

When I run this, I got following error in FFmpeg:

"Unable to open .testsub.srt"

Strangely FFmpeg cut the backlashes from $subfile variable.

If I change the command like below (remove curly braces for $subfile variable):

$subfile = ".\test\sub.srt"

ffmpeg -i in.mp4 -c:v h264 -vf "subtitles=$subfile:'force_style=fontsize=20'" out.mp4

This time PowerShell gets me an error like this:

"Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name."

In FFmpeg command above all of the " ' and : symbols are necessary, so I can't change that unfortunetly. What should I do?

Thank you.

  • Can you try $($subfile): io ${subfile}: – Lieven Keersmaekers Mar 12 at 6:49
  • No that didn't work unfortunately. – Serhat Çelik Mar 12 at 6:58
  • 1
    Replace with forward slashes and check. – Gyan Mar 12 at 7:07
  • Okay forward slashes did work! But why we use /? Isn't it Linux like? – Serhat Çelik Mar 12 at 8:41

When I tried "Gyan's" solution it worked. I also tried changing all patterns from \ to / like this:

$subfile = ".\test\sub.srt"

$pattern = "[\\]"

$subfile = $subfile -replace $pattern, "/"

ffmpeg -i in.mp4 -c:v h264 -vf "subtitles=${subfile}:'force_style=fontsize=20'" out.mp4
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.