In modern web development, video streaming has become a common feature. This guide walks you through how to build an online movie player using PHP and FFmpeg, from setup to embedding a player on your webpage.
First, install the PHP-FFmpeg library, which provides powerful video processing capabilities through FFmpeg. It supports format conversion, trimming, screenshots, and streaming. Install it via Composer with the following command:
composer require php-ffmpeg/php-ffmpeg
Next, define the video source you want to stream. The source can be a local file on your server or a remote video URL.
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('path/to/video.mp4');
A video stream controls the playback and resolution of your video output. You can configure the stream and synchronize it using the following code:
$stream = $video->filters()
->resize(new FFMpeg\Coordinate\Dimension(640, 480))
->synchronize();
Once your stream is ready, you can output the video to a specific path or stream it directly to the browser. The example below outputs the file in webm format:
$stream->save('path/to/output.mp4', array(
'webm' => true,
'format' => 'webm',
'audioCodec' => 'libvorbis',
'videoCodec' => 'libvpx'
));
Finally, use the HTML5 tag to embed your player. Set the src attribute to the path of your output video file to enable online playback:
<video width="640" height="480" controls>
<source src="path/to/output.mp4" type="video/webm">
</video>
Following the above steps, you can implement video streaming functionality in PHP with ease. From creating the source and stream to embedding the player, the process is straightforward and extendable. You can further enhance it with features like video transcoding, resolution switching, and thumbnails for a better user experience.