48

I want to know how to get the duration, dimension and size of uploaded video file in PHP. The file can be in any video format.

brasofilo
  • 23,940
  • 15
  • 86
  • 168
Chandan
  • 483
  • 1
  • 5
  • 4

4 Answers4

77

getID3 supports video formats. See: http://getid3.sourceforge.net/

Edit: So, in code format, that'd be like:

include_once('pathto/getid3.php');
$getID3 = new getID3;
$file = $getID3->analyze($filename);
echo("Duration: ".$file['playtime_string'].
" / Dimensions: ".$file['video']['resolution_x']." wide by ".$file['video']['resolution_y']." tall".
" / Filesize: ".$file['filesize']." bytes<br />");

Note: You must include the getID3 classes before this will work! See the above link.

Edit: If you have the ability to modify the PHP installation on your server, a PHP extension for this purpose is ffmpeg-php. See: http://ffmpeg-php.sourceforge.net/

Ebrahim
  • 1,388
  • 2
  • 21
  • 27
aendrew
  • 4,918
  • 3
  • 35
  • 54
  • Hi aendrew,Thanks but this workes well for the audio file but not working correctly on the video files like mp4,wmv etc... Please let me know any other solution apart from this. – Chandan Feb 11 '11 at 07:32
  • 1
    Use a different library, then, or a PHP extension like ffmpeg-php (http://ffmpeg-php.sourceforge.net/) – aendrew Feb 11 '11 at 07:58
  • what is the problem with getID3 and ffmpeg ?? why they are not making UI proper so that reading that become very user friendly... got fade up by looking at home page of getID3 only... – Shyam Jun 26 '14 at 11:48
  • @GhanshyamDobariya Different tools are useful for different uses. getID3 would be significantly less useful to me if it had a UI. – aendrew Jun 28 '14 at 00:42
  • 1
    I tested this with an FLV file that had 480 res and the `$file['video']['resolution_x']` gave me `int(16)`. I was able to get the correct video width using `$width=$file['flv']['meta']['onMetaData']['width'];` – kmoney12 May 29 '15 at 03:11
  • 4
    If you're using composer: `{"require":{"james-heinrich/getid3": ">=1.9.9"}}` then run `php composer.phar update`. – John Erck Jun 11 '15 at 17:44
  • This one helped me a lot :D – FosAvance Jul 29 '17 at 13:37
  • Thanks a lot, you saved my day – sammax Jul 12 '20 at 15:19
  • Worked well with MOV, FLV and MP4, thanks. – Manish gour Apr 28 '21 at 08:13
23

If you have FFMPEG installed on your server (http://www.mysql-apache-php.com/ffmpeg-install.htm), it is possible to get the attributes of your video using the command "-vstats" and parsing the result with some regex - as shown in the example below. Then, you need the PHP funtion filesize() to get the size.

$ffmpeg_path = 'ffmpeg'; //or: /usr/bin/ffmpeg , or /usr/local/bin/ffmpeg - depends on your installation (type which ffmpeg into a console to find the install path)
$vid = 'PATH/TO/VIDEO'; //Replace here!

 if (file_exists($vid)) {

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $vid); // check mime type
    finfo_close($finfo);

    if (preg_match('/video\/*/', $mime_type)) {

        $video_attributes = _get_video_attributes($vid, $ffmpeg_path);

        print_r('Codec: ' . $video_attributes['codec'] . '<br/>');

        print_r('Dimension: ' . $video_attributes['width'] . ' x ' . $video_attributes['height'] . ' <br/>');

        print_r('Duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
                . $video_attributes['secs'] . '.' . $video_attributes['ms'] . '<br/>');

        print_r('Size:  ' . _human_filesize(filesize($vid)));

    } else {
        print_r('File is not a video.');
    }
} else {
    print_r('File does not exist.');
}

function _get_video_attributes($video, $ffmpeg) {

    $command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';
    $output = shell_exec($command);

    $regex_sizes = "/Video: ([^,]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; // or : $regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; (code from @1owk3y)
    if (preg_match($regex_sizes, $output, $regs)) {
        $codec = $regs [1] ? $regs [1] : null;
        $width = $regs [3] ? $regs [3] : null;
        $height = $regs [4] ? $regs [4] : null;
    }

    $regex_duration = "/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/";
    if (preg_match($regex_duration, $output, $regs)) {
        $hours = $regs [1] ? $regs [1] : null;
        $mins = $regs [2] ? $regs [2] : null;
        $secs = $regs [3] ? $regs [3] : null;
        $ms = $regs [4] ? $regs [4] : null;
    }

    return array('codec' => $codec,
        'width' => $width,
        'height' => $height,
        'hours' => $hours,
        'mins' => $mins,
        'secs' => $secs,
        'ms' => $ms
    );
}

function _human_filesize($bytes, $decimals = 2) {
    $sz = 'BKMGTP';
    $factor = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
F3L1X79
  • 2,367
  • 2
  • 25
  • 40
  • 2
    Thanks so much for this! I found that the provided regex fell short because my test videos had an extra comma in the codec section. That said, here's a fix for anyone with the same problem: `$regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/";` also, my ffmpeg path was: `/usr/local/bin/ffmpeg` (type `which ffmpeg` into a console to find the install path). Here's a Regexr sample: http://regexr.com/3fs8d – 1owk3y May 02 '17 at 01:27
  • Best one. Thanks a lot – Fokrule Jan 21 '19 at 03:59
11

If you use Wordpress you can just use the wordpress build in function with the video id provided wp_get_attachment_metadata($videoID):

wp_get_attachment_metadata($videoID);

helped me a lot. thats why i'm posting it, although its just for wordpress users.

Timotheus Triebl
  • 1,466
  • 3
  • 18
  • 27
3

https://github.com/JamesHeinrich/getID3 download getid3 zip and than only getid3 named folder copy paste in project folder and use it as below show...

<?php
        require_once('/fire/scripts/lib/getid3/getid3/getid3.php');
        $getID3 = new getID3();
        $filename="/fire/My Documents/video/ferrari1.mpg";
        $fileinfo = $getID3->analyze($filename);

        $width=$fileinfo['video']['resolution_x'];
        $height=$fileinfo['video']['resolution_y'];

        echo $fileinfo['video']['resolution_x']. 'x'. $fileinfo['video']['resolution_y'];
        echo '<pre>';print_r($fileinfo);echo '</pre>';
?>
Manthan Patel
  • 1,105
  • 10
  • 19