genY4M.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import argparse
  2. from os import listdir, path
  3. from PIL import Image
  4. import sys
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("--frame_path", default="../data/frame/", type=str)
  7. parser.add_argument("--frame_rate", default="25:1", type=str)
  8. parser.add_argument("--interlacing", default="Ip", type=str)
  9. parser.add_argument("--pix_ratio", default="0:0", type=str)
  10. parser.add_argument("--color_space", default="4:2:0", type=str)
  11. parser.add_argument("--output", default="output.y4m", type=str)
  12. def generate(args, frames):
  13. if len(frames) == 0:
  14. return
  15. #sort the frames based on the frame index
  16. frames = sorted(frames, key=lambda x: x[0])
  17. #convert the frames to YUV form
  18. frames = [f.convert("YCbCr") for _, f in frames]
  19. #write the header
  20. header = "YUV4MPEG2 W%d H%d F%s %s A%s" % (frames[0].width, frames[0].height,
  21. args.frame_rate, args.interlacing,
  22. args.pix_ratio)
  23. cs = args.color_space.split(":")
  24. header += " C%s%s%s\n" % (cs[0], cs[1], cs[2])
  25. #estimate the sample step based on subsample value
  26. subsamples = [int(c) for c in cs]
  27. r_step = [1, int(subsamples[2] == 0) + 1, int(subsamples[2] == 0) + 1]
  28. c_step = [1, 4 // subsamples[1], 4 // subsamples[1]]
  29. #write in frames
  30. with open(args.output, "wb") as y4m:
  31. y4m.write(header)
  32. for f in frames:
  33. y4m.write("FRAME\n")
  34. px = f.load()
  35. for k in xrange(3):
  36. for i in xrange(0, f.height, r_step[k]):
  37. for j in xrange(0, f.width, c_step[k]):
  38. yuv = px[j, i]
  39. y4m.write(chr(yuv[k]))
  40. if __name__ == "__main__":
  41. args = parser.parse_args()
  42. frames = []
  43. frames_mv = []
  44. for filename in listdir(args.frame_path):
  45. name, ext = filename.split(".")
  46. if ext == "png":
  47. name_parse = name.split("_")
  48. idx = int(name_parse[-1])
  49. img = Image.open(path.join(args.frame_path, filename))
  50. if name_parse[-2] == "mv":
  51. frames_mv.append((idx, img))
  52. else:
  53. frames.append((idx, img))
  54. if len(frames) == 0:
  55. print "No frames in directory: " + args.frame_path
  56. sys.exit()
  57. print("----------------------Y4M Info----------------------")
  58. print("width: %d" % frames[0][1].width)
  59. print("height: %d" % frames[0][1].height)
  60. print("#frame: %d" % len(frames))
  61. print("frame rate: %s" % args.frame_rate)
  62. print("interlacing: %s" % args.interlacing)
  63. print("pixel ratio: %s" % args.pix_ratio)
  64. print("color space: %s" % args.color_space)
  65. print("----------------------------------------------------")
  66. print("Generating ...")
  67. generate(args, frames)
  68. if len(frames_mv) != 0:
  69. args.output = args.output.replace(".y4m", "_mv.y4m")
  70. generate(args, frames_mv)