Jim OHalloran
Jim OHalloran

Reputation: 5908

How can I obtain the dimensions of a Flash video file from PHP code?

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!

Upvotes: 3

Views: 3214

Answers (5)

Deniss Kozlovs
Deniss Kozlovs

Reputation: 4841

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

Upvotes: 0

Christoph
Christoph

Reputation: 169563

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.

Upvotes: 1

lpfavreau
lpfavreau

Reputation: 13211

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

Upvotes: 0

lpfavreau
lpfavreau

Reputation: 13211

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.

Upvotes: 2

Ady
Ady

Reputation: 4736

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.

Upvotes: 5

Related Questions