-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode-vbi
executable file
·121 lines (98 loc) · 3.76 KB
/
decode-vbi
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
# Given a .tbc.json file, extract data from the VBI information in useful
# formats.
import argparse
import json
import re
import sys
from vbi import FieldInfo
def show_vbi(tbcjson):
"""Show the decoded VBI."""
for index, fieldjson in enumerate(tbcjson["fields"]):
fi = FieldInfo(fieldjson)
print(index, fi)
def ffmpeg_escape(s):
"""Escape strings for ffmpeg metadata files."""
return re.sub(r'[=;#\\\n]', lambda m: "\\" + m.group(0), s)
def make_metadata(tbcjson, metadata_fn):
"""Generate an ffmpeg metadata file with the chapter locations.
Format description: <https://ffmpeg.org/ffmpeg-formats.html#Metadata-1>"""
# We'll give positions using field indexes directly, rather than using the
# times encoded in the VBI, because we might be working with a capture
# of only part of a disc. Select the appropriate timebase to make this work.
if tbcjson["videoParameters"]["isSourcePal"]:
timebase = "1/50"
else:
timebase = "1001/60000"
# Scan through the VBI...
chapter_changes = []
stopcodes = set()
chapter = None
first_field_index = 0
for index, fieldjson in enumerate(tbcjson["fields"]):
# Codes may be in either field; we want the index of the first
if fieldjson["isFirstField"]:
first_field_index = index
fi = FieldInfo(fieldjson)
if fi.chapter is not None and fi.chapter != chapter:
# Chapter change
chapter = fi.chapter
chapter_changes.append((first_field_index, chapter))
if fi.stopcode:
# Stop code
stopcodes.add(first_field_index)
# Add a dummy change at the end of the input, so we can get the length of the last chapter
chapter_changes.append((len(tbcjson["fields"]), None))
# Because chapter markers have no error detection, a corrupt marker will
# result in a spurious chapter change. Remove suspiciously short chapters.
# XXX Do better when an error occurs near the start of a chapter
clean_changes = []
for i in range(len(chapter_changes) - 1):
change = chapter_changes[i]
next_change = chapter_changes[i + 1]
if (next_change[0] - change[0]) < 10:
# Too short - drop
pass
elif clean_changes != [] and change[1] == clean_changes[-1][1]:
# Change to the same chapter - drop
pass
else:
# Keep
clean_changes.append(change)
clean_changes.append(chapter_changes[-1])
chapter_changes = clean_changes
# Begin the file
if metadata_fn == "-":
f = sys.stdout
else:
f = open(metadata_fn, "w")
f.write(";FFMETADATA1\n")
# Write out the chapters
for i, change in enumerate(chapter_changes[:-1]):
f.write("\n")
f.write("[CHAPTER]\n")
f.write("TIMEBASE=%s\n" % timebase)
f.write("START=%d\n" % change[0])
f.write("END=%d\n" % (chapter_changes[i + 1][0] - 1))
f.write("title=%s\n" % ffmpeg_escape("Chapter %d" % change[1]))
# Write out the stop codes
# XXX Is there a way to represent these properly?
f.write("\n")
for index in sorted(stopcodes):
f.write("; Stop code at %d\n" % index)
if metadata_fn != "-":
f.close()
def main():
# Parse command-line options
parser = argparse.ArgumentParser()
parser.add_argument('jsonfile', type=str, help='JSON file containing VBI data')
parser.add_argument('-m', '--metadata', metavar='FILE', help='Generate ffmpeg metadata file')
args = parser.parse_args()
with open(args.jsonfile) as f:
tbcjson = json.load(f)
if args.metadata:
make_metadata(tbcjson, args.metadata)
else:
show_vbi(tbcjson)
if __name__ == "__main__":
main()