【问题标题】:child process spawn output file and directory in NodeJsNodeJs 中的子进程生成输出文件和目录
【发布时间】:2021-08-22 19:12:16
【问题描述】:

我正在使用带有子进程的ffmpeg来合并音频和视频,但是当我尝试下载文件时,它正在同一个文件夹中下载,但我希望它下载到公共文件夹中。

以下是我的子进程生成代码。

// Start the ffmpeg child process
const ffmpegProcess = cp.spawn(ffmpeg, [
    // Remove ffmpeg's console spamming
    '-loglevel', '8', '-hide_banner',
    // Redirect/Enable progress messages
    '-progress', 'pipe:3',
    // Set inputs
    '-i', 'pipe:4',
    '-i', 'pipe:5',
    // Map audio & video from streams
    '-map', '0:a',
    '-map', '1:v',
    // Keep encoding
    '-c:v', 'copy',
    // Define output file
    'title.mp4',
], {
    windowsHide: true,
    stdio: [
        /* Standard: stdin, stdout, stderr */
        'inherit', 'inherit', 'inherit',
        /* Custom: pipe:3, pipe:4, pipe:5 */
        'pipe', 'pipe', 'pipe',
    ],
});

【问题讨论】:

  • // Define output file,也许可以尝试更改路径,例如'./public/title.mp4',

标签: node.js ffmpeg child-process spawn ytdl


【解决方案1】:

您可以设置cwd 选项。它将设置您的子进程执行其代码的路径。例如,如果您喜欢在 fs 的根目录下运行您的进程,您可以这样做:

............
], {
    cwd: '/',
    windowsHide: true,
    stdio: [
        /* Standard: stdin, stdout, stderr */
        'inherit', 'inherit', 'inherit',
        /* Custom: pipe:3, pipe:4, pipe:5 */
        'pipe', 'pipe', 'pipe',
    ],
});

所以设置你的公共文件夹的路径,然后它应该在那里下载。

【讨论】:

  • 你能帮我更改输出文件的名称吗,我正在尝试使用分配给变量的字符串来更改它,它会给出错误并关闭进程。我正在做${fileName}.mp4
  • 应该可以工作,例如const dd = console.log('archchchchch'); const x = spawn(process.execPath, ['-e', `${dd}`])
  • 我的意思是对你来说可能是 ....'-c:v', 'copy',` ${fileName}.mp4`], {..... .当然文件名必须在之前声明
  • 我已经这样做了,但是还是不行,有没有其他办法呢?
  • 奇怪,它应该可以工作。您是否尝试过先设置变量然后传递它,例如:let name = `${filename}.mp4`; const ffmpegProcess = cp.spawn(ffmpeg, [...,.'-c:v', 'copy',`${name}`], {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2019-02-10
  • 2019-06-10
  • 2015-12-12
  • 2010-12-08
  • 1970-01-01
相关资源
最近更新 更多