修复播放地址
This commit is contained in:
20
server.js
20
server.js
@@ -543,7 +543,24 @@ app.get('/api/hls/segment.ts', async (req, res) => {
|
|||||||
|
|
||||||
const targetSegPath = path.join(hlsDir, `segment_${seg}.ts`);
|
const targetSegPath = path.join(hlsDir, `segment_${seg}.ts`);
|
||||||
let currentProcess = hlsProcesses.get(progressKey);
|
let currentProcess = hlsProcesses.get(progressKey);
|
||||||
const needsNewProcess = !currentProcess || (!fs.existsSync(targetSegPath) && Math.abs((currentProcess.currentSeg || 0) - seg) > 2);
|
|
||||||
|
const checkIsCachedAndCompleted = () => {
|
||||||
|
if (!fs.existsSync(targetSegPath)) return false;
|
||||||
|
const m3u8Path = path.join(hlsDir, `temp.m3u8`);
|
||||||
|
if (fs.existsSync(m3u8Path) && fs.readFileSync(m3u8Path, 'utf8').includes(`segment_${seg}.ts`)) return true;
|
||||||
|
if (currentProcess && Math.abs((currentProcess.currentSeg || 0) - seg) > 3) return true;
|
||||||
|
// If there's no active process, any existing file is from a past complete run
|
||||||
|
if (!currentProcess) return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (checkIsCachedAndCompleted()) {
|
||||||
|
if (currentProcess) currentProcess.currentSeg = Math.max(currentProcess.currentSeg, seg);
|
||||||
|
res.setHeader('Content-Type', 'video/MP2T');
|
||||||
|
return res.sendFile(targetSegPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const needsNewProcess = !currentProcess || (!fs.existsSync(targetSegPath) && (seg < (currentProcess.currentSeg || 0) || seg > (currentProcess.currentSeg || 0) + 4));
|
||||||
|
|
||||||
if (needsNewProcess) {
|
if (needsNewProcess) {
|
||||||
if (currentProcess && currentProcess.command) {
|
if (currentProcess && currentProcess.command) {
|
||||||
@@ -580,6 +597,7 @@ app.get('/api/hls/segment.ts', async (req, res) => {
|
|||||||
'-hls_segment_filename', segmentFilename,
|
'-hls_segment_filename', segmentFilename,
|
||||||
'-start_number', seg.toString()
|
'-start_number', seg.toString()
|
||||||
]);
|
]);
|
||||||
|
if (startTime > 0) hlsOptions.push('-output_ts_offset', startTime.toString());
|
||||||
|
|
||||||
ffmpegCommand.outputOptions(hlsOptions).output(m3u8Path);
|
ffmpegCommand.outputOptions(hlsOptions).output(m3u8Path);
|
||||||
ffmpegCommand.on('error', (err) => {
|
ffmpegCommand.on('error', (err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user