fluent-ffmpeg / node-fluent-ffmpeg

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

How to seek a transcoded video over HTTP? #1004

Open lucafaggianelli opened 4 years ago

lucafaggianelli commented 4 years ago

Hi there,

I'm working on a media center project (kind of Plex/personal Netflix), where, basically, I serve video files to a browser or to a Chromecast, though some videos are not supported thus I use fluent-ffmpeg for transcoding audio and/or video streams.

Everything works fine, but the videos aren't seekable and haven't the right duration, of course, this is normal due to HTTP protocol, Range headers, etc... Question is, do you suggest other protocols/methods to streaming a transcoded video over HTTP? Such as HLS or others?

This is the repo: https://github.com/lucafaggianelli/couch-buddy

and this is where the transcoding happens (I'm using Koa library): https://github.com/lucafaggianelli/couch-buddy/blob/master/server/routes/streaming.js

    videoStream = require('stream').PassThrough()

    ffmpeg(path)
      .withVideoCodec(isSupported.video ? 'copy' : 'libx264')
      .withAudioCodec(isSupported.audio ? 'copy' : 'aac')
      .format('mp4')
      .on('error', (err) => {
        console.log('FFMPEG error:', err)
      })
      .addOption('-movflags', 'frag_keyframe+empty_moov')
      .pipe(videoStream, { end: true })

  ctx.body = videoStream
schizobulia commented 4 years ago

reference

You can convert the video to hls, After success, the HLS link will be accessed.

You may then need to add a task manager or socket.io to get the status of the video parsing.

const FfmpegCommand = require('fluent-ffmpeg');
FfmpegCommand('filename.mp4')
    .videoCodec('libx264')
    .addOption('-hls_time', 10)
    .addOption('-hls_list_size', 0)
    .save('filename.m3u8')
    .on('start', () =>{})
    .on('end', () =>{
    })
    .on('stderr',(stderrLine)=> {})
    .on('error', (err) => {
    });