PHP調(diào)用FFmpeg實現(xiàn)視頻切片
注:使用的視頻為mp4,轉(zhuǎn)換成.m3u8播放列表和.ts切片文件
1、安裝FFmpeg
我這邊是通過Nux Dextop倉庫來安裝FFmpeg。
(1) 安裝EPEL倉庫
sudo yum install -y epel-release
(2)下載并安裝Nux Dextop倉庫的RPM包
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緩存
sudo yum update -y
(4) 安裝FFmpeg
sudo yum install -y ffmpeg ffmpeg-devel
(5)驗證安裝
ffmpeg -version

2、安裝PHP
sudo yum install php php-cli
驗證安裝
php -v

3、php腳本
<?php
// 設(shè)置輸入視頻文件、切片時長(秒)和輸出目錄
$videoFile = '/data/video/input.mp4'; // 輸入視頻文件路徑
$segmentDuration = 10; // 切片時長,單位:秒
$outputDir = 'output'; // 輸出目錄
// 確保輸出目錄存在
if (!is_dir($outputDir)) {
mkdir($outputDir, 0777, true);
}
// 構(gòu)建并執(zhí)行FFmpeg命令以生成.m3u8播放列表和.ts切片文件
// 使用'-strict -2'參數(shù)允許使用實驗性編碼器'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);
// 設(shè)置目標目錄
$targetDir = 'target_dir';
if (!is_dir($targetDir)) {
mkdir($targetDir, 0777, true);
}
// 檢查.m3u8文件是否存在
$playlistFile = $outputDir . '/output.m3u8';
if(file_exists($playlistFile)){
// 復(fù)制.m3u8播放列表文件
copy($playlistFile, $targetDir . '/output.m3u8');
// 獲取所有.ts切片文件,并將其復(fù)制到目標目錄
$tsFiles = glob($outputDir . '/*.ts');
foreach ($tsFiles as $tsFile) {
copy($tsFile, $targetDir . '/' . basename($tsFile));
}
echo "視頻切片及文件復(fù)制操作完成。\n";
} else {
echo "FFmpeg處理失敗,未找到輸出文件。\n";
}
?>4、創(chuàng)建目錄(/data)
視頻目錄:/data/video
php腳本目錄:/data 腳本名稱:slice_video.php
5、執(zhí)行腳本
php slice_video.php
6、生成的切片文件夾

7、安裝Nginx
(1)安裝
sudo yum install nginx -y
(2)啟動 Nginx
sudo systemctl start nginx sudo systemctl enable nginx
(3) 檢查 Nginx 狀態(tài)
sudo systemctl status nginx
(4)關(guān)閉防火墻
sudo systemctl stop firewalld sudo systemctl disable firewalld sudo systemctl status firewalld
(5)nginx.conf文件配置
文件位置:/etc/nginx/nginx.conf
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_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_header 'Cache-Control' 'no-cache';
add_header 'Access-Control-Allow-Origin' '*';
}
}
}
8、編寫html播放器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HLS Stream Player</title>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>
<body>
<h1>視頻播放器</h1>
<video id="video" controls autoplay></video>
<script>
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,則可以直接設(shè)置src
video.src = 'http://192.168.126.129/hls/output.m3u8'; // 替換為你的實際URL
video.addEventListener('loadedmetadata', function() {
video.play();
});
}
</script>
</body>
</html>到此這篇關(guān)于PHP調(diào)用FFmpeg實現(xiàn)視頻切片的文章就介紹到這了,更多相關(guān)PHP FFmpeg視頻切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
thinkphp實現(xiàn)發(fā)送郵件密碼找回功能實例
這篇文章主要介紹了thinkphp實現(xiàn)發(fā)送郵件密碼找回功能的方法,以實例形式詳細講述了配置文件與功能代碼的實現(xiàn)方法,是非常實用的技巧,需要的朋友可以參考下2014-12-12
Thinkphp框架 表單自動驗證登錄注冊 ajax自動驗證登錄注冊
這篇文章主要介紹了Thinkphp框架 表單自動驗證登錄注冊 ajax自動驗證登錄注冊的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12

