#!/usr/bin/env php
<?php

// Load options
if (count($argv) != 5) {
  echo "Usage: makehdr <frames> <fps> <Hz> <basename>\n";
  return 1;
}
$name = array_shift($argv);
$frames = array_shift($argv);
$fps = array_shift($argv);
$hz = array_shift($argv);
$base = array_shift($argv);

$asize = filesize(sprintf("%s.mp2", $base));
$vsize = filesize(sprintf("%s.m1v", $base));
$gsize = filesize(sprintf("%s.gop", $base));

if (!$asize || !$vsize || !$gsize) {
  printf("Make sure you have %s.{mp2,m1v,gop} in the current directory!\n", $base);
  return 1;
}

$astart = 36 + 12;
$vstart = $astart + $asize;
$gstart = $vstart + $vsize;

$hdr = sprintf("DPG2%s", pack('lnnlllllllll', $frames, $fps, 0, $hz, 0, $astart, $asize, $vstart, $vsize, $gstart, $gsize, 3));

file_put_contents(sprintf("%s.hdr", $base), $hdr);

printf("F:%u FPS:%u Hz:%u as:%u al:%u vs:%u vl:%u gs:%u gl:%u\n", $frames, $fps, $hz, $astart, $asize, $vstart, $vsize, $gstart, $gsize);
printf("%s.hdr: %u bytes written\n", $base, strlen($hdr));

?>