简体   繁体   English

在 nodejs 和 express 中使用 fluent-ffmpeg 下载视频

[英]Downloading a video using fluent-ffmpeg in nodejs and express

I am working on a side project to download videos from Reddit, but they separate video and audio in different files.我正在做一个副项目,从 Reddit 下载视频,但它们将视频和音频分开放在不同的文件中。 so i have to merge them first before downloading them in the client.所以我必须先合并它们,然后再将它们下载到客户端。 i was able to do all of this as in the following snippet of code.我能够按照以下代码片段执行所有这些操作。

const ffmpeg = require("fluent-ffmpeg");
const proc = new ffmpeg();

app.post('/download', async (req, res) => {
   
   const audio = "some aduio link";
   const video = "some video link";

   proc.addInput(video)
     .output('${some path}./video.mp4')
     .format('mp4')
     .on("error", err => console.log(err))
     .on('end', () => console.log('Done'));

  if(audio) {
  proc.addInput(audio);
  }

  proc.run()
 
});

using the above code, the video is being download locally in the the server in the specified path.使用上面的代码,视频正在指定路径的服务器本地下载。

but i want to download the video in the client browser who sent the request.但我想在发送请求的客户端浏览器中下载视频。 i tried:我试过了:

proc.pipe(res); 

but it didn't work, it's my first time working with ffmpeg, so it would be nice if someone give me a hint但它没有用,这是我第一次使用 ffmpeg,所以如果有人给我提示就好了

add writeToStream(res, { end: true });添加writeToStream(res, { end: true }); atn the end to stream atn 结束到 stream

        const ffmpeg = require("fluent-ffmpeg");
        const proc = new ffmpeg();

        app.post('/download', async (req, res) => {
           
           const audio = "some aduio link";
           const video = "some video link";

           ffmpeg(video).format('mp4')
             .on("error", err => console.log(err))
             .on('end', () => console.log('Done')).writeToStream(res, { end: true });
         
        });

` i hope it works `我希望它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM