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
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad