Home  >  Article  >  Backend Development  >  How to use PHP and FFMPEG to process and transcode audio and video

How to use PHP and FFMPEG to process and transcode audio and video

WBOY
WBOYOriginal
2023-06-25 16:46:401730browse

With the rapid development of audio and video media, we are increasingly exposed to audio and video files in various formats in our lives and work. However, different devices, different occasions and different needs may require audio and video files in different encoding formats. Therefore, transcoding of audio and video files has become an indispensable requirement. In this article, we will introduce how to use PHP and FFMPEG (an open source audio and video codec) to process and transcode audio and video.

  1. Installing FFMPEG

First, we need to install FFMPEG on the server. The installation process is relatively cumbersome, but the main steps are as follows:

1.1 Install dependencies

First, we need to install the libraries that FFMPEG depends on. On Ubuntu system, you can use the following command to install:

sudo apt-get update
sudo apt-get install autoconf automake build-essential libass-dev libfreetype6-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev

1.2 Download FFMPEG

Then, we need to download the FFMPEG source code, you can go to the official website (https://www.ffmpeg.org) Download the latest version, or use the following command to download:

wget https://www.ffmpeg.org/releases/ffmpeg-4.4.tar.gz
tar xvf ffmpeg-4.4.tar.gz

1.3 Compile and install FFMPEG

Next, we need to compile and install FFMPEG, you can use the following command:

cd ffmpeg-4.4
./configure --enable-shared --disable-static --enable-gpl --enable-libfreetype --enable-libvorbis --enable-libx264 --enable-libx265 --enable-nonfree
make -j8
sudo make install

Compile and install Once completed, we can use FFMPEG on the server.

  1. Use PHP to call FFMPEG for audio and video processing

After understanding the installation process of FFMPEG, we can consider how to use PHP to call FFMPEG to transcode audio and video files. and processing.

2.1 Use the exec function to call FFMPEG

PHP provides the exec function, which can be used to execute commands on the system command line. Therefore, you can use the exec function in PHP to call FFMPEG for audio and video processing. For example, the following code can convert a video file in mp4 format to avi format:

$cmd = "ffmpeg -i input.mp4 output.avi";
exec($cmd);

2.2 Use PHP extension to encapsulate FFMPEG

Although the exec function can be used to conveniently call FFMPEG, there is a clear Security risk, any command can be executed, therefore, its use is not recommended. To this end, we can use PHP extensions to encapsulate the FFMPEG API to call FFMPEG more conveniently. In PHP, there are two main extensions that can be used to encapsulate FFMPEG: FFMPEG extension (http://ffmpeg-php.sourceforge.net) and PHP-FFMpeg (https://github.com/PHP-FFMpeg/PHP-FFMpeg ).

2.2.1 Using FFMPEG extension

FFMPEG extension can be installed through the PECL tool and can be installed using the following command:

sudo pecl install ffmpeg

After the installation is completed, the extension needs to be added to In the php.ini file, you can add the following lines to the file:

extension=ffmpeg.so

Using the FFMPEG extension, you can easily call the FFMPEG API for audio and video processing. For example, the following code can convert a video file in mp4 format to avi format:

$movie = new ffmpeg_movie("input.mp4");
$cmd = "ffmpeg -i input.mp4 output.avi";
exec($cmd);

2.2.2 Using PHP-FFMpeg extension

PHP-FFMpeg is a PHP extension based on FFMPEG that provides A unified API is developed for audio and video processing. You can use the following command to install:

composer require php-ffmpeg/php-ffmpeg

After the installation is complete, you can use the following code for audio and video processing:

$ffmpeg = FFMpegFFMpeg::create();
$video = $ffmpeg->open('video.mp4');
$format = new FFMpegFormatVideoWMV();
$format->setAudioCodec("wmav2")
       ->setVideoCodec("wmv2");
$video->save($format, 'video.wmv');
  1. Conclusion

This article introduces How to use PHP and FFMPEG to process and transcode audio and video. In actual development, it is necessary to choose the appropriate method for audio and video processing and transcoding according to specific needs.

The above is the detailed content of How to use PHP and FFMPEG to process and transcode audio and video. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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