Home > Article > Backend Development > FFmpeg operation guide in PHP
FFmpeg is a command line-based audio and video processing tool that can be used for transcoding, editing, cropping, merging, acceleration, volume adjustment and other functions. It is widely used in the multimedia field. In PHP, we can also implement audio and video processing functions by calling FFmpeg's command line parameters. This article will introduce how to use FFmpeg to operate audio and video in PHP.
1. Install FFmpeg extension
To use FFmpeg in PHP, you need to install the FFmpeg extension first. You can enter the following command in the terminal to install:
sudo apt-get install ffmpeg sudo apt-get install php-ffmpeg
After the installation is completed, you need to modify the php.ini file and remove the comment symbols from ffmpeg.so under extensions.
sudo nano /etc/php/7.2/apache2/php.ini
Find the following code segment:
;extension=bcmath.so ;extension=bz2.so ;extension=ctype.so ;extension=calendar.so ;extension=dba.so ;extension=exif.so ;extension=ftp.so ;extension=gettext.so ;extension=iconv.so ;extension=json.so ;extension=mysqli.so ;extension=pdo_mysql.so ;extension=shmop.so ;extension=sockets.so ;extension=sqlite3.so ;extension=sysvmsg.so ;extension=sysvsem.so ;extension=sysvshm.so ;extension=xmlrpc.so
Remove the comment symbol ";" in extension=ffmpeg.so.
After the installation is completed, you can use the following code to test whether the installation is successful:
<?php if(extension_loaded('ffmpeg')) { echo 'FFmpeg扩展已安装'; } else { echo '请确认是否安装FFmpeg扩展'; } ?>
2. Use FFmpeg
The basic function of FFmpeg is to convert audio and video files in different formats into different formats Audio and video files, here are some simple usage examples:
ffmpeg -i input.mp4 output.avi
In PHP The command line parameters can be executed through the exec() function:
<?php exec('ffmpeg -i input.mp4 output.avi'); ?>
ffmpeg -ss 00:00:00 -t 00:00:05 -i input.mp4 output.mp4
In PHP:
<?php exec('ffmpeg -ss 00:00:00 -t 00:00:05 -i input.mp4 output.mp4'); ?>
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "concat=n=2:v=1:a=1" output.mp4
In PHP:
ffmpeg -i input.mp4 -vf scale=1366:768 output.mp4
In PHP:
<?php exec('ffmpeg -i input.mp4 -vf scale=1366:768 output.mp4'); ?>
ffmpeg -i input.mp4 -af "volume=0.5" output.mp4
In PHP:
FFmpeg has more advanced functions, which can be learned through the official documentation: https: //www.ffmpeg.org/documentation.html
3. Avoid security risks
Since the exec() function executes command line parameters, it may cause security risks, such as injection attacks. In order to avoid this risk, the following methods can be used:
In short, you need to carefully consider security issues when using FFmpeg to avoid risks to your website and server.
IV. Summary
This article introduces the basic operations of using FFmpeg in PHP, including installing FFmpeg extensions, using FFmpeg to achieve audio and video conversion, cropping, splicing, resizing, adjusting volume and other functions, and provides Recommendations for avoiding security risks. I hope this article can help readers who want to use FFmpeg in PHP.
The above is the detailed content of FFmpeg operation guide in PHP. For more information, please follow other related articles on the PHP Chinese website!