Created
March 9, 2020 01:06
-
-
Save koush/b40488b1103c86bca80c25e7353b9627 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const fs = require('fs'); | |
var f = fs.readFileSync('check.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 adaptationAdjust = 0 | |
if (adaptation) { | |
var adaptationLength = chunk[4] | |
adaptationAdjust = chunk[4] + 1 | |
props.adaptationAdjust = adaptationAdjust | |
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 pesLength = chunk.readUInt16BE(8 + adaptationAdjust) | |
props.pesLength = pesLength | |
var pesFlag = chunk[10 + adaptationAdjust] | |
var alignment = pesFlag & 0x04 | |
var ptsOrDts = chunk[11 + adaptationAdjust] | |
var pts = ptsOrDts & 0x80 | |
var dts = ptsOrDts & 0x40 | |
if (pts || dts) { | |
var ptsLength = chunk.readUInt8(12 + adaptationAdjust) | |
var pesPosition = 13 + adaptationAdjust | |
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