I am stumped. I am trying to read 2 binary values out of a FLV file header meta data section. I start by reading in 400 bytes of data into a string, and then locating the keywords that locate the values I want. The problem is that I cannot figure out how to read the value so that it becomes a number.
Here is the code I have tried so far.
First, I wrote a function to locate the desired value in the input string. According to the FLV header documentation, the value is an 8 byte double. I have tried all kinds of type casting, and only end up with zero as the result. I know that the correct value is stored in there, as other GUI applications can successfully pick out the correct value.
function getValue ($source, $keyword)
{
$loc = strpos($source, $keyword);
if ($loc > 0)
{
$value = substr($source, $loc + strlen($keyword), 8);
return $value;
}
else
return -1;
}
Then I open the file, read in 400 bytes and try to pick out the width and height values.
$fp = fopen($file, 'r');
if ($fp)
{
$header = fread($fp, 400);
if ($header != false)
{
$isFLV = ($header[0]=='F' && $header[1]=='L' && $header[2]=='V');
$isVideo = (hexdec(bin2hex($header[4])) & 0x01);
if ($isFLV && $isVideo)
{
// find and read the height and width from the file header
$width = getValue ($header, "width");
$height = getValue ($header, "height");
}
}
fclose($fp);
}
The $isFLV and $isVideo flags are being correctly set. And when I add "echo" statements to the GetValue function, as well as print out the full 400 byte header, I know that the values I am looking for are in there, and they are being correctly located by the GetValue function. However, nothing I have tried so far has resulted in converting the bit stream into a number.
When I display the "ord" of each of the 8 characters read for the values, I get the following output:
0 64 118 0 0 0 0 0
and
0 64 126 0 0 0 0 0
From other applications, I know that the first value is supposed to be 352 and the second one is 480.
I am reasonably sure that the problem is with my approach - that once the bytes have been read in as a string, that they are in a format that I cannot convert. But I sure don't know how else to approach the solution. Any help would be greatly appreciated. I have bruised my head and ego both on this problem.
Trying to read a binary value from a file
Moderator: General Moderators
Re: Trying to read a binary value from a file
Thanks for the suggestion. I had not tried it previously, but did try it this morning, with no luck. I also tried using the fseek and fscanf functions to read the desired area directly from the binary file with the same negative result.
Re: Trying to read a binary value from a file
take a look at the pack() and unpack() functions
Re: Trying to read a binary value from a file
Thanks for the suggestion. I tried it and was able to get values, just not the right ones. I have tried varying the location of the bytes, and a large variety of different unpack formats. To no avail. Seems like it should be so simple, yet I can't seem to figure out the right combination of settings. I have seen a Java object oriented version of the solution, which appears to be doing the same thing I am attempting, but frankly, I don't understand OOP enough to be able to determine what the significant difference is.
Thanks for your help..............!
Thanks for your help..............!
Re: Trying to read a binary value from a file
Wow! Thanks so much for digging into this. I am spending the rest of today, and probably some of tomorrow, trying to catch up with what I should have accomplished while beating my head against this wall. However, as soon as I get caught up, I will try to bit shift my way through this one to see if I can figure out how to extract the value. I appreciate your help, and I will also dig into that spec.