-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
35 lines (26 loc) · 896 Bytes
/
extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 5 10:48:54 2019
Converts metadata from H.264 files to .json
@author: remy.dheygere
"""
import re
import json
import sys
import os.path
try:
if os.path.isfile(sys.argv[1]):
file = sys.argv[1]
except Exception as e:
print("Script requires h264 file as argument.")
sys.exit()
with open(file, 'rb') as f: # rb read binary
data = f.read()
result = re.findall(b'(?<=\xff\xff\xff\xff\xff)\{.+?(?=\x00\x00\x00\x01)', data)
result = [str(part)[2:-1] for part in result] # trim the b'' around every string
print(len(result), "strings found!")
regularjson = "[" + ",\n".join(result) + "]"
prettyjson = json.loads(regularjson)
with open(os.path.splitext(file)[0] + '_output.json', 'w') as joutput:
#joutput.write(regularjson) # compact, on one line
joutput.write(json.dumps(prettyjson, indent=4))