PHP + FFMPEG automatically transcode the video into an H264 standard Mp4 file _ php instance

Source: Internet
Author: User
Tags form post
Recently, I am working on an online tutorial Network project. I need to upload videos in any format to automatically play h264 standard videos using html5. Finally, PHP + FFMPEG is used for implementation. Here we will share our detailed solutions with you! Configure the php. ini file

The Code is as follows:


File_uploads = on; // whether to allow file upload over HTTP. ON is enabled by default.
Upload_tmp_dir; // upload the file to the place where the temporary file is stored on the server. If it is not specified, the default Temporary Folder will be used.
Upload_max_filesize = 1024 m; // wangwen business, that is, the maximum file size allowed to be uploaded. The default value is 2 M. We set it to 1G.
Post_max_size = 1024 m; // The maximum value that can be received by a form POST to PHP. We also set it to 1G.

Max_execution_time = 3600; // maximum time (in seconds) for running each PHP page. The default value is 30 seconds. It is set to one hour because the transcoding time is long.
Max_input_time = 36000; // The maximum time required for receiving data on each PHP page. The default value is 60 seconds.
Memory_limit = 8 m; // maximum memory consumed by each PHP page. The default value is 8 M.


File Upload plugin

We recommend that you use uploadify in flame rain. Uploadify is an upload plug-in of JQuery to display progress. The plug-in is easy to install, Which is skipped here.
However, you need to note that sessions in non-ie browsers will be lost, and a lot of information has been found. The final reason is as follows:

For example, uploadify uses the flash client. The useragent generated by uploadify is different from the user-agent of the browser.

Final Solution:

The Code is as follows:


// Add the session parameter to the upmodify upload parameter 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 cannot pass pages correctly.

Video Format Conversion

Currently, FFMPEG is a popular video format conversion software in Linux. FFMPEG is a complete solution for recording, conversion, and streaming audio and video. It is a set of leading Audio/Video Codec class libraries. In addition to FFMPEG, an extension is required to transcode to H264 format. (All software packages are provided at the end of the article)

Install the h264 extension with the source code in centos 5.4

The Code is as follows:


Tar-xjvf x264-snapshot-20120718-2245-stable.tar.bz2
# Enter the decompressed source file directory
Cd x264-snapshot-20120718-2245-stable/
./Configure -- prefix =/usr/local -- enable-shared
Make
Make install

// Install ffmpeg with h264 extension in centos 5.4 Environment
Tar-xjvf ffmpeg-2.1.1.tar.bz2
// Enter the directory after decompression
Cd ffmpeg-2.1.1
./Configure -- enable-gpl -- enable-libx264
Make
Make install
// Reload the configuration
Lcfonfig
// Test whether the installation is successful
Ffmpeg

If you see the following content, the installation is successful:

The Code is as follows:


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 90.100/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 to transcode the video

The Code is as follows:


$ Cmd = 'ffmpeg-I uploadfile/video/test. wmv-c: v libx264-strict-2 uploadfile/mp4/test.mp4 ';
Exec ($ cmd, $ status );

Before running the command, make sure that the exec function is enabled. Otherwise, modify the php. ini file.

The FFMPEG + H264 extension + YASM + apache_mod_h264 is attached.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.