如何为linux,nodejs exec函数转义特殊字符

svmlkihl  于 2023-03-01  发布在  Linux
关注(0)|答案(1)|浏览(138)

我在我的linux服务器上运行这个ffmpeg命令,当我把它粘贴到终端时,它工作得很好,但是当我使用execPromise运行完全相同的命令时,它返回了一个错误。

const { exec } = require('child_process');
const { promisify } = require('util');
const execPromise = promisify(exec);

const encode = async ffmpegCode => {
    try {
        console.log(ffmpegCode) //Here I can see that the code is the
                                //exact same one than the one that works
                                //when pasted into the terminal
        await execPromise(ffmpegCode);
        return 200
    } catch (err) {
        console.log(err)
    }
}

我需要将\:解释为\:。当我按原样键入\:时,错误消息显示它将其解释为:,这是预期的。
如果我传入\\:,我希望它按照我的需要解释它,这将是\:,但错误显示它将它解释为\\:
\\\:解释为\\:,将\\\\:解释为\\\\:
已传递命令的一部分:
...drawtext=text='timestamp \\: %{pts \\: localtime \\: 1665679092.241...
预期命令:
x1米11米1x
错误信息:
...drawtext=text='timestamp \\: %{pts \\: localtime \\: 1665679092.241...
如何让/:进入exec函数?

ymdaylpp

ymdaylpp1#

最后,ChatGPT通过问正确的问题救了我。它说javascript确实在转义我的\,这就是导致问题的原因。答案是使用String.raw()来防止这种行为。
const command = String.raw...drawtext=text='timestamp : %{pts : localtime...``

相关问题