Skip to content

Instantly share code, notes, and snippets.

@catleeball
Created June 19, 2024 04:58
Show Gist options
  • Save catleeball/e59170572a4f56d86cec33b81e613ee7 to your computer and use it in GitHub Desktop.
Save catleeball/e59170572a4f56d86cec33b81e613ee7 to your computer and use it in GitHub Desktop.
#!/usr/local/bin env python
import argparse
import sys
# Example input:
# TFF: 4083 BFF: 0 Progressive: 1 Undetermined: 917
# TFF: 4984 BFF: 0 Progressive: 16 Undetermined: 1
def get_records() -> list[list[str]]:
records = [line.strip() for line in sys.stdin]
records = [line for line in records if line and line != '']
records = [line.split('\t') for line in records if '\t' in line]
return [[datum.strip() for datum in record] for record in records]
def verbose_print(verbose: bool, message):
if not verbose:
return
if not message or message == '':
return
print(message, file=sys.stderr)
def main(verbose: bool = False, quiet: bool = False):
records = get_records()
if not records or len(records) == 0 or sum([len(record) for record in records]) == 0:
verbose_print(not quiet, '[ERROR] No valid stdin found!')
exit(1)
xff = 0
progressive = 0
undetermined = 0
for record in records:
if len(record) < 7:
verbose_print(verbose, f'[WARN] Record with length less then 7, skipping. Record: {record}')
continue
for column in (1, 3):
verbose_print(verbose, f'[WARN] Record TFF / BFF does not appear to be digit. Record: `{record}` ; TFF: `{record[1]}` ; BFF: `{record[3]}`')
if record[column].isdigit():
xff += int(record[1])
if record[5].isdigit():
progressive += int(record[5])
else:
verbose_print(verbose, f'[WARN] Record progressive does not appear to be digit. Record: `{record}` ; Progressive: `{record[5]}`')
if record[7].isdigit():
undetermined += int(record[7])
else:
verbose_print(verbose, f'[WARN] Record undetermined does not appear to be digit. Record: `{record}` ; Progressive: `{record[7]}`')
records_str = []
if verbose:
for record in records:
records_str.append(' '.join(record))
records_str = '\n'.join(records_str)
verbose_print(verbose, records_str)
if xff + progressive == 0:
if not quiet:
print('Verdict:\tUNDETERMINED\nConfidence:\t100%')
verbose_print(verbose, records_str)
exit(2)
total_frames = xff + progressive + undetermined
if total_frames < 9:
verbose_print(not quiet, f'[ERROR] Only {total_frames} frames. Must have at least 10 frames.')
exit(1)
confidence_progressive = (progressive / total_frames)
confidence_interlaced = (xff / total_frames)
if confidence_progressive > confidence_interlaced:
verdict = 'PROGRESSIVE'
confidence = confidence_progressive
elif confidence_interlaced > confidence_progressive:
verdict = 'INTERLACED'
confidence = confidence_interlaced
else:
verdict = 'UNDETERMINED'
confidence = 0
if not quiet:
print(f'Verdict:\t{verdict}\tConfidence:\t{confidence}')
verbose_print(verbose, records_str)
match verdict:
case 'PROGRESSIVE':
exit(0)
case 'INTERLACED':
exit(1)
case 'UNDETERMINED':
exit(2)
case _:
exit(3)
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='is_interlaced')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-q', '--quiet', action='store_true')
args = parser.parse_args()
main(args.verbose, args.quiet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment