import glob import json import textwrap import sys import os from collections import defaultdict def json_per_row_to_json(fpath = '/home/thebears/Videos/Winslow/saved/marks'): fid = open(fpath,'r') d = list() for x in fid: d.append(json.loads(x)) json.dump(d,open(fpath+'.json','w'),indent=4) def is_ipython(): import __main__ as main_ff if hasattr(main_ff,'__file__'): return False else: return True def exit_if_not_ipython(): import __main__ as main_ff if hasattr(main_ff,'__file__'): print('Running in non-interactive mode, exiting...') sys.exit() else: print('Running in interactive mode, not exiting...') def append_json_no_file_read(obj_append, jpath = '/home/thebears/Videos/Winslow/saved/marks.json'): fid = open(jpath,'r+') fid.seek(0,2) position = fid.tell() -2 fid.seek(position) fid.write(",\n") fid.write(textwrap.indent(json.dumps(obj_append, indent=4),' ')) fid.write('\n]') fid.close() def get_cset_match(input_in): if not os.path.exists(input_in): return {} if os.path.isdir(input_in): return get_cset_match_dir(input_in) else: dir_list = os.path.dirname(input_in) summ = get_cset_match_dir(dir_list) name, ext = os.path.splitext(input_in) tstamp = int(name.replace('_trimmed','').split('_')[-1]) return summ[tstamp] def get_cset_for_file_matching(file_in): name, ext = os.path.splitext(file_in) cset = dict() files = glob.glob(name+"*") for f in files: [f_name, f_ext ] =f.split(".",1) cset['.'+f_ext] = f return cset def get_cset_match_dir(rtdir): summary = dict() other_files = dict() for f in os.walk(rtdir): c_rt = f[0] c_files = f[2] for c_file in c_files: name, ext = os.path.splitext(c_file) try: tstamp = int(name.replace('_trimmed','').split('_')[-1]) if ext in {'.mp4'}: summary[tstamp] = dict() summary[tstamp][ext] = os.path.join(c_rt, c_file) else: if tstamp not in other_files: other_files[tstamp] = dict() other_files[tstamp][ext] = os.path.join(c_rt, c_file) except: pass mp4_tstamps = sorted(summary) other_tstamps = sorted(other_files) idx_other = 0 merge_map = defaultdict(list) merged = 0 for idx_mp4, m_t in enumerate(mp4_tstamps): # for idx_o_test in range(len(other_tstamps))) for idx_o_test, o_t in enumerate(other_tstamps): if abs(m_t - o_t) < 25: merge_map[m_t].append(o_t) for k, v in merge_map.items(): for v_join in v: summary[k].update(other_files[v_join]) return summary