如何从二进制 WebM 文件中读取浮点数?

How to read a float from binary WebM file?

我正在尝试学习二进制并基于 Matroska 在 PHP 中创建一个简单的 WebM 解析器。

我用 unpack(format, data) 阅读了 TimecodeScale、MuxingAppm WritingApp 等。我的问题是当我在 Segment Information (0x1549a966) 中到达 Duration (0x4489) 时,我必须读取 float 并根据 TimecodeScale 将其转换为秒:261.564s->00 :04:21.564 我不知道怎么办。

这是一个示例序列:

`2A D7 B1 83 0F 42 40 4D 80 86 67 6F 6F 67 6C 65 57 41 86 67 6F 6F 67 6C 65 44 89 88 41 0F ED E0 00 00 00 00 16 54 AE 6B`

TimecodeScale := 2ad7b1 uint [ def:1000000; ]

MuxingApp := 4d80 string; ("google")

WritingApp := 5741 string; ("google")

Duration := 4489 float [ range:>0.0; ]

Tracks := 1654ae6b container [ card:*; ]{...}

我必须在 (0x4489) 和 return 261.564s 之后读取一个浮点数。

持续时间是 IEEE 754 format. If you want to see how the conversion is done check this 中表示的双精度浮点值(64 位)。

TimecodeScale 是以纳秒为单位的时间戳刻度。

php你可以做:

$bin = hex2bin('410fede000000000');
$timecode_scale = 1e6;

// endianness
if (unpack('S', "\xff\x00")[1] === 0xff) {
    $bytes = unpack('C8', $bin);
    $bytes = array_reverse($bytes);
    $bin = implode('', array_map('chr', $bytes));
}

$duration = unpack('d', $bin)[1];
$duration_s = $duration * $timecode_scale / 1e9;

echo "duration=${duration_s}s\n";

结果:

duration=261.564s