Skip to content

Instantly share code, notes, and snippets.

@metajack
Created May 13, 2015 01:34
Show Gist options
  • Save metajack/bb541f11fca1c0b8b9cf to your computer and use it in GitHub Desktop.
Save metajack/bb541f11fca1c0b8b9cf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import re
Y4M_HEADER_RE = re.compile("YUV4MPEG2 W([0-9]+) H([0-9]+) F([:0-9]+) .*")
def parse_header(header):
print(header)
m = Y4M_HEADER_RE.match(header)
return (int(m.group(1)), int(m.group(2)), m.group(3))
left_video = open(sys.argv[1], "rb")
right_video = open(sys.argv[2], "rb")
left_sections = left_video.read().split("FRAME\n")
right_sections = right_video.read().split("FRAME\n")
(left_w, left_h, left_f) = parse_header(left_sections.pop(0))
(right_w, right_h, right_f) = parse_header(right_sections.pop(0))
assert left_w == right_w
assert left_h == right_h
assert left_f == right_f
assert len(left_sections) == len(right_sections)
output_video = open(sys.argv[3], "wb")
output_video.write("YUV4MPEG2 W%d H%d F%s Ip A0:0 C420jpeg\n" % (left_w * 2, left_h, left_f))
for frame in range(len(left_sections)):
output_video.write("FRAME\n")
# Y
width = left_w
for j in range(left_h):
start = j * width
stop = (j + 1) * width
output_video.write(left_sections[frame][start:stop])
output_video.write(right_sections[frame][start:stop])
# Cb
Y_len = left_w * left_h
width = width / 2
for j in range(left_h / 2):
start = j * width + Y_len
stop = (j + 1) * width + Y_len
output_video.write(left_sections[frame][start:stop])
output_video.write(right_sections[frame][start:stop])
# Cr
for j in range(left_h / 2):
start = j * width + Y_len
stop = (j + 1) * width + Y_len
output_video.write(left_sections[frame][start:stop])
output_video.write(right_sections[frame][start:stop])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment