fluent-ffmpeg / node-fluent-ffmpeg

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

How to stream video in Nextjs 14 api route #1240

Open MasterProvider opened 10 months ago

MasterProvider commented 10 months 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 7 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 3 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?