Skip to content

Instantly share code, notes, and snippets.

@hhrhhr
Created July 27, 2019 20:41
Show Gist options
  • Save hhrhhr/270a3a930a90df13e93540e1600a71dd to your computer and use it in GitHub Desktop.
Save hhrhhr/270a3a930a90df13e93540e1600a71dd to your computer and use it in GitHub Desktop.
парсер медиапотока с BB-M2
--[[
lua strea_parser.lua <input> [output]
output - путь к выходному файлу без расширения
]]
assert("Lua 5.3" == _VERSION)
assert(arg[1], "\n\n[ERROR] no input file\n\n")
local r
local function uint8() return string.unpack("B", r:read(1)) end
local function uint16() return string.unpack("H", r:read(2)) end
local function uint32() return string.unpack("I4", r:read(4)) end
local h264 = arg[1]
local out = arg[2] or nil
r = io.open(h264, "r+b")
local filesize = r:seek("end")
r:seek("set")
if nil == out then
print("type, sid, ms, sec, frame, len, ver, res, sessid, cursid, endflag, byzone, ch, type1, sample, index")
end
local w0, w6
if out then
w0 = io.open(out .. ".h264", "w+b")
w6 = io.open(out .. ".adpcm", "w+b")
end
while r:seek() < filesize do
assert("\x55\xaa\x15\xa8" == r:read(4))
local t = {
uint8(), uint8(), uint16(), uint32(),
uint32(), uint32(), uint8(), uint8(),
uint8(), uint8(), uint8(), uint8(),
uint8(), uint8(), uint16(), uint16(),
}
if out then
local t1 = t[1]
local data = r:read(t[6])
if 0 == t1 or 1 == t1 then w0:write(data)
elseif 6 == t1 then w6:write(data)
else assert(false)
end
else
print(table.concat(t, ","))
r:seek("cur", t[6])
end
end
r:close()
if out then
local wsz = w0:seek()
w0:close()
if 0 == wsz then os.remove(out .. ".h264") end
w6:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment