-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Displayed live streams using Adaptive technologhy HLS
- Loading branch information
1 parent
aed8eca
commit 59d3a3f
Showing
13 changed files
with
801 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export type Camera = { | ||
id: number; | ||
name: string; | ||
rtspUrl: string; | ||
}; | ||
|
||
export const cameraData: Camera[] = [ | ||
{ | ||
id: 1, | ||
name: 'Backyard', | ||
rtspUrl: 'rtsp://192.168.252.244:554', | ||
}, | ||
/* { | ||
id: 2, | ||
name: 'Pet room', | ||
rtspUrl: 'rtsp://192.168.252.244:554', | ||
}, | ||
{ | ||
id: 3, | ||
name: 'Main road', | ||
rtspUrl: 'rtsp://192.168.252.244:554', | ||
}, | ||
{ | ||
id: 4, | ||
name: 'Parking', | ||
rtspUrl: 'rtsp://192.168.252.244:554', | ||
}, | ||
{ | ||
id: 1, | ||
name: 'Backyard', | ||
rtspUrl: 'rtsp://192.168.252.244:554', | ||
}, */ | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* eslint-disable padded-blocks */ | ||
import type { Camera } from './cameraData'; | ||
import { cameraData } from './cameraData'; | ||
const { spawn } = require('child_process'); | ||
const fs = require('fs'); | ||
|
||
function createDirectory(directoryPath) { | ||
// Check if the directory already exists | ||
fs.access(directoryPath, fs.constants.F_OK, (err) => { | ||
if (!err) { | ||
console.log( | ||
`Directory '${directoryPath}' already exists. No action taken.`, | ||
); | ||
return; | ||
} | ||
|
||
// If the directory does not exist, create it | ||
fs.mkdir(directoryPath, { recursive: true }, (err) => { | ||
if (err) { | ||
console.error(`Error creating directory: ${err}`); | ||
return; | ||
} | ||
|
||
console.log(`Directory '${directoryPath}' created successfully.`); | ||
}); | ||
}); | ||
} | ||
|
||
function transcodeRTSPtoHLS(camera: Camera) { | ||
//Create stream directory and then run FFmpeg command | ||
|
||
createDirectory( | ||
`${__dirname}\\..\\${process.env.HLS_OUTPUT_DIRECTORY}\\${camera.id}`, | ||
); | ||
|
||
const ffmpegCommand = [ | ||
'-i', | ||
camera.rtspUrl, | ||
'-c:v', | ||
'libx264', | ||
'-c:a', | ||
'aac', | ||
'-hls_time', | ||
'3', | ||
'-hls_list_size', | ||
'15', | ||
'-hls_flags', | ||
'delete_segments', | ||
'-strict', | ||
'experimental', | ||
'-threads', | ||
'8', | ||
'-preset', | ||
'ultrafast', | ||
`${__dirname}\\..\\${process.env.HLS_OUTPUT_DIRECTORY}\\${camera.id}\\${camera.id}_${process.env.HLS_FILE_NAME}`, | ||
]; | ||
|
||
// Spawn FFmpeg process | ||
const ffmpegProcess = spawn('ffmpeg', ffmpegCommand); | ||
|
||
// Event listeners for process output | ||
ffmpegProcess.stdout.on('data', (data) => { | ||
console.log(`stdout: ${data}`); | ||
}); | ||
|
||
ffmpegProcess.stderr.on('data', (data) => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
|
||
ffmpegProcess.on('close', (code) => { | ||
console.log(`FFmpeg process closed with code ${code}`); | ||
}); | ||
|
||
ffmpegProcess.on('error', (err) => { | ||
console.error(`Error executing FFmpeg: ${err}`); | ||
}); | ||
} | ||
|
||
// Periodically check the RTSP streams and restart transcoding for each | ||
function periodicCheck() { | ||
console.log('Checking RTSP stream status...'); | ||
cameraData.forEach(transcodeRTSPtoHLS); | ||
} | ||
|
||
// Run once the application is run | ||
export function initiateCameraStream() { | ||
// Run the periodic check every 10 minutes (adjust as needed) | ||
setInterval(periodicCheck, 10 * 60 * 1000); | ||
|
||
// Initial transcoding for each RTSP stream | ||
cameraData.forEach(transcodeRTSPtoHLS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.