fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.97k stars 881 forks source link

How to stream video in Nextjs 14 api route #1240

Open MasterProvider opened 1 year ago

MasterProvider commented 1 year ago

Version information

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

opheliagame commented 9 months ago

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

DivyanshuLohani commented 5 months ago

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?