乌力吉木日 發表於 2026-5-3 17:21:58

PHP调用FFmpeg实现视频切片

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">1、安装FFmpeg</a></li><li><a href="#_label1">2、安装PHP</a></li><li><a href="#_label2">3、php脚本</a></li><li><a href="#_label3">4、创建目录(/data)</a></li><li><a href="#_label4">5、执行脚本</a></li><li><a href="#_label5">6、生成的切片文件夹</a></li><li><a href="#_label6">7、安装Nginx</a></li></ul></div><p>注:使用的视频为mp4,转换成.m3u8播放列表和.ts切片文件</p>
<p class="maodian"><a name="_label0"></a></p><h2>1、安装FFmpeg</h2>
<p>我这边是通过Nux Dextop仓库来安装FFmpeg。</p>
<p>(1) 安装EPEL仓库</p>
<div class="jb51code"><pre class="brush:bash;">sudo yum install -y epel-release
</pre></div>
<p>(2)下载并安装Nux Dextop仓库的RPM包</p>
<div class="jb51code"><pre class="brush:bash;">sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
</pre></div>
<p>(3)更新YUM缓存</p>
<div class="jb51code"><pre class="brush:bash;">sudo yum update -y
</pre></div>
<p>(4) 安装FFmpeg</p>
<div class="jb51code"><pre class="brush:bash;">sudo yum install -y ffmpeg ffmpeg-devel
</pre></div>
<p>(5)验证安装</p>
<div class="jb51code"><pre class="brush:bash;">ffmpeg -version</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202502/202502231038531.png" /></p>
<p class="maodian"><a name="_label1"></a></p><h2>2、安装PHP</h2>
<div class="jb51code"><pre class="brush:bash;">sudo yum install php php-cli</pre></div>
<p>验证安装</p>
<div class="jb51code"><pre class="brush:bash;">php -v</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202502/202502231038532.png" /></p>
<p class="maodian"><a name="_label2"></a></p><h2>3、php脚本</h2>
<div class="jb51code"><pre class="brush:php;">&lt;?php
// 设置输入视频文件、切片时长(秒)和输出目录
$videoFile = '/data/video/input.mp4'; // 输入视频文件路径
$segmentDuration = 10; // 切片时长,单位:秒
$outputDir = 'output'; // 输出目录
// 确保输出目录存在
if (!is_dir($outputDir)) {
    mkdir($outputDir, 0777, true);
}
// 构建并执行FFmpeg命令以生成.m3u8播放列表和.ts切片文件
// 使用'-strict -2'参数允许使用实验性编码器'aac'
$cmd = "ffmpeg -i " . escapeshellarg($videoFile) .
       " -codec:v libx264 -codec:a aac -strict -2 -hls_time " . escapeshellarg($segmentDuration) .
       " -hls_list_size 0 -hls_flags delete_segments " . escapeshellarg($outputDir . "/output.m3u8");
// 或者,如果您有 'libfdk_aac' 可用,可以替换 '-codec:a aac -strict -2' 为 '-codec:a libfdk_aac'
// $cmd = "ffmpeg -i " . escapeshellarg($videoFile) .
//      " -codec:v libx264 -codec:a libfdk_aac -hls_time " . escapeshellarg($segmentDuration) .
//      " -hls_list_size 0 -hls_flags delete_segments " . escapeshellarg($outputDir . "/output.m3u8");
shell_exec($cmd);
// 设置目标目录
$targetDir = 'target_dir';
if (!is_dir($targetDir)) {
    mkdir($targetDir, 0777, true);
}
// 检查.m3u8文件是否存在
$playlistFile = $outputDir . '/output.m3u8';
if(file_exists($playlistFile)){
    // 复制.m3u8播放列表文件
    copy($playlistFile, $targetDir . '/output.m3u8');
    // 获取所有.ts切片文件,并将其复制到目标目录
    $tsFiles = glob($outputDir . '/*.ts');
    foreach ($tsFiles as $tsFile) {
      copy($tsFile, $targetDir . '/' . basename($tsFile));
    }
    echo "视频切片及文件复制操作完成。\n";
} else {
    echo "FFmpeg处理失败,未找到输出文件。\n";
}
?&gt;</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2>4、创建目录(/data)</h2>
<p>视频目录:/data/video</p>
<p>php脚本目录:/data &nbsp;脚本名称:slice_video.php&nbsp;</p>
<p class="maodian"><a name="_label4"></a></p><h2>5、执行脚本</h2>
<div class="jb51code"><pre class="brush:bash;">php slice_video.php
</pre></div>
<p class="maodian"><a name="_label5"></a></p><h2>6、生成的切片文件夹</h2>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202502/202502231038533.png" /></p>
<p class="maodian"><a name="_label6"></a></p><h2>7、安装Nginx</h2>
<p>(1)安装</p>
<div class="jb51code"><pre class="brush:bash;">sudo yum install nginx -y
</pre></div>
<p>(2)启动 Nginx</p>
<div class="jb51code"><pre class="brush:bash;">sudo systemctl start nginx
sudo systemctl enable nginx
</pre></div>
<p>(3) 检查 Nginx 状态</p>
<div class="jb51code"><pre class="brush:bash;">sudo systemctl status nginx
</pre></div>
<p>(4)关闭防火墙</p>
<div class="jb51code"><pre class="brush:bash;">sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo systemctl status firewalld
</pre></div>
<p>(5)nginx.conf文件配置</p>
<p>文件位置:/etc/nginx/nginx.conf</p>
<div class="jb51code"><pre class="brush:php;">sudo nginx -t# 测试配置文件语法是否正确
sudo systemctl reload nginx# 重新加载 Nginx使配置生效
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log/var/log/nginx/access.logmain;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type      application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
      listen 80;
      server_name 192.168.126.129;

      location /hls/ {
            alias /data/target_dir/; # 替换为你的实际目录路径
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
         }
            add_header 'Cache-Control' 'no-cache';
            add_header 'Access-Control-Allow-Origin' '*';
      }
    }

}
</pre></div>
<p>8、编写html播放器</p>
<div class="jb51code"><pre class="brush:xhtml;">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;HLS Stream Player&lt;/title&gt;
    &lt;script src="https://cdn.jsdelivr.net/npm/hls.js@latest"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;视频播放器&lt;/h1&gt;
&lt;video id="video" controls autoplay&gt;&lt;/video&gt;

&lt;script&gt;
    if(Hls.isSupported()) {
      var video = document.getElementById('video');
      var hls = new Hls();
      // 这里替换为你的.m3u8文件的实际URL
      var url = 'http://192.168.126.129/hls/output.m3u8'; // 替换为你的实际URL
      
      hls.loadSource(url);
      hls.attachMedia(video);
      hls.on(Hls.Events.MANIFEST_PARSED, function() {
            video.play();
      });
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      // 如果浏览器直接支持HLS,则可以直接设置src
      video.src = 'http://192.168.126.129/hls/output.m3u8'; // 替换为你的实际URL
      video.addEventListener('loadedmetadata', function() {
            video.play();
      });
    }
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</pre></div>
頁: [1]
查看完整版本: PHP调用FFmpeg实现视频切片