2

I'm using FFmpeg on Windows where I need to insert an \r\n in a parameter itself.

ffmpeg … -headers "X-Test: Test\r\nX-Test-2: Test 2\r\n" …

While this will work fine under Linux and such, it does not work from a normal Windows 10 command prompt. It also does not work under Bash under Windows. In both cases, a literal \r\n (backslash, r, backslash, n characters) are sent in.

I have tried using Alt+13 Alt+10, and while this appeared to insert characters, they were interpreted as 0xE299AA 0xE29799 by FFmpeg.

This question is similar to How can I insert a new line in a cmd.exe command?, except that I'm not tied to cmd.exe. I'm happy to use any shell as long as I can use it under Windows, if there are any suggestions. Is this possible via Powershell somehow? Or, maybe by putting my parameters in a text file and bringing it in as a parameter value?

phuclv
  • 30,396
  • 15
  • 136
  • 260
Brad
  • 6,629

3 Answers3

2

In PowerShell, you can use backtick-r-backtick-n and use backticks in place of the backslashes before the r and the n for the CRLF portions of the command—backtick "r" backtick "n".

This only requires this change to the existing command

ffmpeg … -headers "X-Test: Test`r`nX-Test-2: Test 2`r`n"

Further Resources

Brad
  • 6,629
1

Bash has $'…' syntax for this. It should work regardless of the OS.

ffmpeg … -headers $'X-Test: Test\r\nX-Test-2: Test 2\r\n' …

From Bash Reference Manual:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

[…]

\n
newline

\r
carriage return

[…]

0

Pure batch solution. Much worse than powershell of course so it's for reference only

setlocal EnableExtensions EnableDelayedExpansion

rem Get CR and LF characters
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

(set LF=^
%=EMPTY=%
)

ffmpeg … -headers "X-Test: Test!CR!!LF!X-Test-2: Test 2!CR!!LF!" …

However it's unlikely that you really need the CR character because the C library will automatically convert between '\n' and the native new line character(s) on input/output so you just need to use LF

phuclv
  • 30,396
  • 15
  • 136
  • 260