2017-08-29 55 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

相關問題