From f6d4e06c3452539e07f0304638c4b9a77137ddf3 Mon Sep 17 00:00:00 2001 From: 910JQK Date: Sun, 6 Oct 2013 14:30:26 +0800 Subject: [PATCH] Add sH5V sH5V project: https://github.com/910JQK/sH5V This commit merges pull request #1 --- danmaku2ass.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/danmaku2ass.py b/danmaku2ass.py index d7b0900..ac27058 100755 --- a/danmaku2ass.py +++ b/danmaku2ass.py @@ -91,6 +91,13 @@ def ProbeCommentFormat(f): if tmp == '[': f.seek(0) return 'Acfun' + elif tmp == '{': + tmp = f.read(17) + f.seek(0) + if tmp == '"root":{"total":"': + return 'sH5V' + else: + return None elif tmp == '<': tmp = f.read(39) f.seek(0) @@ -172,6 +179,26 @@ def ReadCommentsBilibili(f, fontsize): continue +def ReadCommentsSH5V (f, fontsize): + 'Output format: [(timeline, timestamp, no, comment, pos, color, size, height, width)]' + comment_element = json.load(f) + i = 0 + for comment in comment_element["root"]["bgs"] : + try: + c_at = str(comment['at']) + c_type = str(comment['type']) + c_date = str(comment['timestamp']) + c_color = str(comment['color']) + c = str(comment['text']) + size = fontsize + #print(c_at,' ',round(float(c_at),2),' ',c) + yield (float(c_at), int(c_date), i, c, {'0': 0, '1': 0, '4': 2, '5': 1}[c_type], int(c_color[1:],16), size, (c.count('\n')+1)*size, CalculateLength(c)*size) + i += 1 + except (AssertionError, AttributeError, IndexError, TypeError, ValueError): + logging.warning(_('Invalid comment: %r') % comment) + continue + + def ConvertTimestamp(timestamp): hour, minute = divmod(timestamp, 3600) minute, second = divmod(minute, 60) @@ -232,7 +259,7 @@ if __name__ == '__main__': comments = [] for i in args.file: with open(i, 'r', encoding='utf-8') as f: - CommentProcesser = {None: None, 'Niconico': ReadCommentsNiconico, 'Acfun': ReadCommentsAcfun, 'Bilibili': ReadCommentsBilibili}[ProbeCommentFormat(f)] + CommentProcesser = {None: None, 'Niconico': ReadCommentsNiconico, 'Acfun': ReadCommentsAcfun, 'Bilibili': ReadCommentsBilibili, 'sH5V': ReadCommentsSH5V}[ProbeCommentFormat(f)] if not CommentProcesser: raise ValueError(_('Unknown comment file format: %s') % i) for comment in CommentProcesser(f, args.fontsize):