首页 编程设计PHP正文

php实现m3u8格式转换mp4

云水 PHP 2021-10-15 12:20:29 1451 0

有时候在网上看多视屏格式都是m3u8,如何将m3u8视频格式转成MP4格式呢,网上搜集了好多教程,总结如下:

1. 安装ffmpeg插件

linux安装ffmpeg:

下载地址:https://ffmpeg.org/download.html

选择对应系统的版本(根据自己CUP类型选择),下载后解压

wget -O ffmpeg.tar.xz https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-armhf-static.tar.xz
xz -d ffmpeg.tar.xz
tar -xvf ffmpeg.tar
cd ffmpeg
./ffmpeg

如果是debian系统,直接运行

apt-get update
apt-get install ffmpeg

2.在服务器上部署PHP源码

<?php
$token = $_GET['token'];
$mytoken = "www.apod.cc";
if($token != $mytoken){
    exit();
}
$url =  $_GET['url'];
$file= "/www/wwwroot/192.168.31.80/m3u8tomp4/vedio";//文件保存地址
//$url = 'https://rescdn.yishihui.com/longvideo/videocomposite/output/live/696380/1583998162597time-1LD.m3u8?Expires=1585381145&OSSAccessKeyId=LTAIHZz0zdTMC7HN&Signature=nrihRPuLby7Lm8F92P3NtoXdlIo%3D';
$ts_content = file_get_contents($url);
$ts_content = explode(',', $ts_content);
$ts_file = array();
foreach ($ts_content as $key => $value) {
if($key == 0) continue;
$value = trim($value);
$ts_file[] = substr($value, 0, strpos($value, '.ts') + 3);
}
$url_prefix = substr($url, 0, strpos($url, '.m3u8'));
$url_prefix = substr($url, 0, strrpos($url, '/') + 1);
$file_content = '';
foreach ($ts_file as $key => $value) {
$file_content .= file_get_contents($url_prefix . $value);
}
 
file_put_contents('tmp_out.ts', $file_content);
exec($file . "ffmpeg -i tmp_out.ts tmp_out.mp4");

通过访问http://ip?token=www.apod.cc&url=你的m3u8地址,来获取mp4文件。


版权声明

1.本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行测试。
2.本站资源仅供学习和交流使用,版权归资源原作者所有,请在下载后24小时之内自觉删除。
3.若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。
4.若内容涉及侵权或违法信息,请联系本站管理员进行下架处理,邮箱ganice520@163.com(本站不支持其他投诉反馈渠道,谢谢合作)

本文链接:http://apod.cc/index.php/post/623.html

发表评论

评论列表(0人评论 , 1451人围观)
☹还没有评论,来说两句吧...