Skip to content

Instantly share code, notes, and snippets.

@koush
Created March 11, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koush/bee38219a6c9c0a0a5f3522ba97bbbed to your computer and use it in GitHub Desktop.
Save koush/bee38219a6c9c0a0a5f3522ba97bbbed to your computer and use it in GitHub Desktop.
const fs = require('fs');
var f = fs.readFileSync('test.ts')
var offset = 0
var pids = {}
// var fout = fs.openSync("check-trim.ts", "w")
var incompleteTypes = {
video: 0,
audio: 0,
}
function readPts(chunk, position) {
var h = chunk.readUInt8(position)
var m = chunk.readUInt16BE(position + 1)
var l = chunk.readUInt16BE(position + 3)
return ((h & 0x0e) << 29) | ((m >> 1) << 15) | (l >> 1)
// return (int64_t)(*buf & 0x0e) << 29 |
// (AV_RB16(buf+1) >> 1) << 15 |
// AV_RB16(buf+3) >> 1;
}
var lastPacket
function checkPtsDts(type, chunk) {
var pusi = chunk[1] & 0x40
var adaptation = chunk[3] & 0x20
var payload = chunk[3] & 0x10
if (!payload)
props.payload = false
var props = {}
var tsPrefixLength = 4
var pesPrefixLength = 3
var pesAdjust = pesPrefixLength + tsPrefixLength
if (adaptation) {
var adaptationLength = chunk[4]
pesAdjust += adaptationLength + 1
props.adaptationAdjust = pesAdjust
if (adaptationLength) {
var discontinuity = chunk[5] & 0x80
var randomAccess = chunk[5] & 0x40
var pcr = chunk[5] & 0x10
if (discontinuity)
props.discontinuity = true
if (randomAccess)
props.randomAccess = true
if (pcr) {
var pcrHigh = chunk.readUInt32BE(6)
var pcrLow = chunk.readUInt16BE(10)
pcrHigh <<= (16 - 9)
pcrHigh |= pcrLow >> (16 - 9)
pcrLow &= 0x1FF
pcr = pcrHigh * 300 + pcrLow
props.pcr = pcr
}
}
else {
console.log('emoty adpa')
}
}
if (!pusi) {
incompleteTypes[type]++
// props.partial = true
}
else {
// props.first = true
var pesStreamId = chunk.readUInt8(0 + pesAdjust)
var pesLength = chunk.readUInt16BE(1 + pesAdjust)
props.pesLength = pesLength
var pesFlag = chunk[3 + pesAdjust]
var alignment = pesFlag & 0x04
var ptsOrDts = chunk[4 + pesAdjust]
var pts = ptsOrDts & 0x80
var dts = ptsOrDts & 0x40
if (pts || dts) {
var ptsLength = chunk.readUInt8(5 + pesAdjust)
var pesPosition = 6 + pesAdjust
if (pts) {
pts = readPts(chunk, pesPosition)
pesPosition += 5
props.pts = pts
}
else {
props.pts = false
}
if (dts) {
dts = readPts(chunk, pesPosition)
pesPosition += 5
props.dts = dts
}
else {
props.dts = false
}
}
else {
props.pts = false
props.dts = false
}
if (!alignment)
props.alignment = false
else
console.log('aignmten')
}
if (Object.keys(props).length != 0) {
console.log(`${type}: ${JSON.stringify(props)}`)
}
else {
// console.log(`${type} fine`)
}
// if (props.pts)
// console.log(`${type} ${props.pts}`)
lastPacket = chunk
}
while (offset < f.length) {
var chunk = f.slice(offset, offset + 188)
offset += 188
var pid = chunk.readInt16BE(1) & 0x00001FFF
pids[pid] = (pids[pid] || 0) + 1
if (pid == 256 || pid == 0x64)
checkPtsDts('video', chunk)
else if (pid == 257 || pid == 0xc8)
checkPtsDts('audio', chunk)
// if (pid != 17) {
// fs.writeSync(fout, chunk)
// }
}
// fs.closeSync(fout)
for (var pid of Object.keys(pids)) {
console.log(parseInt(pid).toString(16) + " : " + pids[pid])
}
console.log('partial video: ' + incompleteTypes.video)
console.log('partial audio: ' + incompleteTypes.audio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment