PHP調(diào)用FFmpeg實(shí)現(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)驗(yàn)證安裝
ffmpeg -version
2、安裝PHP
sudo yum install php php-cli
驗(yàn)證安裝
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ù)允許使用實(shí)驗(yàn)性編碼器'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è)置目標(biāo)目錄 $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ù)制到目標(biāo)目錄 $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/; # 替換為你的實(shí)際目錄路徑 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文件的實(shí)際URL var url = 'http://192.168.126.129/hls/output.m3u8'; // 替換為你的實(shí)際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'; // 替換為你的實(shí)際URL video.addEventListener('loadedmetadata', function() { video.play(); }); } </script> </body> </html>
到此這篇關(guān)于PHP調(diào)用FFmpeg實(shí)現(xiàn)視頻切片的文章就介紹到這了,更多相關(guān)PHP FFmpeg視頻切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TP3.2.3框架使用CKeditor編輯器在頁面中上傳圖片的方法分析
這篇文章主要介紹了TP3.2.3框架使用CKeditor編輯器在頁面中上傳圖片的方法,結(jié)合實(shí)例形式分析了thinkPHP3.2.3框架使用CKeditor編輯器相關(guān)配置方法與操作注意事項(xiàng),需要的朋友可以參考下2019-12-12PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的資源更新頁面部分,需要的朋友可以參考下2014-06-06php設(shè)計(jì)模式之建造器模式分析【星際爭霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之建造器模式,結(jié)合星際爭霸游戲案例形式分析了PHP建造器模式相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-01-01thinkPHP5.0框架引入Traits功能實(shí)例分析
這篇文章主要介紹了thinkPHP5.0框架引入Traits功能,結(jié)合實(shí)例形式分析了Traits的概念、功能及thinkPHP5.0中Traits功能的使用方法,需要的朋友可以參考下2017-03-03Laravel框架實(shí)現(xiàn)定時發(fā)布任務(wù)的方法
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)定時發(fā)布任務(wù)的方法,結(jié)合實(shí)例形式分析了使用Linux的cronTab功能實(shí)現(xiàn)定時發(fā)布任務(wù)的相關(guān)設(shè)置與操作技巧,需要的朋友可以參考下2018-08-08laravel 實(shí)現(xiàn)劃分admin和home 模塊分組
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)劃分admin和home 模塊分組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析
這篇文章主要介紹了PHP xpath提取網(wǎng)頁數(shù)據(jù)內(nèi)容代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07