Декодирование потока видео на node
Есть такая проблема в браузере нет звука для формата mkv как можно конвентировать на лету поток стрима?
router.get('/:magnet/:fileName', (req: Request, res: Response, next: NextFunction) => {
const {
params: { magnet, fileName },
headers: { range }
} = req
if(!range) {
const err = new Error('Range is not defined, please make request from HTML5 Player') as ErrorWithStatuses
err.status = 416
return next(err)
}
const torrentFile = client.get(magnet) as Torrent
let file = <TorrentFile> {}
for(let i = 0; i < torrentFile.files.length; i++) {
const currentTorrentPiece = torrentFile.files[i]
if(currentTorrentPiece.name === fileName) {
file = currentTorrentPiece
}
}
const fileSize = file.length
const [startParsed, endParsed] = range.replace(/bytes=/, '').split('-')
const start = Number(startParsed)
const end = endParsed ? Number(endParsed) : fileSize - 1
const chunkSize = end - start + 1
const headers = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunkSize,
'Content-Type': 'video/mp4'
}
res.writeHead(206, headers)
const streamPositions = {
start,
end
}
const stream = file.createReadStream(streamPositions)
stream.pipe(res)
stream.on('error', err => {
return next(err)
})
})```
Я пробовал нарезать через ffmpeg но не получилось
const stream = file.createReadStream(streamPositions)
.on("open", function() {
ffmpeg()
.input(stream)
.outputFormat('mp4')
.outputOptions([ '-movflags faststart', '-frag_size 4096', '-cpu-used 2', '-deadline realtime', '-threads 4' ])
.videoBitrate(640, true)
.audioBitrate(128)
.audioCodec('aac')
.videoCodec('libx264')
.stream()
.pipe(res, { end: true})
}).on("error", function(err) {
console.error('Ffmpeg conversion error:', err);
res.end(err)
})