the_man
the_man

Reputation: 79

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:

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;
}

Upvotes: 2

Views: 5472

Answers (1)

szatmary
szatmary

Reputation: 31100

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.

Upvotes: 3

Related Questions