首页 > 解决方案 > NodeJs 中的子进程生成输出文件和目录

问题描述

我正在使用带有子进程的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',
    ],
});

标签: node.jsffmpegchild-processspawnytdl

解决方案


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

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

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


推荐阅读