| 1 | #coding:utf-8
|
|---|
| 2 | from __future__ import print_function
|
|---|
| 3 | import os
|
|---|
| 4 | import sys
|
|---|
| 5 | import struct
|
|---|
| 6 | import math
|
|---|
| 7 |
|
|---|
| 8 | TAG_TYPE = {
|
|---|
| 9 | 18:'SCRIPT',
|
|---|
| 10 | 9:'VIDEO',
|
|---|
| 11 | 8:'AUDIO'
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | def _byte2ui24(byte):
|
|---|
| 15 | if sys.version.startswith('2'):
|
|---|
| 16 | _ = map(ord, byte)
|
|---|
| 17 | else:
|
|---|
| 18 | _ = byte
|
|---|
| 19 | return (_[0] << 16) + (_[1] << 8) + _[2]
|
|---|
| 20 |
|
|---|
| 21 | def grab(infile, outfile, buff_size = 0x200000, debug = True):
|
|---|
| 22 | #flv format http://wuyuans.com/2012/08/flv-format/
|
|---|
| 23 | _in = open(infile, 'rb')
|
|---|
| 24 | _in.seek(0xe40000)
|
|---|
| 25 | _out = None
|
|---|
| 26 |
|
|---|
| 27 | idx = 0xe40000
|
|---|
| 28 | cnt = 1
|
|---|
| 29 | _ = b'\x00' * 11 # setup
|
|---|
| 30 | while _:
|
|---|
| 31 | if _ == b'\x00' * 11:
|
|---|
| 32 | if _out:#not first
|
|---|
| 33 | _out.close()
|
|---|
| 34 | _out = None
|
|---|
| 35 | idx = math.ceil(idx / 0x40000) * 0x40000 # block size 0x40000?
|
|---|
| 36 | _in.seek(idx)
|
|---|
| 37 | _h = _in.read(9 + 4)#header 9, pre tag size
|
|---|
| 38 | if _h.strip(b'\x00') != '' and _h[:3] == b'FLV':#test header magic
|
|---|
| 39 | _out = open(outfile[:-4] + '_%d.flv' % cnt, 'wb')
|
|---|
| 40 | print('New flv(%dp) @0x%08X%s' % (cnt, idx, ' ' * 20))
|
|---|
| 41 | _out.write(_h)
|
|---|
| 42 | idx += 9 + 4
|
|---|
| 43 | cnt += 1
|
|---|
| 44 | #raw_input('>>>')
|
|---|
| 45 | else:
|
|---|
| 46 | tagtype, length = struct.unpack('b3s7x', _)
|
|---|
| 47 | length = _byte2ui24(length)
|
|---|
| 48 | if debug:
|
|---|
| 49 | print('TAG:%6s 0x%06X @0x%08X-0x%08X%s' % (TAG_TYPE[tagtype], length, idx, idx + length + 4, '\b' * 42), end = '')
|
|---|
| 50 | idx += length + 4
|
|---|
| 51 | _ = _in.read(length + 4) #data, pre tag size
|
|---|
| 52 | _out.write(_)
|
|---|
| 53 |
|
|---|
| 54 | #next tag
|
|---|
| 55 | _ = _in.read(11)
|
|---|
| 56 | if _out:
|
|---|
| 57 | _out.write(_)
|
|---|
| 58 | idx += 11
|
|---|
| 59 |
|
|---|
| 60 | _in.close()
|
|---|
| 61 | #_out.close()# out is already closed
|
|---|
| 62 | return cnt - 1
|
|---|
| 63 |
|
|---|
| 64 | if __name__ == "__main__":
|
|---|
| 65 | cnt = grab('sample.kux', 'sample.flv', debug = False)
|
|---|