2017-08-29 50 views
1

我想学习二进制文件,并基于Matroska在PHP中创建一个简单的WebM解析器。如何从二进制WebM文件读取浮点数?

我用unpack(format, data)阅读TimecodeScale,MuxingAppm WritingApp等。我的问题是当我达到Duration(0x4489)在Segment Information(0x1549a966)我必须阅读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)的浮子和返回261.564s。

回答

1

持续时间是以IEEE 754格式表示的双精度浮点值(64位)。如果您想查看转换完成情况,请检查this

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 
+0

THX @aergistal,这正是我需要的,你救了我的一天(我学到了很多东西你) – jvambio

相关问题