Python 获取 Flash SWF 文件宽度和高度(Width, Height)

 Python脚本快速获取 swf 宽度和高度(像素),用法

python swfdimension.py test.swf

'''
filename swfdimension.py
SWF 文件格式
https://wwwimages2.adobe.com/content/dam/acom/en/devnet/pdf/swf-file-format-spec.pdf

获取单个文件长宽
python swfdimension.py onestab.swf

批量处理当前文件夹内所有swf文件
python swfdimension.py ./

© onestab csdn 2019-06
'''
import sys, os, zlib, lzma

def getswfdimension(fname):
    with open(fname, 'rb') as f:
        buf = f.read()
    sig = buf[:3].decode('ascii')
    d = None
    if sig == 'FWS':
        # uncompressed
        d = buf[8:]
    elif sig == 'CWS':
        d = zlib.decompress(buf[8:])
    elif sig == 'ZWS':
        d = lzma.decompress(buf[8:])
    if not d:
        print('unknown file format')
        return
    nbits = d[0]>>3
    bstr=''
    for i in range(32):
        bstr += '{:08b}'.format(d[i])
    bstr = bstr[5:]
    w = int( bstr[ nbits : 2*nbits], 2) # width in twips
    h = int (bstr[3*nbits: 4*nbits], 2)
    print("{} {} x {}".format(fname, int(w/20), int(h/20)))

def getdirswf(fname):
    flist = os.listdir(fname)
    flist = [x for x in flist if x.lower().endswith('.swf')]
    for f in flist:
        fn = os.path.join(fname, f)
        getswfdimension(fn)
#

if __name__ == "__main__":
    if len(sys.argv)>1:
        fname = sys.argv[1]
        if os.path.isfile(fname):
            getswfdimension(fname)
        elif os.path.isdir(fname):
            getdirswf(fname)
    else:
        print('swfdimension.py filename(or dirname)')

 

你可能感兴趣的:(Python)