mirror of
https://github.com/ad1217/PrinterStatus
synced 2024-11-25 00:43:49 -05:00
Add reconnection handling to FFmpeg streams
This commit is contained in:
parent
36c3af1446
commit
d6a3fb766f
@ -1,5 +1,8 @@
|
|||||||
import * as ffmpeg from 'fluent-ffmpeg';
|
import * as ffmpeg from 'fluent-ffmpeg';
|
||||||
import * as Mp4Frag from 'mp4frag';
|
import * as Mp4Frag from 'mp4frag';
|
||||||
|
import { PassThrough } from 'stream';
|
||||||
|
|
||||||
|
const RECONNECT_TIME = 5000;
|
||||||
|
|
||||||
interface WebcamSettings {
|
interface WebcamSettings {
|
||||||
flipH: boolean;
|
flipH: boolean;
|
||||||
@ -7,11 +10,11 @@ interface WebcamSettings {
|
|||||||
rotate90: boolean;
|
rotate90: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function make_mp4frag(
|
function start_ffmpeg(
|
||||||
slug: string,
|
|
||||||
url: URL | string,
|
url: URL | string,
|
||||||
webcamSettings: WebcamSettings
|
webcamSettings: WebcamSettings,
|
||||||
): Mp4Frag {
|
mp4frag: Mp4Frag
|
||||||
|
) {
|
||||||
let transforms = [];
|
let transforms = [];
|
||||||
if (webcamSettings.flipH) {
|
if (webcamSettings.flipH) {
|
||||||
transforms.push('hflip');
|
transforms.push('hflip');
|
||||||
@ -26,6 +29,7 @@ export function make_mp4frag(
|
|||||||
const command = ffmpeg(url.toString())
|
const command = ffmpeg(url.toString())
|
||||||
.nativeFramerate()
|
.nativeFramerate()
|
||||||
.inputOptions([
|
.inputOptions([
|
||||||
|
'-timeout 5000000',
|
||||||
'-probesize 1048576',
|
'-probesize 1048576',
|
||||||
'-analyzeduration 10000000',
|
'-analyzeduration 10000000',
|
||||||
'-use_wallclock_as_timestamps 1',
|
'-use_wallclock_as_timestamps 1',
|
||||||
@ -48,16 +52,43 @@ export function make_mp4frag(
|
|||||||
'-pix_fmt yuv420p',
|
'-pix_fmt yuv420p',
|
||||||
'-movflags +dash+negative_cts_offsets',
|
'-movflags +dash+negative_cts_offsets',
|
||||||
])
|
])
|
||||||
|
.on('start', function (commandLine) {
|
||||||
.on('error', function (err) {
|
console.log('Spawned FFmpeg for ' + url.toString());
|
||||||
console.log('ffmpeg error occurred: ' + err.message);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let pipe = command.pipe(undefined, { end: false }) as PassThrough;
|
||||||
|
pipe.pipe(mp4frag);
|
||||||
|
|
||||||
|
function reconnect() {
|
||||||
|
start_ffmpeg(url, webcamSettings, mp4frag);
|
||||||
|
}
|
||||||
|
|
||||||
|
command.once('error', (err) => {
|
||||||
|
console.log(
|
||||||
|
`FFmpeg error occurred, reconnecting in ${RECONNECT_TIME}ms: ${err.message}`
|
||||||
|
);
|
||||||
|
pipe.unpipe();
|
||||||
|
command.kill('SIGKILL');
|
||||||
|
setTimeout(() => reconnect(), RECONNECT_TIME);
|
||||||
|
});
|
||||||
|
|
||||||
|
command.once('end', () => {
|
||||||
|
console.log(`FFmpeg stream ended, reconnecting in ${RECONNECT_TIME}ms:`);
|
||||||
|
pipe.unpipe();
|
||||||
|
setTimeout(() => reconnect(), RECONNECT_TIME);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function make_mp4frag(
|
||||||
|
slug: string,
|
||||||
|
url: URL | string,
|
||||||
|
webcamSettings: WebcamSettings
|
||||||
|
): Mp4Frag {
|
||||||
const mp4frag = new Mp4Frag({
|
const mp4frag = new Mp4Frag({
|
||||||
hlsPlaylistBase: slug,
|
hlsPlaylistBase: slug,
|
||||||
});
|
});
|
||||||
|
|
||||||
command.pipe(mp4frag);
|
start_ffmpeg(url, webcamSettings, mp4frag);
|
||||||
|
|
||||||
return mp4frag;
|
return mp4frag;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user