def test_mp3(path): fd = open(path, 'rb') f_mp3 = fd.read() fd.close() mp3 = MP3() mp3.from_bytes(f_mp3) mp3.reautomate() assert (mp3.to_bytes() == f_mp3)
def test_mp3(f_mp3=f_mp3): mp3 = MP3() mp3.from_bytes(f_mp3)
def test_perf_media(bmp_path, png_path, jpg_path, tiff_path, gif_path, mp4_path, mp3_path): fd = open(bmp_path, 'rb') f_bmp = fd.read() fd.close() def test_bmp(f_bmp=f_bmp, fmt=BMP): bmp = BMP() bmp.from_bytes(f_bmp) print('[+] instantiating and parsing BMP in LBUF mode') BMP._GEN = BMP._GEN_LBUF Ta = timeit(test_bmp, number=150) print('test_bmp: {0:.4f}'.format(Ta)) bmp = BMP() bmp.from_bytes(f_bmp) bmp.reautomate() print('[+] regenerating BMP in LBUF mode') Tb = timeit(bmp.to_bytes, number=200) print('bmp.to_bytes: {0:.4f}'.format(Tb)) print('[+] instantiating and parsing BMP in LPIX mode') BMP._GEN = BMP._GEN_LPIX Tc = timeit(test_bmp, number=3) print('test_bmp: {0:.4f}'.format(Tc)) bmp = BMP() bmp.from_bytes(f_bmp) bmp.reautomate() print('[+] regenerating BMP in LPIX mode') Td = timeit(bmp.to_bytes, number=2) print('bmp.to_bytes: {0:.4f}'.format(Td)) fd = open(png_path, 'rb') f_png = fd.read() fd.close() def test_png(f_png=f_png): png = PNG() png.from_bytes(f_png) print('[+] instantiating and parsing PNG') Te = timeit(test_png, number=120) print('test_png: {0:.4f}'.format(Te)) png = PNG() png.from_bytes(f_png) png.reautomate() print('[+] regenerating PNG') Tf = timeit(png.to_bytes, number=350) print('png.to_bytes: {0:.4f}'.format(Tf)) fd = open(jpg_path, 'rb') f_jpg = fd.read() fd.close() def test_jpg(f_jpg=f_jpg): jpeg = JPEG() jpeg.from_bytes(f_jpg) print('[+] instantiating and parsing JPEG') Tg = timeit(test_jpg, number=30) print('test_png: {0:.4f}'.format(Tg)) jpeg = JPEG() jpeg.from_bytes(f_jpg) jpeg.reautomate() print('[+] regenerating JPEG') Th = timeit(jpeg.to_bytes, number=60) print('jpeg.to_bytes: {0:.4f}'.format(Th)) fd = open(tiff_path, 'rb') f_tiff = fd.read() fd.close() def test_tiff(f_tiff=f_tiff): tiff = TIFF() tiff.from_bytes(f_tiff) print('[+] instantiating and parsing TIFF') Ti = timeit(test_tiff, number=60) print('test_tiff: {0:.4f}'.format(Ti)) tiff = TIFF() tiff.from_bytes(f_tiff) tiff.reautomate() print('[+] regenerating TIFF') Tj = timeit(tiff.to_bytes, number=240) print('tiff.to_bytes: {0:.4f}'.format(Tj)) fd = open(gif_path, 'rb') f_gif = fd.read() fd.close() def test_gif(f_gif=f_gif): gif = GIF() gif.from_bytes(f_gif) print('[+] instantiating and parsing GIF') Tk = timeit(test_gif, number=8) print('test_gif: {0:.4f}'.format(Tk)) gif = GIF() gif.from_bytes(f_gif) gif.reautomate() print('[+] regenerating GIF') Tl = timeit(gif.to_bytes, number=12) print('gif.to_bytes: {0:.4f}'.format(Tl)) fd = open(mp4_path, 'rb') f_mp4 = fd.read() fd.close() def test_mp4(f_mp4=f_mp4): mp4 = MPEG4() mp4.from_bytes(f_mp4) print('[+] instantiating and parsing MPEG4') Tm = timeit(test_mp4, number=12) print('test_mp4: {0:.4f}'.format(Tm)) mp4 = MPEG4() mp4.from_bytes(f_mp4) mp4.reautomate() print('[+] regenerating MPEG4') Tn = timeit(mp4.to_bytes, number=50) print('mp4.to_bytes: {0:.4f}'.format(Tn)) fd = open(mp3_path, 'rb') f_mp3 = fd.read() fd.close() def test_mp3(f_mp3=f_mp3): mp3 = MP3() mp3.from_bytes(f_mp3) print('[+] instantiating and parsing MP3') To = timeit(test_mp3, number=60) print('test_mp3: {0:.4f}'.format(To)) mp3 = MP3() mp3.from_bytes(f_mp3) mp3.reautomate() print('[+] regenerating MP3') Tp = timeit(mp3.to_bytes, number=180) print('mp3.to_bytes: {0:.4f}'.format(Tp)) print('[+] fmt_media total time: {0:.4f}'\ .format(Ta+Tb+Tc+Td+Te+Tf+Ti+Tj+Tk+Tl+Tm+Tn+To+Tp))
def main(): parser = argparse.ArgumentParser(description='print the internal structure of the input media file,'\ 'supported formats are: %s' % mediasup) parser.add_argument('input', type=str, help='input media file') parser.add_argument('-bl', type=int, default=1024, help='maximum length for buffer representation') parser.add_argument('-wt', action='store_true', default=False, help='show also absent / transparent fields') args = parser.parse_args() if not os.path.isfile(args.input): print('%s, args error: invalid input %s' % (sys.argv[0], args.input)) return 0 suf = args.input.split('.')[-1].upper() try: fd = open(args.input, 'rb') except: print('%s, args error: unable to open input %s' % (sys.argv[0], args.input)) return 0 else: buf = fd.read() fd.close() if suf == 'BMP': from pycrate_media.BMP import BMP struct = BMP() elif suf == 'GIF': from pycrate_media.GIF import GIF struct = GIF() elif suf in ('JPG', 'JPEG'): from pycrate_media.JPEG import JPEG struct = JPEG() elif suf == 'MP3': from pycrate_media.MP3 import MP3 struct = MP3() elif suf in ('MPEG4', 'MP4'): from pycrate_media.MPEG4 import MPEG4 struct = MPEG4() elif suf == 'PNG': from pycrate_media.PNG import PNG struct = PNG() elif suf in ('TIFF', 'TIF'): from pycrate_media.TIFF import TIFF struct = TIFF() else: print('%s, unknown format: %s' % (sys.argv[0], suf)) return 0 Buf.REPR_MAXLEN = args.bl try: struct.from_bytes(buf) except: print('%s, parsing error: unable to parse file %s' % (sys.argv[0], args.input)) return 0 # do not print absent / transparent fields if not args.wt: Element.ENV_SEL_TRANS = False print(struct.show()) return 0