Current Location: Home> Latest Articles> PHP Video Streaming Processing: Common Methods and Code Examples

PHP Video Streaming Processing: Common Methods and Code Examples

M66 2025-06-13

PHP Video Streaming Processing: Common Methods and Code Examples

With the rapid development of the internet, video streaming has become the main way for users to watch and share videos. When developing web applications, PHP provides several ways to handle video streams, making it a great choice for video processing. This article introduces several common PHP video streaming processing methods and provides code examples for implementation.

1. Open Local Video Files and Output Video Stream

PHP can easily open a local video file and convert it into a video stream. Here’s a simple example:

$filename = 'path/to/video.mp4';
header('Content-type: video/mp4');
header('Content-Length: ' . filesize($filename));
readfile($filename);
    

In the code above, we first specify the MIME type of the video as video/mp4 and set the correct Content-Length header. Then, we use the readfile() function to read the video file and directly output it to the browser.

2. Use FFmpeg for Video Streaming Processing

FFmpeg is a powerful open-source multimedia processing tool that can handle various types of video streams. In PHP, you can easily use the exec() function to execute FFmpeg commands for video processing. Below is an example of using FFmpeg for video transcoding:

$inputFile = 'path/to/input.mp4';
$outputFile = 'path/to/output.mp4';
$ffmpegCommand = "ffmpeg -i {$inputFile} -c:v libx264 -c:a aac -strict experimental {$outputFile}";
exec($ffmpegCommand);
    

In this code, we first set the paths for the input and output files, then use the exec() function to run an FFmpeg command that transcodes the video to H.264 video encoding (libx264) and AAC audio encoding. The transcoded file is saved to the specified output path.

3. Use Streaming for Video File Transfer

In some cases, you may need to transfer video files in chunks for streaming playback. Here’s an example of using PHP for video file streaming:

$filename = 'path/to/video.mp4';
header('Accept-Ranges: bytes');
$start = 0;
$end = filesize($filename) - 1;
header("Content-Range: bytes {$start}-{$end}/" . filesize($filename));
header("Content-Length: " . filesize($filename));
$fp = fopen($filename, 'rb');

if ($fp) {
    fseek($fp, $start, SEEK_SET);
    while (!feof($fp) && ($p = ftell($fp)) <= $end) {
        $length = ($p + 1024 > $end) ? $end - $p + 1 : 1024;
        echo fread($fp, $length);
        ob_flush();
        flush();
    }
}

fclose($fp);
    

This code demonstrates how to use the `Accept-Ranges` and `Content-Range` headers to handle chunked file transfer. The `fopen()` function is used to open the video file, and the `fseek()` function sets the pointer to the correct position. Inside the loop, `fread()` reads the required data and is output via `ob_flush()` and `flush()` functions to the browser.

Summary

This article introduced several common video streaming processing methods in PHP, including how to directly output video streams, use FFmpeg for video transcoding, and implement video file streaming. These methods help developers easily handle video processing needs for various streaming applications.