【问题标题】:What is the best way to convert files with a heroku deployed app?使用 heroku 部署的应用程序转换文件的最佳方法是什么?
【发布时间】:2021-08-25 06:00:22
【问题描述】:

我正在编写一个 Discord 机器人,它已部署到 heroku。我有一个机器人的功能,它将接收一个视频文件并将其从任何格式转换为 .mp4,然后机器人回复嵌入的文件。

在本地,一切运行正常,而且我知道 Heroku 使用临时文件系统,但考虑到我并不真正关心持久化这些文件,我认为在磁盘上创建它们,回复,然后删除就可以了他们。

但是,每当我在已部署的机器人上运行命令时,我都会收到此错误错误:ENOENT: no such file or directory, stat '/app/dest/videostoconvert/851730039589306388.mp4'

const getAndConvertVideoFile = async (
    message: Discord.Message,
    url: string,
    inputPath: string,
    outputPath: string
) => {
    try {
        if (!fs.existsSync(path.join(__dirname, "../../", "videostoconvert"))) {
            const mkDirPromise = util.promisify(fs.mkdir);
            await mkDirPromise(path.join(__dirname, "../../", "videostoconvert"));
        }
    } catch (error) {
        console.log("ERROR CREATING DIRECTORY");
        console.error(error);
    }

    const response = await axios({
        method: "get",
        url: url,
        responseType: "stream"
    });

    console.log("INPUT PATH", inputPath);
    // INPUT PATH /app/dest/videostoconvert/851730039589306388.m4v
    console.log("OUTPUT PATH", outputPath);
    // OUTPUT PATH /app/dest/videostoconvert/851730039589306388.mp4
    try {
        const diskWriteStream = fs.createWriteStream(inputPath);
        response.data.pipe(diskWriteStream);

        diskWriteStream.on("error", (error) => {
            console.log("DISK WRITE ERROR");
            console.error(error);
        });

        diskWriteStream.on("finish", async function () {
            hbjs.spawn({ input: inputPath, output: outputPath })
                .on("error", (error: any) => {
                    console.error("THERE WAS AN ERROR", error);
                    console.log("There was an error", error);
                })
                .on("progress", (progress: any) => {
                    console.log("Percent complete: %s, ETA: %s", progress.percentComplete, progress.eta);
                })
                .on("complete", async () => {
                    console.log("COMPLETE");
                    // seems to be where it fails, heroku logs "COMPLETE" and then the error
                    const attachment = new Discord.MessageAttachment(outputPath);
                    

                    await message.reply(attachment);
                });
        });
    } catch (error) {
        console.log("ERROR GETTING AND CONVERTING FILE");
        console.error(error);
    }
};

我在代码中添加了一些可能有帮助的 cmets。在我看来,这意味着该目录不存在,但如果该目录在代码的前面不存在,我将创建该目录,所以我不确定是什么导致了问题。此外,根据 Heroku 控制台输出,故障点似乎发生在 hbjs.spawn.on("complete", ...) 行,因此该目录似乎是为了在下载文件后写入文件而创建的。

我会说,我不反对使用 S3 来托管文件而不是直接写入 heroku 磁盘,但我不确定在这种情况下如何转换它们,正弦 hbjs 似乎期望用于写出转换后文件的磁盘路径。

【问题讨论】:

  • 我假设这一切都在本地正常工作?
  • @Loyd 是的!这一切都在本地工作。

标签: node.js typescript heroku handbrake-js


【解决方案1】:

我怀疑这与内部事实有关,handbrake-js 产生了一个单独的进程(HandbrakeCLI 二进制文件)来生成输出(编码的视频文件)。然后,Node.js 进程需要保持活动状态足够的滴答声以允许子进程完成。

也许 Heroku 在给 fs 和/或 handbrake-js 完成时间之前终止了 Node.js 进程。

我个人会尝试从使用 fs 和 handbrake-js 的基于事件的接口(即监听事件)切换到使用 async 方法和 Promise-yielding 接口。请参阅fs/promises 和the .run() method of handbrake-js。

【讨论】:

  • 太棒了,感谢您的洞察力。我会在接下来的几天里试一试,如果成功,我会通知你。
  • no probs.. 至少我们知道如果一切都在本地运行,它与 Heroku 相关.. 显然,您还应该检查您的应用容器上可用的 CPU 和存储类型 - 您可能没有任何持久存储选项..
猜你喜欢
  • 2010-09-05
  • 2023-03-29
  • 2016-10-02
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多