get_video_resolution()

Description

Get video resolution and framerate using FFMpeg

Parameters

ColumnTypeDefaultDescription
$file string Path to video file

Return

array

Location

include/video_functions.php lines 12 to 56

Definition

 
function get_video_resolution($file)
{
    
$video_resolution = array(
        
'width'     => 0,
        
'height'    => 0,
        
'framerate' => 0.0,
    );

    
$video_info get_video_info($file);

    
// Different versions of ffprobe store the dimensions in different parts of the json output
    
if (!empty($video_info['width'])) {
        
$video_resolution['width'] = intval($video_info['width']);
    }

    if (!empty(
$video_info['height'])) {
        
$video_resolution['height'] = intval($video_info['height']);
    }

    if (!empty(
$video_info['r_frame_rate'])) {
        
$framerate_pieces explode('/'$video_info['r_frame_rate']);

        if (
floatval($framerate_pieces[1]) > 0) {
            
$video_resolution['framerate'] = floatval($framerate_pieces[0] / $framerate_pieces[1]);
        }
    }

    if (isset(
$video_info['streams']) && is_array($video_info['streams'])) {
        foreach (
$video_info['streams'] as $stream) {
            if (!empty(
$stream['codec_type']) && 'video' === $stream['codec_type']) {
                
$video_resolution['width']  = intval($stream['width']);
                
$video_resolution['height'] = intval($stream['height']);

                
$framerate_pieces explode('/'$stream['r_frame_rate']);
                if (
floatval($framerate_pieces[1]) > 0) {
                    
$video_resolution['framerate'] = floatval($framerate_pieces[0] / $framerate_pieces[1]);
                }

                break;
            }
        }
    }

    return 
$video_resolution;
}

This article was last updated 18th June 2025 21:35 Europe/London time based on the source file dated 13th May 2025 11:50 Europe/London time.