浮叶蓝空的博客
分享学习心得,记录学习笔记
文章:
84
访问:
525914
登录
关于
首页
今天是:
2025年09月09日 星期二
类别
PHP(39)
其他笔记(20)
Java(1)
C++(17)
mysql(2)
JavaScript(4)
近期文章
Linux下Cmake引用第三方静态库示例
Linux下使用Cmake构建静态库示例
Ubuntu设置自动挂载硬盘,挂载U盘
压力测试工具Apache JMeter基本使用
使用PHP脚本借助FFmpeg一键合并视频
Shell中的>、1>、2>、2>&1、&>详解
PHP开启多进程实现异步非阻塞并行执行任务
博主推荐
PHP实现文件下载接口
Qt读写注册表,C++读写注册表
PHP动态修改配置文件,存储为文件
MySql常用语法
Powershell远程连接
PHP从字符串中获取需要的内容部分
封装PHP的HTTP请求
使用PHP脚本借助FFmpeg一键合并视频
fuyelk
2022年03月02日
6493
#### 使用PHP脚本借助FFmpeg一键合并视频;合并ts视频片段;合并M3U8视频片段 #### 一、准备工作 ##### 安装FFmpeg:[https://github.com/BtbN/FFmpeg-Builds/releases](https://github.com/BtbN/FFmpeg-Builds/releases "https://github.com/BtbN/FFmpeg-Builds/releases") ##### Windows环境需将FFmpeg添加至系统环境变量 #### 二、将待合并视频片段拷贝至空目录 [](https://www.milinger.com/a251.html "待合并的视频片段") #### 三、在视频目录创建PHP脚本:merge.php(脚本内容见文末) #### 四、运行脚本 ```php php merge.php ``` [](https://www.milinger.com/a251.html "运行合并脚本") 末尾显示'Video merge complete'则合并成功 #### 合并完成 [](https://www.milinger.com/a251.html "视频合并完成") ### 脚本内容在这 ```php <?php // +--------------------------------------------------------------+ // | 合并视频片段 | // +--------------------------------------------------------------+ // | FFmpeg下载地址:https://github.com/BtbN/FFmpeg-Builds/releases | // +--------------------------------------------------------------+ // | 请将FFmpeg程序bin目录添加到系统环境变量! | // +--------------------------------------------------------------+ // | @Author fuyelk@fuyelk.com | // +--------------------------------------------------------------+ // | @date 2022/02/25 21:24 | // +--------------------------------------------------------------+ /** * 递归获取指定路径下的所有文件路径 * @param string $path 绝对路径 * @param int $removeBaseDirLen [移除初始路径长度] * @return array * @author fuyelk <fuyelk@fuyelk.com> * @date 2021/06/28 19:51 */ function show_files_path($path = '', $removeBaseDirLen = -1) { $list = []; if (-1 == $removeBaseDirLen) { $removeBaseDirLen = mb_strlen($path); } if (is_dir($path) && ($handle = opendir($path))) { if (DIRECTORY_SEPARATOR !== $path{-1}) { $path .= DIRECTORY_SEPARATOR; } while (false !== ($file = readdir($handle))) { if ('.' !== $file && '..' !== $file) { if (is_dir($path . $file)) { $list = array_merge($list, show_files_path($path . $file, $removeBaseDirLen)); } else { $list[] = str_replace('\\', '/', mb_substr($path, $removeBaseDirLen) . $file); } } } closedir($handle); } return $list; } $LIST_FILE = 'videolist'; $OUTPUT_FILE = date('YmdHis') . '_output.mp4'; $LIST_FILE = str_replace('\\', '/', __DIR__ . '/' . $LIST_FILE); $deleteListFile = !is_file($LIST_FILE); // 创建视频清单 $content = ""; $list = show_files_path(__DIR__); foreach ($list as $item) { $file = str_replace('\\', '/', __DIR__ . $item); $line = sprintf("file '%s'\n", $file); // 排除不能使用的文件 if ('ts' != pathinfo($file, PATHINFO_EXTENSION)) { continue; } $content .= $line; } file_put_contents($LIST_FILE, $content); // 调用FFmpeg $ret = ''; system(sprintf("ffmpeg -f concat -safe 0 -i \"%s\" -c copy %s", $LIST_FILE, $OUTPUT_FILE), $ret); if (0 == $ret) { echo sprintf("%soutput file:%s%s%s\n", "\033[33m", "\033[34m", $OUTPUT_FILE, "\033[0m"); echo sprintf("%sVideo merge complete!%s\n", "\033[33m", "\033[0m"); } else { echo "\033[31mError:Merge failed!\n\33[0m"; } if ($deleteListFile) { @unlink($LIST_FILE); } // 手动合并视频方法: // 1. 创建一个文件,文件中按顺序列出所有需要合并的视频(可以使用create_filelist.php来创建),格式如下 // file 'D:/Media/Videos/0001.ts' // file 'D:/Media/Videos/0002.ts' // file 'D:/Media/Videos/0003.ts' // ... // // 2. 执行命令:ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4 ```
上一篇:
Shell中的>、1>、2>、2>&1、&>详解
下一篇:
压力测试工具Apache JMeter基本使用
0人点赞
天蓝色的彼岸
OKK
2023-02-03 14:02:59
# 1
登录后评论
友情链接
doywb
2018-2025 Copyright© 米灵尔 浮叶蓝空
豫ICP备15007436号-1
豫公网安备 41152302000146号