Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get playtime, height and width of a vedio by PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.16k
    Comment on it

    Hello Reader's if you want to get the details about the vedio from your local server then you can use the code liberary as below:-

    $ffmpeg_path = 'ffmpeg'; //or: /usr/bin/ffmpeg - depends on your installation $vid = 'PATH/TO/VIDEO'; //Replace here!

    if (file_exists($vid)) {
    
        $video_attributes = _get_video_attributes($vid, $ffmpeg_path);
    
        print_r('Video codec: ' . $video_attributes['codec'] . ' - width: '  . $video_attributes['width'] 
                . ' - height: ' .  $video_attributes['height'] . ' <br/>');
    
        print_r('Video duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
               . $video_attributes['secs'] . '.'. $video_attributes['ms']);
    } else { echo '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})/";
        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
        );
    
    }
    

    Now in the code you just have to sepecify the url link to your vedio file in $vid. And every detail about your vedio will be print on the page. Before you run this code make sure you have install FFMPEG on your server. You can get from here

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: