How to Get Video Duration, Dimension and Size in PHP

How to get video duration, dimension and size in PHP?

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/

How to get duration, size and dimensions of video file input in Yii2?

Solved.

Step 1: (Install FFmpeg from here)

Step 2: To get duration of video

public static function getDuration($filePath)
{
exec('ffmpeg -i'." '$filePath' 2>&1 | grep Duration | awk '{print $2}' | tr -d ,",$O,$S);
if(!empty($O[0]))
{
return $O[0];
}else
{
return false;
}
}

Step 3: To get dimensions of video

 public static function getAVWidthHeight( $filePath )
{
exec("ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 '$filePath'",$O,$S);
if(!empty($O))
{
$list = [
"width"=>explode("=",$O[0])[1],
"height"=>explode("=",$O[1])[1],
];

return $list;
}else
{
return false;
}
}

PHP - get video and image dimensions on upload

<?php require_once('getid3/getid3.php');
$filename='uploads/4thofJuly_184.mp4';
$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 />");
$getID3 = new getID3;
$filename='uploads/25PercentOff_710.wmv';
$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 />");

// Calling getimagesize() function
$filename="uploads/25PercentOff_960.jpg";
list($width, $height) = getimagesize($filename);

// Displaying dimensions of the image
echo "Width of image : " . $width . "<br>";

echo "Height of image : " . $height . "<br>";
?>

How can I detect video file duration in minutes with PHP?

You can get video duration with ffmpeg or getID3

Example

$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 />");

Or

 ob_start();
passthru("ffmpeg -i working_copy.flv 2>&1");
$duration = ob_get_contents();
$full = ob_get_contents();
ob_end_clean();
$search = "/duration.*?([0-9]{1,})/";
print_r($duration);
$duration = preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
print_r('<pre>');
print_r($matches[1][0]);
print_r($full);

Please see

http://getid3.sourceforge.net/

http://ffmpeg.org/

How to get video duration, dimension and size in PHP?

get flv video length

Thanks

:D

Check length of uploaded video

You can get the video duration like that:

var vid = document.getElementById("video");var duration = document.getElementById("duration");
vid.addEventListener('loadedmetadata', function() { duration.innerHTML = vid.duration;});
<div id="duration"></div><video src="http://media.w3.org/2010/05/sintel/trailer.mp4" width=300 height=200 id="video"></video>

php-ffmpeg get video duration

$ffprobe
->streams($this->videoFile)
->videos()
->first()
->get('duration');

RETURNS the duration. So I have to store that command into a variable. The correct way to get the duration is:

$duration = $ffprobe
->streams($this->videoFile)
->videos()
->first()
->get('duration');

Php ffmpeg get video duration

OK I did it finally, but I do not know if it is the best practice, but for me it is ok at the moment, what I did is saving the duration in a temporary text file.

Command:

cd C:\FFMpeg\bin; .\ffprobe -i " . $path . " -show_format | findstr
duration >
C:\xampp\htdocs\bass\storage\generator\projects\duration.txt

Now I will access that file with php and get the content of it.



Related Topics



Leave a reply



Submit