Ad

How To Convert Video *.ts To *.mp4 On The Fly And Playback It On Web (node.js)

Using the Node.js with 'fluent-ffmpeg' i may convert video stream from Live TV to mp4, so it is playing in HTML5 Video. What i have:

  • incoming video is transport stream (the Live TV) which i receive by link from my server http://IP/stream/direct?channel=8724
  • receive data from stream and write it to file, say in.ts , then this in.ts file i give to ffmpeg as an input file.
  • this scheme works, But, i got a short output file from ffmpeg (out.mp4), even thought the in.ts grows constantly. FFmpeg converts only file size which was written at the time when ffmpeg starts to work.

I need somehow to say to FFmpeg, that incoming file is growing and need to wait new data for futher convertation ...

Also curious if there a way to give this out.mp4 file which should constantly grow to HTML5 video player.

Here is a code i have now:

let ffmpeg = require('fluent-ffmpeg');
let fs = require('fs');
let http = require('http');

let inStream = 'http://IP/stream/direct?channel=8724';
let inFileName = 'in.ts';
let inWriteStream = fs.createWriteStream(inFileName);

let isRun = false;
let request = http.get(inStream, (d) => {
  d.on('data', (d) => {
    inWriteStream.write(d);
    console.log(getSize());
    if (getSize() > 10 && !isRun) {
      startDecode();
      isRun = true;
    }
  });
})
  .on('error', (e) => {
    console.error(e);
  });
function startDecode() {
  var infs = fs.createReadStream(inFileName);
  ffmpeg(infs)
    .save('out.mp4');
  console.log('Decoding....');
}

function getSize() {
  let stats = fs.statSync(inFileName);
  let fileSizeInBytes = stats.size;
  let fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
  //size in Mb
  return fileSizeInMegabytes;
}
Ad

Answer

Standard mp4 files can not be used for live video. MP4 files use a structure that encodes all frame sizes into a single location at the end (or beginning) of a file. Therefore, an mp4 is not playable until it is complete and this information is written. There is such a thing as “fragmented mp4” that makes little mp4s that can be played back to back.

Ad
source: stackoverflow.com
Ad