How to scape 特殊字符 for linux, nodejs exec function

问题描述 投票:0回答:1

我在我的 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...

预期命令:

...drawtext=text='timestamp \: %{pts \: localtime \: 1665679092.241...

错误信息:

...drawtext=text='timestamp \\: %{pts \\: localtime \\: 1665679092.241...

如何让

/:
进入 exec 函数?

node.js linux ffmpeg escaping special-characters
1个回答
0
投票

原来 JS 确实在运行我的命令之前试图转义我的特殊字符所以诀窍是使用

String.raw()

const command = String.raw`...drawtext=text='timestamp \: %{pts \: localtime...` 

© www.soinside.com 2019 - 2024. All rights reserved.