-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDHRUtil-DVR-fileMetadata
executable file
·304 lines (265 loc) · 13 KB
/
HDHRUtil-DVR-fileMetadata
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
"""
HDHRUtil-DVR-fileMetadata
HDHRUtil-DVR-fileMetadata is a utility for manipulating
the HDHR DVR file metadata
Copyright (c) 2018 by Gary Buhrmaster <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===== NOTE ===== NOTE ===== NOTE ===== NOTE ===== NOTE =====
This utility currently uses a reverse engineered
schema definition to the file metadata. As it is
not documented as a stable ABI, it could change at
any time.
===== NOTE ===== NOTE ===== NOTE ===== NOTE ===== NOTE =====
"""
import sys
import json
import argparse
import re
import datetime
import dateutil
import dateutil.parser
import dateutil.tz
def episodeNumNormalize(episodeNum):
m0 = re.match(r'^S(\d+)E(\d+)$', episodeNum, re.IGNORECASE)
if m0:
return 'S{0:02}E{1:02}'.format(int(m0.group(1)), int(m0.group(2)))
else:
raise TypeError('Invalid episode number: {0}'.format(episodeNum))
def episodeNumCheck(episodeNum):
try:
return episodeNumNormalize(episodeNum)
except TypeError:
raise argparse.ArgumentTypeError('{} is not a valid episode number (SnnEnn)'.format(episodeNum))
def programIDCheck(programID):
if (len(programID) == 14) and (programID[0:2] in ['EP', 'MV', 'SH', 'SP']) and (re.match(r'^[0-9]+$', programID[2:])):
return programID
else:
raise argparse.ArgumentTypeError('{} is not a valid ProgramID (14 characters, starts with EP, MV, SH, SP'.format(programID))
def datetimeCheck(dt):
# Accept a epoch integer (presume the person knows for what they offer)
if re.match(r'^(\d+)$', dt):
return int(dt)
try:
d = dateutil.parser.parse(dt)
if d.tzinfo is None:
d = d.replace(tzinfo=dateutil.tz.tzlocal())
return int((d - datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)).total_seconds())
except:
raise argparse.ArgumentTypeError('{} is not a valid datetime'.format(dt))
def airdateCheck(dt):
# Accept a epoch integer (presume the person knows for what they offer)
if re.match(r'^(\d+)$', dt):
return int(dt)
try:
d = dateutil.parser.parse(dt)
if d.tzinfo is None:
d = d.replace(tzinfo=dateutil.tz.tzlocal())
dz = d.astimezone(tz=datetime.timezone.utc)
dm = datetime.datetime(dz.year, dz.month, dz.day, tzinfo=datetime.timezone.utc)
return int((dm - datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)).total_seconds())
except:
raise argparse.ArgumentTypeError('{} is not a valid datetime'.format(dt))
def channelNormalize(channel):
m0 = re.match(r'^(\d+)$', channel)
m1 = re.match(r'^(\d+)\.(\d+)$', channel)
m2 = re.match(r'^(\d+)_(\d+)$', channel)
m3 = re.match(r'^(\d+)-(\d+)$', channel)
if m0:
return '{}'.format(int(m0.group(1)))
elif m1:
return '{}.{}'.format(int(m1.group(1)), int(m1.group(2)))
elif m2:
return '{}.{}'.format(int(m2.group(1)), int(m2.group(2)))
elif m3:
return '{}.{}'.format(int(m3.group(1)), int(m3.group(2)))
raise TypeError('Invalid channel: {}'.format(channel))
def channelCheck(channel):
try:
return channelNormalize(channel)
except Exception:
raise argparse.ArgumentTypeError('{} is not a valid channel'.format(channel))
def transportStreamToMetadata(ts):
# Obtain metadata from transport stream
metadata = bytearray()
if len(ts) != 12032:
raise ValueError('transport stream too short for metadata')
for pkt in range(0, 64, 1):
if ts[pkt * 188 + 0] != 0x47:
raise ValueError('transport stream does not contain proper sync bytes')
if pkt == 0:
if ts[pkt * 188 + 1] != 0x5f:
raise ValueError('transport stream does not contain proper start payload and PID value bytes')
else:
if ts[pkt * 188 + 1] != 0x1f:
raise ValueError('transport stream does not contain proper payload and PID value bytes')
if ts[pkt * 188 + 2] != 0xfa:
raise ValueError('transport stream does not contain proper PID value')
if ts[pkt * 188 + 3] != 0x10 + pkt % 16:
raise ValueError('transport stream does not contain proper adaption and sequence bytes')
for c in range(4, 188, 1):
if ts[pkt * 188 + c] == 0xff:
break
metadata.append(ts[pkt * 188 + c])
if len(metadata) <= 0:
raise ValueError('transport stream does not contain any metadata')
try:
return json.loads(metadata)
except:
raise ValueError('transport stream metadata is not decodable as json')
def metadataToTransportStream(metadata):
# Create transport stream with metadata
TS = bytearray([0xff]*12032)
for pkt in range(0, 64, 1):
TS[pkt * 188 + 0] = 0x47 # sync byte
if pkt == 0:
TS[pkt * 188 + 1] = 0x5f # start + PID (0x1ffa)
else:
TS[pkt * 188 + 1] = 0x1f # PID (0x1ffa)
TS[pkt * 188 + 2] = 0xfa
TS[pkt * 188 + 3] = 0x10 + pkt % 16 # Scrambling (none) + Adaption (payload only) + sequence
if type(metadata) != dict:
raise TypeError('metadata is not a valid dict')
try:
meta = bytearray(json.dumps(metadata, separators=(',', ':')), 'utf-8')
except:
raise ValueError('metadata can not be converted to json')
if len(meta) > 11776:
raise ValueError('metadata json string exceeds allowable length')
for pkt in range(0, 64, 1):
l = min(len(meta), 184)
offset = pkt * 188 + 4
for c in range(0, l, 1):
TS[offset + c] = meta[c]
meta = meta[l:]
if len(meta) <= 0:
break
return TS
if __name__ == '__main__':
# Parse our args
parser = argparse.ArgumentParser()
parser.add_argument('--file', action='store', dest='file', required=True,
help='The file whose metadata will be manipulated')
actiongroup = parser.add_mutually_exclusive_group(required=False)
actiongroup.add_argument('--update', action='store_true', dest='update', default=False,
help='Update the file metadata')
actiongroup.add_argument('--set', action='store_true', dest='set', default=False,
help='Set the file metadata')
actiongroup.add_argument('--delete', nargs='+', dest='delete',
choices=['Category', 'ChannelAffiliate', 'ChannelImageURL',
'ChannelName', 'EndTime', 'EpisodeNumber', 'EpisodeTitle',
'FirstAiring', 'ImageURL', 'OriginalAirdate',
'ProgramID', 'RecordEndTime', 'RecordStartTime',
'RecordSuccess', 'Resume', 'SeriesID', 'StartTime',
'Synopsis', 'Team1', 'Team2', 'Title'],
help='Delete the specified keys in the file metadata')
parser.add_argument('--Category', action='store', dest='Category',
choices=['series', 'movie', 'news', 'sports', 'special', 'other'],
help='The program category (one of series, movie, news, sports, special, or other)')
parser.add_argument('--ChannelAffiliate', action='store', dest='ChannelAffiliate',
help='The program channel affiliate')
parser.add_argument('--ChannelImageURL', action='store', dest='ChannelImageURL',
help='The program channel image url')
parser.add_argument('--ChannelName', action='store', dest='ChannelName',
help='The program channel name')
parser.add_argument('--ChannelNumber', action='store', type=channelCheck, dest='ChannelNumber',
help='The program channel number')
parser.add_argument('--EndTime', action='store', type=datetimeCheck, dest='EndTime',
help='The program end time')
parser.add_argument('--EpisodeNumber', action='store', type=episodeNumCheck, dest='EpisodeNumber',
help='The program episode number (SnnEnn)')
parser.add_argument('--EpisodeTitle', action='store', dest='EpisodeTitle',
help='The program episode title')
parser.add_argument('--FirstAiring', action='store_const', const=1, dest='FirstAiring',
help='Mark the recording as a first airing')
parser.add_argument('--no-FirstAiring', action='store_const', const=0, dest='FirstAiring',
help='Mark the recording as not a first airing')
parser.add_argument('--ImageURL', action='store', dest='ImageURL',
help='The program image')
parser.add_argument('--OriginalAirdate', action='store', type=airdateCheck, dest='OriginalAirdate',
help='The program original air date')
parser.add_argument('--ProgramID', action='store', dest='ProgramID',
help='The program id')
parser.add_argument('--RecordEndTime', action='store', type=datetimeCheck, dest='RecordEndTime',
help='The program recording end time')
parser.add_argument('--RecordStartTime', action='store', type=datetimeCheck, dest='RecordStartTime',
help='The program recording start time')
parser.add_argument('--RecordSuccess', action='store_const', const=1, dest='RecordSuccess',
help='Mark the recording as successful')
parser.add_argument('--no-RecordSuccess', action='store_const', const=0, dest='RecordSuccess',
help='Mark the recording as not successful')
parser.add_argument('--Resume', action='store', dest='Resume', type=int,
help='The program resume point')
parser.add_argument('--SeriesID', action='store', dest='SeriesID',
help='The program series id')
parser.add_argument('--StartTime', action='store', type=datetimeCheck, dest='StartTime',
help='The program start time')
parser.add_argument('--Synopsis', action='store', dest='Synopsis',
help='The program synopsis')
parser.add_argument('--Title', action='store', dest='Title',
help='The program title')
parser.add_argument('--Team1', action='store', dest='Team1',
help='The first team (sports)')
parser.add_argument('--Team2', action='store', dest='Team2',
help='The second team (sports)')
args = parser.parse_args()
# Build metadata from args
metadata = {}
for m in ['Category', 'ChannelAffiliate', 'ChannelImageURL', 'ChannelName', 'ChannelNumber',
'EndTime', 'EpisodeNumber', 'EpisodeTitle', 'FirstAiring', 'ImageURL',
'OriginalAirdate', 'ProgramID', 'RecordEndTime', 'RecordStartTime', 'RecordSuccess',
'Resume', 'SeriesID', 'StartTime', 'Synopsis', 'Team1', 'Team2', 'Title']:
value = vars(args)[m]
if value is not None:
metadata[m] = value
# Open file
openmode = 'rb'
if args.set or args.update or args.delete:
openmode = 'r+b'
try:
ffile = open(args.file, openmode)
except (OSError, IOError) as e:
print('Unable to open input file: ' + str(e))
sys.exit(1)
# Check metadata at front
ffile.seek(0, 0)
pkts = ffile.read(12032)
try:
fileMetadata = transportStreamToMetadata(pkts)
except (ValueError, TypeError) as e:
print('File {} does not appear to contain valid metadata at the start of the file: {}'.format(args.file, str(e)))
sys.exit(1)
# Perform the request
if (not args.set) and (not args.update) and (not args.delete):
if metadata:
print('WARNING: Metadata values specified ignored, did you mean to --update or --set?')
print(json.dumps(fileMetadata, sort_keys=True, indent=4))
else:
if args.update:
fileMetadata.update(metadata)
elif args.set:
fileMetadata = metadata
elif args.delete:
for key in args.delete:
try:
del fileMetadata[key]
except KeyError:
pass
try:
ts = metadataToTransportStream(fileMetadata)
except (ValueError, TypeError) as e:
print('Unable to convert specified metadata to transport stream: {}'.format(str(e)))
sys.exit(1)
ffile.seek(0, 0)
ffile.write(ts)
ffile.seek(0, 2)
ffile.close()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4