support filters in json

This commit is contained in:
Chaigidel 2020-02-24 18:43:40 +08:00
parent ed1694999c
commit bb8f1cee06

View File

@ -512,7 +512,7 @@ def ConvertFlashRotation(rotY, rotZ, X, Y, width, height):
return (trX, trY, WrapAngle(outX), WrapAngle(outY), WrapAngle(outZ), scaleXY * 100, scaleXY * 100) return (trX, trY, WrapAngle(outX), WrapAngle(outY), WrapAngle(outZ), scaleXY * 100, scaleXY * 100)
def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsize, alpha, duration_marquee, duration_still, filter_regex, reduced, progress_callback): def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsize, alpha, duration_marquee, duration_still, filters_regex, reduced, progress_callback):
styleid = 'Danmaku2ASS_%04x' % random.randint(0, 0xffff) styleid = 'Danmaku2ASS_%04x' % random.randint(0, 0xffff)
WriteASSHead(f, width, height, fontface, fontsize, alpha, styleid) WriteASSHead(f, width, height, fontface, fontsize, alpha, styleid)
rows = [[None] * (height - bottomReserved + 1) for i in range(4)] rows = [[None] * (height - bottomReserved + 1) for i in range(4)]
@ -520,7 +520,12 @@ def ProcessComments(comments, f, width, height, bottomReserved, fontface, fontsi
if progress_callback and idx % 1000 == 0: if progress_callback and idx % 1000 == 0:
progress_callback(idx, len(comments)) progress_callback(idx, len(comments))
if isinstance(i[4], int): if isinstance(i[4], int):
skip = False
for filter_regex in filters_regex:
if filter_regex and filter_regex.search(i[3]): if filter_regex and filter_regex.search(i[3]):
skip = True
break
if skip:
continue continue
row = 0 row = 0
rowmax = height - bottomReserved - i[7] rowmax = height - bottomReserved - i[7]
@ -726,12 +731,16 @@ def export(func):
@export @export
def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_height, reserve_blank=0, font_face=_('(FONT) sans-serif')[7:], font_size=25.0, text_opacity=1.0, duration_marquee=5.0, duration_still=5.0, comment_filter=None, is_reduce_comments=False, progress_callback=None): def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_height, reserve_blank=0, font_face=_('(FONT) sans-serif')[7:], font_size=25.0, text_opacity=1.0, duration_marquee=5.0, duration_still=5.0, comment_filter=None, comment_filters_json=None, is_reduce_comments=False, progress_callback=None):
comment_filters = [comment_filter]
if comment_filters_json:
with open(comment_filters_json, 'r') as f:
comment_filters.extend(json.load(f))
filters_regex = []
for comment_filter in comment_filters:
try: try:
if comment_filter: if comment_filter:
filter_regex = re.compile(comment_filter) filters_regex.append(re.compile(comment_filter))
else:
filter_regex = None
except: except:
raise ValueError(_('Invalid regular expression: %s') % comment_filter) raise ValueError(_('Invalid regular expression: %s') % comment_filter)
fo = None fo = None
@ -741,7 +750,7 @@ def Danmaku2ASS(input_files, input_format, output_file, stage_width, stage_heigh
fo = ConvertToFile(output_file, 'w', encoding='utf-8-sig', errors='replace', newline='\r\n') fo = ConvertToFile(output_file, 'w', encoding='utf-8-sig', errors='replace', newline='\r\n')
else: else:
fo = sys.stdout fo = sys.stdout
ProcessComments(comments, fo, stage_width, stage_height, reserve_blank, font_face, font_size, text_opacity, duration_marquee, duration_still, filter_regex, is_reduce_comments, progress_callback) ProcessComments(comments, fo, stage_width, stage_height, reserve_blank, font_face, font_size, text_opacity, duration_marquee, duration_still, filters_regex, is_reduce_comments, progress_callback)
finally: finally:
if output_file and fo != output_file: if output_file and fo != output_file:
fo.close() fo.close()
@ -800,6 +809,7 @@ def main():
parser.add_argument('-dm', '--duration-marquee', metavar=_('SECONDS'), help=_('Duration of scrolling comment display [default: %s]') % 5, type=float, default=5.0) parser.add_argument('-dm', '--duration-marquee', metavar=_('SECONDS'), help=_('Duration of scrolling comment display [default: %s]') % 5, type=float, default=5.0)
parser.add_argument('-ds', '--duration-still', metavar=_('SECONDS'), help=_('Duration of still comment display [default: %s]') % 5, type=float, default=5.0) parser.add_argument('-ds', '--duration-still', metavar=_('SECONDS'), help=_('Duration of still comment display [default: %s]') % 5, type=float, default=5.0)
parser.add_argument('-fl', '--filter', help=_('Regular expression to filter comments')) parser.add_argument('-fl', '--filter', help=_('Regular expression to filter comments'))
parser.add_argument('-flf', '--filter-file', help=_('Regular expressions from json file to filter comments'))
parser.add_argument('-p', '--protect', metavar=_('HEIGHT'), help=_('Reserve blank on the bottom of the stage'), type=int, default=0) parser.add_argument('-p', '--protect', metavar=_('HEIGHT'), help=_('Reserve blank on the bottom of the stage'), type=int, default=0)
parser.add_argument('-r', '--reduce', action='store_true', help=_('Reduce the amount of comments if stage is full')) parser.add_argument('-r', '--reduce', action='store_true', help=_('Reduce the amount of comments if stage is full'))
parser.add_argument('file', metavar=_('FILE'), nargs='+', help=_('Comment file to be processed')) parser.add_argument('file', metavar=_('FILE'), nargs='+', help=_('Comment file to be processed'))
@ -810,7 +820,7 @@ def main():
height = int(height) height = int(height)
except ValueError: except ValueError:
raise ValueError(_('Invalid stage size: %r') % args.size) raise ValueError(_('Invalid stage size: %r') % args.size)
Danmaku2ASS(args.file, args.format, args.output, width, height, args.protect, args.font, args.fontsize, args.alpha, args.duration_marquee, args.duration_still, args.filter, args.reduce) Danmaku2ASS(args.file, args.format, args.output, width, height, args.protect, args.font, args.fontsize, args.alpha, args.duration_marquee, args.duration_still, args.filter, args.filter_file, args.reduce)
if __name__ == '__main__': if __name__ == '__main__':