Loading scripts/cut_bs.py +38 −10 Original line number Diff line number Diff line Loading @@ -39,7 +39,28 @@ SID_BITS = {35, 48, 104} SYNC_WORDS = {b"!k", b" k"} def cut_bs(fp, fp_out, start_frame=0, start_with_sid=False): def cut_to_length(fp, fp_out, length): assert length > 0 fr_cnt = 0 for f in range(length): sync_word = fp.read(2) if sync_word == b"": return (fr_cnt, cut_cnt) if sync_word not in SYNC_WORDS: raise ValueError("Bad Sync word!") n_bits_bs = fp.read(2) n_bits = struct.unpack("h", n_bits_bs)[0] fp_out.write(sync_word) fp_out.write(n_bits_bs) fp_out.write(fp.read(n_bits * 2)) fr_cnt += 1 return fr_cnt def cut_from_start(fp, fp_out, start_frame=0, start_with_sid=False): # cut until start frame fr_cnt = 0 cut_cnt = 0 Loading Loading @@ -92,14 +113,15 @@ def cut_bs(fp, fp_out, start_frame=0, start_with_sid=False): if __name__ == "__main__": my_parser = argparse.ArgumentParser( description="Cut frames from the beginning of a G.192 bit stream file" description="Cut a G.192 bit stream file" ) my_parser.add_argument( "--sid", "-s", help="Cut until the first SID frame", action="store_true" "--sid", "-s", help="Cut away all frames before the first SID frame", action="store_true" ) my_parser.add_argument( "--frame", "-f", type=int, help="Number of frames to cut.", default=0 "--frame", "-f", type=int, help="Number of frames to cut from the start of the file.", default=0 ) my_parser.add_argument("--length", "-l", type=int, help="Cut bitstream to this length (in frames)", default=-1) my_parser.add_argument("bs_in", type=str, help="G.192 bit stream file name to cut") my_parser.add_argument("bs_out", type=str, help="Cut G.192 bit stream file name") my_args = my_parser.parse_args() Loading @@ -113,10 +135,16 @@ if __name__ == "__main__": with open(my_args.bs_in, "rb") as fp_in: with open(my_args.bs_out, "wb") as fp_out: fr_cnt, cut_cnt = cut_bs( if my_args.sid or my_args.frame: fr_cnt, cut_cnt = cut_from_start( fp_in, fp_out, start_frame=my_args.frame, start_with_sid=my_args.sid ) if my_args.sid and (fr_cnt == cut_cnt): print("Warning! No SID frame found in bitstream!") print(f"Cut {cut_cnt} of {fr_cnt} frames from {my_args.bs_in}") elif my_args.length: fr_cnt = cut_to_length(fp_in, fp_out, my_args.length) if fr_cnt != my_args.length: print(f"Warning! Could not cut to length {my_args.length} as bitstream only contained {fr_cnt} frames!") print(f"Cut {my_args.bs_in} to {fr_cnt} frames") Loading
scripts/cut_bs.py +38 −10 Original line number Diff line number Diff line Loading @@ -39,7 +39,28 @@ SID_BITS = {35, 48, 104} SYNC_WORDS = {b"!k", b" k"} def cut_bs(fp, fp_out, start_frame=0, start_with_sid=False): def cut_to_length(fp, fp_out, length): assert length > 0 fr_cnt = 0 for f in range(length): sync_word = fp.read(2) if sync_word == b"": return (fr_cnt, cut_cnt) if sync_word not in SYNC_WORDS: raise ValueError("Bad Sync word!") n_bits_bs = fp.read(2) n_bits = struct.unpack("h", n_bits_bs)[0] fp_out.write(sync_word) fp_out.write(n_bits_bs) fp_out.write(fp.read(n_bits * 2)) fr_cnt += 1 return fr_cnt def cut_from_start(fp, fp_out, start_frame=0, start_with_sid=False): # cut until start frame fr_cnt = 0 cut_cnt = 0 Loading Loading @@ -92,14 +113,15 @@ def cut_bs(fp, fp_out, start_frame=0, start_with_sid=False): if __name__ == "__main__": my_parser = argparse.ArgumentParser( description="Cut frames from the beginning of a G.192 bit stream file" description="Cut a G.192 bit stream file" ) my_parser.add_argument( "--sid", "-s", help="Cut until the first SID frame", action="store_true" "--sid", "-s", help="Cut away all frames before the first SID frame", action="store_true" ) my_parser.add_argument( "--frame", "-f", type=int, help="Number of frames to cut.", default=0 "--frame", "-f", type=int, help="Number of frames to cut from the start of the file.", default=0 ) my_parser.add_argument("--length", "-l", type=int, help="Cut bitstream to this length (in frames)", default=-1) my_parser.add_argument("bs_in", type=str, help="G.192 bit stream file name to cut") my_parser.add_argument("bs_out", type=str, help="Cut G.192 bit stream file name") my_args = my_parser.parse_args() Loading @@ -113,10 +135,16 @@ if __name__ == "__main__": with open(my_args.bs_in, "rb") as fp_in: with open(my_args.bs_out, "wb") as fp_out: fr_cnt, cut_cnt = cut_bs( if my_args.sid or my_args.frame: fr_cnt, cut_cnt = cut_from_start( fp_in, fp_out, start_frame=my_args.frame, start_with_sid=my_args.sid ) if my_args.sid and (fr_cnt == cut_cnt): print("Warning! No SID frame found in bitstream!") print(f"Cut {cut_cnt} of {fr_cnt} frames from {my_args.bs_in}") elif my_args.length: fr_cnt = cut_to_length(fp_in, fp_out, my_args.length) if fr_cnt != my_args.length: print(f"Warning! Could not cut to length {my_args.length} as bitstream only contained {fr_cnt} frames!") print(f"Cut {my_args.bs_in} to {fr_cnt} frames")