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

Patricia Arquette
Release: 2024-10-31 15:06:02
Original
978 people have browsed it

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:

<code class="php">include_once('pathto/getid3.php');
$getID3 = new getID3;
$file = $getID3->analyze($filename);</code>
Copy after login

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
Copy after login
  1. Restart your web server.

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

<code class="php">$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 />");</code>
Copy after login

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.

The above is the detailed content of How can I determine the duration, dimensions, and size of a video file in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template