Media Metadata Inspector — PHP source
Inspect audio, video and image files at the byte level — MP4 boxes, ID3 tags, WAV chunks, GIF headers — parsed by our own readers, entirely in your browser.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
/**
* media-metadata-inspector — read container metadata straight from the bytes.
*
* Language: PHP 7.4+ (standard library only).
* CosmoDev polyglot showcase port of the `media-metadata-inspector` tool,
* ported from src/lib/media-meta.ts (the canonical TypeScript implementation).
*
* PHP strings are raw byte buffers, so substr/ord/unpack give direct byte
* access with no copies. The one substitution: TextDecoder becomes iconv
* ('//IGNORE' drops invalid UTF-8 instead of substituting U+FFFD — a lenient
* decoder either way, and real-world ID3 frames are clean ASCII/UTF-8).
*
* display source — part of CosmoDev's polyglot tool pages.
*/
declare(strict_types=1);
/**
* Probe result. Null fields mean "container parsed, field not present".
*
* @property string $kind 'mp4'|'mp3'|'wav'|'gif'|'webm'|'unknown'
*/
final class MediaMeta
{
public string $kind;
public ?float $durationSec = null;
public ?float $width = null;
public ?float $height = null;
public ?string $title = null;
public ?string $artist = null;
public ?string $album = null;
public function __construct(string $kind)
{
$this->kind = $kind;
}
}
/** Big-endian u32 (MP4 / ID3); 0 past the buffer, mirroring the lib's reads. */
function mmU32be(string $b, int $o): int
{
return strlen($b) < $o + 4 ? 0 : unpack('N', substr($b, $o, 4))[1];
}
/** Little-endian u32 (RIFF/WAV). */
function mmU32le(string $b, int $o): int
{
return strlen($b) < $o + 4 ? 0 : unpack('V', substr($b, $o, 4))[1];
}
/** Little-endian u16 (GIF dimensions). */
function mmU16le(string $b, int $o): int
{
return strlen($b) < $o + 2 ? 0 : unpack('v', substr($b, $o, 2))[1];
}
/** ID3v2 syncsafe integer: four 7-bit groups, no byte sets the high bit. */
function mmSyncsafe(string $b, int $o): int
{
return (ord($b[$o]) << 21) | (ord($b[$o + 1]) << 14) | (ord($b[$o + 2]) << 7) | ord($b[$o + 3]);
}
/** MP4 box walk: payload ranges [start, end) for every box of $wanted. */
function mmFindBoxes(string $b, int $start, int $end, string $wanted): array
{
$found = [];
$pos = $start;
while ($pos + 8 <= $end) {
$size = mmU32be($b, $pos);
if ($size === 1) {
return $found; // 64-bit sizes unsupported — stop safely.
}
if ($size === 0) {
$size = $end - $pos; // size 0: box extends to the end
}
if ($size < 8 || $pos + $size > $end) {
return $found;
}
if (substr($b, $pos + 4, 4) === $wanted) {
$found[] = [$pos + 8, $pos + $size];
}
$pos += $size;
}
return $found;
}
/** MP4: moov -> mvhd gives duration; the first trak's tkhd gives dimensions. */
function mmProbeMp4(string $b): MediaMeta
{
$meta = new MediaMeta('mp4');
foreach (mmFindBoxes($b, 0, strlen($b), 'moov') as [$ms, $me]) {
foreach (mmFindBoxes($b, $ms, $me, 'mvhd') as [$s, $e]) {
$version = ord($b[$s]);
if ($version === 0 && $e - $s >= 20) {
$timescale = mmU32be($b, $s + 12);
if ($timescale > 0) {
$meta->durationSec = mmU32be($b, $s + 16) / $timescale;
}
} elseif ($version === 1 && $e - $s >= 32) {
$timescale = mmU32be($b, $s + 20);
// 64-bit duration: high and low u32 halves of the u64.
$duration = mmU32be($b, $s + 24) * 4294967296 + mmU32be($b, $s + 28);
if ($timescale > 0) {
$meta->durationSec = $duration / $timescale;
}
}
}
foreach (mmFindBoxes($b, $ms, $me, 'trak') as [$ts, $te]) {
$tkhds = mmFindBoxes($b, $ts, $te, 'tkhd');
if ($tkhds === []) {
continue;
}
[$s, $e] = $tkhds[0];
// tkhd stores width/height as 16.16 fixed point — divide by 65536.
if (ord($b[$s]) === 0 && $e - $s >= 84) {
$meta->width = mmU32be($b, $s + 76) / 65536;
$meta->height = mmU32be($b, $s + 80) / 65536;
break; // first track is enough
}
}
}
return $meta;
}
/** Decode one ID3 text frame (first byte selects the encoding). */
function mmDecodeId3Text(string $b, int $start, int $len): string
{
if ($len <= 0 || $start >= strlen($b)) {
return '';
}
$encoding = ord($b[$start]);
$text = substr($b, $start + 1, $len - 1); // substr clamps, like the lib
if ($text === '') {
return '';
}
if ($encoding === 3) {
// UTF-8 (ID3v2.4); stop at the null terminator.
$zero = strpos($text, "\x00");
$chunk = $zero === false ? $text : substr($text, 0, $zero);
return (string) iconv('UTF-8', 'UTF-8//IGNORE', $chunk);
}
if ($encoding === 1 || $encoding === 2) {
// UTF-16: encoding 1 carries a BOM (sniff endianness); 2 is BOM-less BE.
$first = ord($text[0]);
$bom = $encoding === 1 && ($first === 0xff || $first === 0xfe);
$from = $bom ? ($first === 0xff ? 'UTF-16LE' : 'UTF-16BE') : 'UTF-16BE';
$body = $bom ? substr($text, 2) : $text;
// Cut at the aligned UTF-16 NUL terminator, then one iconv call.
for ($i = 0; $i + 1 < strlen($body); $i += 2) {
if (ord($body[$i]) === 0 && ord($body[$i + 1]) === 0) {
$body = substr($body, 0, $i);
break;
}
}
return (string) iconv($from, 'UTF-8//IGNORE', $body);
}
// ISO-8859-1 / ASCII range: PHP bytes are code points, so just cut at NUL.
$zero = strpos($text, "\x00");
return $zero === false ? $text : substr($text, 0, $zero);
}
/** ID3v2: walk frames, keep TIT2 (title), TPE1 (artist), TALB (album). */
function mmProbeId3(string $b): MediaMeta
{
$meta = new MediaMeta('mp3');
$version = ord($b[3]);
$tagSize = mmSyncsafe($b, 6);
$pos = 10;
$end = min(10 + $tagSize, strlen($b));
while ($pos + 10 <= $end) {
$id = substr($b, $pos, 4);
if (preg_match('/^[A-Z0-9]{4}$/', $id) !== 1) {
break;
}
// ID3v2.4 frame sizes are syncsafe; v2.2/v2.3 are plain big-endian.
$size = $version === 4 ? mmSyncsafe($b, $pos + 4) : mmU32be($b, $pos + 4);
if ($size === 0) {
break;
}
// Clamp declared size to the tag end: a corrupt frame stays inside the tag.
$avail = max($end - ($pos + 10), 0);
$text = mmDecodeId3Text($b, $pos + 10, min($size, $avail));
if ($id === 'TIT2') {
$meta->title = $text;
} elseif ($id === 'TPE1') {
$meta->artist = $text;
} elseif ($id === 'TALB') {
$meta->album = $text;
}
$pos += 10 + $size;
}
return $meta;
}
/** WAV: fmt chunk gives byteRate; data chunk size / byteRate = duration. */
function mmProbeWav(string $b): MediaMeta
{
$meta = new MediaMeta('wav');
$pos = 12; // past RIFF size + WAVE
$byteRate = 0;
while ($pos + 8 <= strlen($b)) {
$id = substr($b, $pos, 4);
$size = mmU32le($b, $pos + 4);
if ($id === 'fmt ' && $pos + 8 + 16 <= strlen($b)) {
$byteRate = mmU32le($b, $pos + 16); // byteRate sits at fmt payload offset 8
} elseif ($id === 'data' && $byteRate > 0) {
$meta->durationSec = $size / $byteRate;
break;
}
$pos += 8 + $size + ($size % 2); // chunks are word-aligned
}
return $meta;
}
/** Sniff the container and parse its metadata. Null under 12 bytes. */
function mmProbe(string $bytes): ?MediaMeta
{
if (strlen($bytes) < 12) {
return null;
}
if (substr($bytes, 4, 4) === 'ftyp') {
return mmProbeMp4($bytes); // MP4 / MOV
}
if (substr($bytes, 0, 3) === 'ID3') {
return mmProbeId3($bytes);
}
if (substr($bytes, 0, 4) === 'RIFF' && substr($bytes, 8, 4) === 'WAVE') {
return mmProbeWav($bytes);
}
if (substr($bytes, 0, 4) === 'GIF8') {
$meta = new MediaMeta('gif');
$meta->width = mmU16le($bytes, 6);
$meta->height = mmU16le($bytes, 8);
return $meta;
}
if (substr($bytes, 0, 4) === "\x1a\x45\xdf\xa3") {
return new MediaMeta('webm'); // EBML magic
}
return new MediaMeta('unknown');
}
// Usage:
// $meta = mmProbe(file_get_contents('clip.mp4'));
// var_dump($meta->kind, $meta->durationSec, $meta->width, $meta->height);
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →