如何将视频 *.ts 即时转换为 *.mp4 并在网络上播放 (node.js)

How to convert video *.ts to *.mp4 on the fly and playback it on web (node.js)

使用 Node.js 和 'fluent-ffmpeg' 我可以将视频流从 Live TV 转换为 mp4,因此它在 HTML5 视频中播放。 我有什么:

我需要以某种方式告诉 FFmpeg,传入的文件正在增长,需要等待新数据以进行进一步转换...

也很好奇是否有办法给出这个 .mp4 文件,它应该不断增长到 HTML5 视频播放器。

这是我现在的代码:

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

标准 mp4 文件不能用于实时视频。 MP4 文件使用一种结构,将所有帧大小编码到文件末尾(或开头)的单个位置。因此,mp4 在完成并写入此信息之前无法播放。有一种叫做“fragmented mp4”的东西,可以制作可以连续播放的小 mp4。