tags:

views:

742

answers:

5

I have an application where users can upload video files of any size, and I'd like to be able to determine the the height/width of a Flash video file (flv or f4v) from a PHP script so that I can size the player appropriately. I'd love a pure PHP solution, but I'd be open to shelling out to a command line tool and parsing the output (if such a tool exists).

Thanks in advance!

+4  A: 

ffmpeg is probably your best bet, there is even a php module of it.

ffmpeg -i "FileName"

Alternativly you could read the information from the flv file directly by opening the file and reading the meta information.

Ady
+1  A: 

If you can't use ffmpeg because you don't have control over your server or if you want a PHP solution, have a look at getID3, there's a FLV module that should return a resolution.

lpfavreau
A: 

Alternatively, Tommy Lacroix seems to have an interesting solution too for reading the metas out of FLV files.

lpfavreau
A: 

There's also flv4php.

If the file's header contains the video's dimensions (which might not always be the case), you can also use the following code:

function flvdim($name) {
    $file = @fopen($name, 'rb');
    if($file === false)
     return false;

    $header = fread($file, 2048);
    fclose($file);
    if($header === false)
     return false;

    return array(
     'width' => flvdim_get($header, 'width'),
     'height' => flvdim_get($header, 'height')
    );
}

function flvdim_get($header, $field) {
    $pos = strpos($header, $field);
    if($pos === false)
     return false;

    $pos += strlen($field) + 2;
    return flvdim_decode(ord($header[$pos]), ord($header[$pos + 1]));
}

function flvdim_decode($byte1, $byte2) {
    $high1 = $byte1 >> 4;
    $high2 = $byte2 >> 4;
    $low1 = $byte1 & 0x0f;

    $mantissa = ($low1 << 4) | $high2;

    // (1 + m·2^(-8))·2^(h1 + 1) = (2^8 + m)·2^(h1 - 7)
    return ((256 + $mantissa) << $high1) >> 7;
}

Pleaso note that the code is reverse engineered from binary files, but it seems to work reasonably well.

Christoph
A: 

I've heard that you can use regular getimagesize() but i haven't tested it. Give it a try.

Deniss Kozlovs