新闻详情

新闻详情

首页 / 资讯中心 / 详情

PHP调用FFmpeg实现视频切片

发布时间:2026/11/4 19:19:05来源:尧图网络
PHP调用FFmpeg实现视频切片
注使用的视频为mp4转换成.m3u8播放列表和.ts切片文件1、安装FFmpeg我这边是通过Nux Dextop仓库来安装FFmpeg。1 安装EPEL仓库1sudoyuminstall-y epel-release2下载并安装Nux Dextop仓库的RPM包1sudorpm --importhttp://li.nux.ro/download/nux/RPM-GPG-KEY-nux.rosudorpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm3更新YUM缓存1sudoyum update -y4 安装FFmpeg1sudoyuminstall-y ffmpeg ffmpeg-devel5验证安装1ffmpeg -version2、安装PHP1sudoyuminstallphp php-cli验证安装1php -v3、php脚本123456789101112131415161718192021222324252627282930313233343536373839?php// 设置输入视频文件、切片时长秒和输出目录$videoFile/data/video/input.mp4;// 输入视频文件路径$segmentDuration 10;// 切片时长单位秒$outputDiroutput;// 输出目录// 确保输出目录存在if(!is_dir($outputDir)) {mkdir($outputDir, 0777, true);}// 构建并执行FFmpeg命令以生成.m3u8播放列表和.ts切片文件// 使用-strict -2参数允许使用实验性编码器aac$cmdffmpeg -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);// 设置目标目录$targetDirtarget_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切片文件并将其复制到目标目录$tsFilesglob($outputDir./*.ts);foreach($tsFilesas$tsFile) {copy($tsFile,$targetDir./.basename($tsFile));}echo视频切片及文件复制操作完成。\n;}else{echoFFmpeg处理失败未找到输出文件。\n;}?4、创建目录/data视频目录/data/videophp脚本目录/data 脚本名称slice_video.php5、执行脚本1php slice_video.php6、生成的切片文件夹7、安装Nginx1安装1sudoyuminstallnginx -y2启动 Nginx12sudosystemctl start nginxsudosystemctlenablenginx3 检查 Nginx 状态1sudosystemctl status nginx4关闭防火墙123sudosystemctl stop firewalldsudosystemctl disable firewalldsudosystemctl status firewalld5nginx.conf文件配置文件位置/etc/nginx/nginx.conf1234567891011121314151617181920212223242526272829303132333435363738394041424344454647sudo 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_format main$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.log main;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_headerCache-Controlno-cache;add_headerAccess-Control-Allow-Origin*;}}}8、编写html播放器1234567891011121314151617181920212223242526272829303132333435!DOCTYPE htmlhtmllangenheadmetacharsetUTF-8titleHLS Stream Player/titlescriptsrchttps://cdn.jsdelivr.net/npm/hls.jslatest/script/headbodyh1视频播放器/h1videoidvideocontrols autoplay/videoscriptif(Hls.isSupported()) {var video document.getElementById(video);var hls new Hls();// 这里替换为你的.m3u8文件的实际URLvar url http://192.168.126.129/hls/output.m3u8; // 替换为你的实际URLhls.loadSource(url);hls.attachMedia(video);hls.on(Hls.Events.MANIFEST_PARSED, function() {video.play();});} else if (video.canPlayType(application/vnd.apple.mpegurl)) {// 如果浏览器直接支持HLS则可以直接设置srcvideo.src http://192.168.126.129/hls/output.m3u8; // 替换为你的实际URLvideo.addEventListener(loadedmetadata, function() {video.play();});}/script/body/html到此这篇关于PHP调用FFmpeg实现视频切片的文章就介绍到这了,
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/4 15:56:54

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/3 12:09:35

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/4 17:01:23

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/4 12:33:12

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/4 1:25:31

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/4 3:51:11

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉