Realization of PHP+FFMPEG Transcoding Video into H264 Standard Mp4 File

  • 2021-07-21 07:40:47
  • OfStack

Configure the php. ini file


file_uploads = on ;// Is it allowed to pass HTTP Switch for uploading files. Default to ON That is to say, open
upload_tmp_dir ;// The file is uploaded to the place where the temporary file is stored on the server. If it is not specified, the system default temporary folder will be used
upload_max_filesize = 1024m ;// Looking at the text business, that is, the maximum size of files allowed to be uploaded. Default to 2M, We set it to 1G
post_max_size = 1024m ;// Refers to passing through a form POST To PHP The maximum value that can be received by, we also set to 1G max_execution_time = 3600 ;// Each PHP Maximum time for a page to run ( Seconds ) , default 30 Seconds, set to 1 Hours, because the later transcoding time is very long.
max_input_time = 36000 ;// Each PHP Maximum time required for a page to receive data, default 60 Seconds
memory_limit = 8m ;// Each PHP Maximum memory consumed by the page, default 8M

File upload plug-in

Flame Rain recommends everyone to use uploadify, and Uploadify is an upload plug-in of JQuery to realize progress display. Plug-ins are easy to install and skipped here.
However, there is a problem that needs attention. The non-IE browser session will be lost. After checking a lot of data, the final summary reason is:

Because an flash client, such as uploadify, generates an useragent that is different from the browser's user-agent.

Final solution:


// In upmodify Add to the upload parameter session Parameters, as follows:
scriptData: {"SESSION_ID":""},
// Add the following code to the server receiving page:
if(@$_REQUEST['SESSION_ID'] && ($session_id=$_REQUEST['SESSION_ID']) !=session_id()){
session_destroy();
session_id($session_id);
@session_start();
}

This solves the problem that FLASH can't deliver the page correctly

Video format conversion

At present, the popular video format conversion software under Linux is FFMPEG, and FFMPEG is a complete solution for recording, screenshots, converting and streaming audio and video, and a leading audio/video codec class library. In addition to FFMPEG, we need an extension to transcode to H264 format. (At the end of the article, the download addresses of all software packages are given)

centos 5.4 Environment Installation Source Code Installation h264 Extension


tar -xjvf x264-snapshot-20120718-2245-stable.tar.bz2
  # Enter the extracted source file directory
  cd x264-snapshot-20120718-2245-stable/
  ./configure --prefix=/usr/local --enable-shared
  make
  make install  //centos 5.4 Install source code installation under the environment ffmpeg with h264 Expand
 tar -xjvf ffmpeg-2.1.1.tar.bz2
 // Enter the decompressed directory
 cd ffmpeg-2.1.1
 ./configure --enable-gpl --enable-libx264
 make
 make install
 // Reload configuration
 lcfonfig
 // Test whether the installation is successful
 ffmpeg

If you see the following, the installation was successful:


ffmpeg version 2.1.1 Copyright (c) 2000-2013 the FFmpeg developers
 built on Dec 17 2013 23:32:40 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-3)
 configuration: --enable-libx264 --enable-gpl
 libavutil 52. 48.101 / 52. 48.101
 libavcodec 55. 39.101 / 55. 39.101
 libavformat 55. 19.104 / 55. 19.104
 libavdevice 55. 5.100 / 55. 5.100
 libavfilter 3. 90.100 / 3. 90.100
 libswscale 2. 5.101 / 2. 5.101
 libswresample 0. 17.104 / 0. 17.104
 libpostproc 52. 3.100 / 52. 3.100
 Hyper fast Audio and Video encoder
 usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
 Use -h to get full help or, even better, run 'man ffmpeg'

php calls ffmpeg transcoding video


$cmd = 'FFMPEG  -i  uploadfile/video/test.wmv -c:v libx264 -strict -2 uploadfile/mp4/test.mp4';
 exec($cmd, $status);

Please make sure that the exec function is turned on before running. Otherwise, modify the php. ini file

Finally, we attach FFMPEG+H264 Extension +YASM+apache_mod_h264


Related articles: