"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How can I determine the duration, dimensions, and size of a video file in PHP?

How can I determine the duration, dimensions, and size of a video file in PHP?

Published on 2024-11-10
Browse:640

How can I determine the duration, dimensions, and size of a video file in PHP?

Determining Video Duration, Dimensions, and Size in PHP

Getting information about uploaded video files is essential for various applications. You may need to retrieve the video's duration, dimensions, or size to display it correctly or process it further. In PHP, several methods can be used to achieve this.

getID3 Library

The getID3 library is a powerful tool that can extract metadata from various file formats, including videos. It provides detailed information about audio, video, and container formats.

To use getID3 in your PHP code, follow these steps:

include_once('pathto/getid3.php');
$getID3 = new getID3;
$file = $getID3->analyze($filename);

Once the file is analyzed, you can retrieve the following information:

  • Duration: $file['playtime_string']
  • Dimensions: $file['video']['resolution_x'] (width) and $file['video']['resolution_y'] (height)
  • Size: $file['filesize']

ffmpeg-php Extension

If you have the ability to modify your PHP installation, consider using the ffmpeg-php extension. It's a PHP extension that provides a wrapper around the powerful FFmpeg multimedia library, allowing you to perform advanced video manipulation operations.

Using ffmpeg-php, you can get all the same information as getID3, plus additional features such as:

  • Generating thumbnails
  • Converting videos to different formats
  • Extracting audio from videos

To install ffmpeg-php, follow these steps:

  1. Download the ffmpeg-php module for your PHP version from http://ffmpeg-php.sourceforge.net/.
  2. Copy the module files into your PHP extension directory (usually /usr/local/lib/php/extensions/).
  3. Enable the module by adding the following line to your php.ini file:
extension=ffmpeg.so
  1. Restart your web server.

Once installed, you can use the following code to get video information:

$ffmpeg = new FFMpeg();
$video = $ffmpeg->open($filename);

echo("Duration: ".$video->getDuration()." seconds".
" / Dimensions: ".$video->getWidth()." wide by ".$video->getHeight()." tall".
" / Filesize: ".$video->getSize()." bytes<br />");

Whether you choose the getID3 library or the ffmpeg-php extension, these methods will provide you with the necessary information to work with video files in your PHP applications.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3