Skip to content

Commit

Permalink
Simplify code for initializing parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 2, 2024
1 parent 857bf20 commit 9d4e3b7
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions src/main/php/de/thekid/dialog/processing/Atoms.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,48 @@

/** @see https://developer.apple.com/documentation/quicktime-file-format/movie_atoms */
class Atoms {
private $parsers;
private $parsers= [
'moov.mvhd' => function($f, $atom) {

public function __construct() {
$this->parsers= [
'moov.mvhd' => function($f, $atom) {

// See https://developer.apple.com/documentation/quicktime-file-format/movie_header_atom
return unpack('cversion/c3flags/Ncreated/Nmodified/Nscale/Nduration', $f->read(20));
},
'moov.meta.keys' => function($f, $atom) {
$r= [];
$info= unpack('cversion/c3flags/Ncount', $f->read(8));
for ($i= 0; $i < $info['count']; $i++) {
$entry= unpack('Nsize/a4ns', $f->read(8));
$r[$i + 1]= $entry['ns'].':'.$f->read($entry['size'] - 8);
}
return $r;
},
'moov.meta.ilst' => function($f, $atom) {
$r= [];
foreach ($this->atoms($f, $atom) as $child) {
$index= unpack('N', $child['name'])[1];
$r[$index]= [];
foreach ($this->atoms($f, $child) as $data) {
$entry= unpack('Ntype/a4locale', $f->read(8));
$bytes= $f->read($data['length'] - 16);
// See https://developer.apple.com/documentation/quicktime-file-format/movie_header_atom
return unpack('cversion/c3flags/Ncreated/Nmodified/Nscale/Nduration', $f->read(20));
},
'moov.meta.keys' => function($f, $atom) {
$r= [];
$info= unpack('cversion/c3flags/Ncount', $f->read(8));
for ($i= 0; $i < $info['count']; $i++) {
$entry= unpack('Nsize/a4ns', $f->read(8));
$r[$i + 1]= $entry['ns'].':'.$f->read($entry['size'] - 8);
}
return $r;
},
'moov.meta.ilst' => function($f, $atom) {
$r= [];
foreach ($this->atoms($f, $atom) as $child) {
$index= unpack('N', $child['name'])[1];
$r[$index]= [];
foreach ($this->atoms($f, $child) as $data) {
$entry= unpack('Ntype/a4locale', $f->read(8));
$bytes= $f->read($data['length'] - 16);

// See https://developer.apple.com/documentation/quicktime-file-format/value_atom
$r[$index][]= match ($entry['type']) {
1 => $bytes, // yield utf-8 as-is
default => new Bytes($bytes)
};
}
// See https://developer.apple.com/documentation/quicktime-file-format/value_atom
$r[$index][]= match ($entry['type']) {
1 => $bytes, // yield utf-8 as-is
default => new Bytes($bytes)
};
}
return $r;
},
"moov.udta.\251xyz" => function($f, $atom) {
$entry= unpack('nsize/ntype', $f->read(4));
preg_match_all('/[+-][0-9.]+/', $f->read($entry['size'] - 1), $c);
return $c[0];
},
'moov.udta.*' => function($f, $atom) {
return new Data($f, $atom['offset'] + 8, $atom['length'] - 8);
},
];
}
}
return $r;
},
"moov.udta.\251xyz" => function($f, $atom) {
$entry= unpack('nsize/ntype', $f->read(4));
preg_match_all('/[+-][0-9.]+/', $f->read($entry['size'] - 1), $c);
return $c[0];
},
'moov.udta.*' => function($f, $atom) {
return new Data($f, $atom['offset'] + 8, $atom['length'] - 8);
},
];

private function atom($f, $base= null) {
$atom= ['offset' => $f->tell(), 'path' => $base] + unpack('Nlength/a4name', $f->read(8));
Expand Down

0 comments on commit 9d4e3b7

Please sign in to comment.