• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    PHP调用FFmpeg实现视频切片

    发布者: 网神之王 | 发布时间: 2025-6-14 13:13| 查看数: 51| 评论数: 0|帖子模式

    注:使用的视频为mp4,转换成.m3u8播放列表和.ts切片文件

    1、安装FFmpeg

    我这边是通过Nux Dextop仓库来安装FFmpeg。
    (1) 安装EPEL仓库
    1. sudo yum install -y epel-release
    复制代码
    (2)下载并安装Nux Dextop仓库的RPM包
    1. 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
    复制代码
    (3)更新YUM缓存
    1. sudo yum update -y
    复制代码
    (4) 安装FFmpeg
    1. sudo yum install -y ffmpeg ffmpeg-devel
    复制代码
    (5)验证安装
    1. ffmpeg -version
    复制代码


    2、安装PHP
    1. sudo yum install php php-cli
    复制代码
    验证安装
    1. php -v
    复制代码


    3、php脚本
    1. <?php
    2. // 设置输入视频文件、切片时长(秒)和输出目录
    3. $videoFile = '/data/video/input.mp4'; // 输入视频文件路径
    4. $segmentDuration = 10; // 切片时长,单位:秒
    5. $outputDir = 'output'; // 输出目录
    6. // 确保输出目录存在
    7. if (!is_dir($outputDir)) {
    8.     mkdir($outputDir, 0777, true);
    9. }
    10. // 构建并执行FFmpeg命令以生成.m3u8播放列表和.ts切片文件
    11. // 使用'-strict -2'参数允许使用实验性编码器'aac'
    12. $cmd = "ffmpeg -i " . escapeshellarg($videoFile) .
    13.        " -codec:v libx264 -codec:a aac -strict -2 -hls_time " . escapeshellarg($segmentDuration) .
    14.        " -hls_list_size 0 -hls_flags delete_segments " . escapeshellarg($outputDir . "/output.m3u8");
    15. // 或者,如果您有 'libfdk_aac' 可用,可以替换 '-codec:a aac -strict -2' 为 '-codec:a libfdk_aac'
    16. // $cmd = "ffmpeg -i " . escapeshellarg($videoFile) .
    17. //        " -codec:v libx264 -codec:a libfdk_aac -hls_time " . escapeshellarg($segmentDuration) .
    18. //        " -hls_list_size 0 -hls_flags delete_segments " . escapeshellarg($outputDir . "/output.m3u8");
    19. shell_exec($cmd);
    20. // 设置目标目录
    21. $targetDir = 'target_dir';
    22. if (!is_dir($targetDir)) {
    23.     mkdir($targetDir, 0777, true);
    24. }
    25. // 检查.m3u8文件是否存在
    26. $playlistFile = $outputDir . '/output.m3u8';
    27. if(file_exists($playlistFile)){
    28.     // 复制.m3u8播放列表文件
    29.     copy($playlistFile, $targetDir . '/output.m3u8');
    30.     // 获取所有.ts切片文件,并将其复制到目标目录
    31.     $tsFiles = glob($outputDir . '/*.ts');
    32.     foreach ($tsFiles as $tsFile) {
    33.         copy($tsFile, $targetDir . '/' . basename($tsFile));
    34.     }
    35.     echo "视频切片及文件复制操作完成。\n";
    36. } else {
    37.     echo "FFmpeg处理失败,未找到输出文件。\n";
    38. }
    39. ?>
    复制代码
    4、创建目录(/data)

    视频目录:/data/video
    php脚本目录:/data  脚本名称:slice_video.php

    5、执行脚本
    1. php slice_video.php
    复制代码
    6、生成的切片文件夹



    7、安装Nginx

    (1)安装
    1. sudo yum install nginx -y
    复制代码
    (2)启动 Nginx
    1. sudo systemctl start nginx
    2. sudo systemctl enable nginx
    复制代码
    (3) 检查 Nginx 状态
    1. sudo systemctl status nginx
    复制代码
    (4)关闭防火墙
    1. sudo systemctl stop firewalld
    2. sudo systemctl disable firewalld
    3. sudo systemctl status firewalld
    复制代码
    (5)nginx.conf文件配置
    文件位置:/etc/nginx/nginx.conf
    1. sudo nginx -t  # 测试配置文件语法是否正确
    2. sudo systemctl reload nginx  # 重新加载 Nginx使配置生效
    3. user nginx;
    4. worker_processes auto;
    5. error_log /var/log/nginx/error.log;
    6. pid /run/nginx.pid;

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

    8. events {
    9.     worker_connections 1024;
    10. }

    11. http {
    12.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    13.                       '$status $body_bytes_sent "$http_referer" '
    14.                       '"$http_user_agent" "$http_x_forwarded_for"';

    15.     access_log  /var/log/nginx/access.log  main;

    16.     sendfile            on;
    17.     tcp_nopush          on;
    18.     tcp_nodelay         on;
    19.     keepalive_timeout   65;
    20.     types_hash_max_size 4096;

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

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

    24.     server {
    25.         listen 80;
    26.         server_name 192.168.126.129;

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

    37. }
    复制代码
    8、编写html播放器
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <title>HLS Stream Player</title>
    6.     <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    7. </head>
    8. <body>

    9. <h1>视频播放器</h1>
    10. <video id="video" controls autoplay></video>

    11. <script>
    12.     if(Hls.isSupported()) {
    13.         var video = document.getElementById('video');
    14.         var hls = new Hls();
    15.         // 这里替换为你的.m3u8文件的实际URL
    16.         var url = 'http://192.168.126.129/hls/output.m3u8'; // 替换为你的实际URL
    17.         
    18.         hls.loadSource(url);
    19.         hls.attachMedia(video);
    20.         hls.on(Hls.Events.MANIFEST_PARSED, function() {
    21.             video.play();
    22.         });
    23.     } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
    24.         // 如果浏览器直接支持HLS,则可以直接设置src
    25.         video.src = 'http://192.168.126.129/hls/output.m3u8'; // 替换为你的实际URL
    26.         video.addEventListener('loadedmetadata', function() {
    27.             video.play();
    28.         });
    29.     }
    30. </script>

    31. </body>
    32. </html>
    复制代码
    到此这篇关于PHP调用FFmpeg实现视频切片的文章就介绍到这了,更多相关PHP FFmpeg视频切片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/program/336350pok.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表