fluent-ffmpeg/node-fluent-ffmpeg

How to stream video in Nextjs 14 api route

MasterProvider opened this issue · 2 comments

Version information

  • fluent-ffmpeg version:2.1.2
  • ffmpeg version:2023-11-20-git-e56d91f8a8-essentials_build-www.gyan.dev
  • OS: Windows 11

Code to reproduce

import fs from 'fs';
import ffmpeg from 'fluent-ffmpeg';

export async function GET(request, { params }) {

try{

    const media_url = 'public/mario.mp4';

    var command = ffmpeg(media_url)
    .videoCodec('libx264')
    .audioCodec('libmp3lame')
    .size('320x240')
  
    var ffstream = command.stream(); // how to return the stream output 
    ffstream.on('data', function(chunk) {
        console.log('ffmpeg just wrote ' + chunk.length + ' bytes');
    });

    return new Response(ffstream , {
        status: 200,
        headers: { 'Content-Type': `video/mp4`},
    })

}catch(e){
    console.log(e.message);
}

}

(note: if the problem only happens with some inputs, include a link to such an input file)

Expected results

stream transcoded video in Nextjs api route

Observed results

You should be able to do something like

command.pipe(response)

When using pipe there might be a need to use format as mentioned in #802 here

You should be able to do something like

command.pipe(response)

When using pipe there might be a need to use format as mentioned in #802 here

It doesn't work with the app directory do you have any other solutions?