update tests & replace ffprobe -> mediainfo.js

This commit is contained in:
Ismaël Moret
2025-09-02 17:00:50 +00:00
parent 825d198ac8
commit 1ca59cba57
6 changed files with 207 additions and 476 deletions

View File

@@ -1,27 +1,83 @@
import { Stream } from '../models'
import { IPTVChecker } from 'iptv-checker'
import { TESTING } from '../constants'
import MediainfoFactory from 'mediainfo.js'
export class StreamTester {
checker: IPTVChecker
constructor() {
this.checker = new IPTVChecker()
}
constructor() {}
async test(stream: Stream) {
if (TESTING) {
const results = (await import('../../tests/__data__/input/playlist_test/results.js')).default
return results[stream.url]
return results[stream.url as keyof typeof results]
} else {
return this.checker.checkStream({
url: stream.url,
http: {
referrer: stream.getReferrer(),
'user-agent': stream.getUserAgent()
try {
const controller = new AbortController()
const timeout = 10000
const timeoutId = setTimeout(() => controller.abort(), timeout)
const res = await fetch(stream.url, {
signal: controller.signal,
headers: {
'User-Agent': stream.getUserAgent() || 'Mozilla/5.0',
Referer: stream.getReferrer()
}
})
clearTimeout(timeoutId)
if (!res.ok) {
return {
status: {
ok: false,
code: `HTTP_${res.status}`
}
}
}
})
const mediainfo = await MediainfoFactory({ format: 'object' })
const buffer = await res.arrayBuffer()
const result = await mediainfo.analyzeData(
() => buffer.byteLength,
(size: any, offset: number | undefined) =>
Buffer.from(buffer).subarray(offset, offset + size)
)
if (result && result.media && result.media.track.length > 0) {
return {
status: {
ok: true,
code: 'OK'
}
}
} else {
return {
status: {
ok: false,
code: 'NO_VIDEO'
}
}
}
} catch (error: any) {
let code = 'UNKNOWN_ERROR'
if (error.name === 'AbortError') {
code = 'TIMEOUT'
} else if (error.cause) {
const cause = error.cause as Error & { code?: string }
if (cause.code) {
code = cause.code
} else {
code = cause.name
}
}
return {
status: {
ok: false,
code
}
}
}
}
}
}