saeven / subtitles
此包最新版本(dev-master)没有可用的许可证信息。
PHP字幕转换器和生成器
dev-master
2023-04-10 16:12 UTC
Requires
- php: ^7.4.0 || ~8.0.12 || ~8.1.0 || ~8.2.0
- nesbot/carbon: ^2.0
Requires (Dev)
- laminas/laminas-coding-standard: ^2.3.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-10 19:45:24 UTC
README
转换和编辑字幕和字幕
支持格式
安装
composer require saeven/subtitles
分支说明
此包是从 mantas-done/subtitles
分支出来的(感谢),以使其更加有观点,实现严格的类型和新接口,并实现正确的PSR-4加载。此外,这个版本的包移除了所有直接操作文件或通过file_exists等扫描文件系统来推断转换器存在的能力。它还在原始实现会分割字符串的情况下使用模式匹配。最后,它添加了Carbon来进行时间转换。
使用方法
将 .srt 内容转换为 .vtt
// add namespace use \Circlical\Subtitles\Subtitles; (new Subtitles())->load($srtContent, 'srt')->content('vtt');
手动创建VTT
$subtitles = new Subtitles(); $subtitles->add(0, 5, 'This text is shown in the beggining of video for 5 seconds'); file_put_contents( './foo', $subtitles->content('subtitles.vtt'));
从字符串加载字幕
$string = " 1 00:02:17,440 --> 00:02:20,375 Senator, we're making our final approach "; $subtitles = Subtitles::load($string, 'srt');
获取文件内容而不保存到文件
echo $subtitles->content('vtt');
添加字幕
$subtitles->add(0, 5, 'some text'); // from 0, till 5 seconds // Add multiline text $subtitles->add(0, 5, [ 'first line', 'second line', ]);
删除字幕
$subtitles->remove(0, 5); // from 0, till 5 seconds
将所有字幕增加1秒
$subtitles->shiftTime(1);
减去0.5秒
$subtitles->shiftTime(-0.5);
将1分钟到2分钟的字幕增加5秒
$subtitles->shiftTime(5, 60, 120);
示例:在1小时的视频中逐渐将时间调整2秒。视频开头不改变时间,中间调整1秒。视频结束时调整2秒。
$subtitles->shiftTimeGradually(2, 0, 3600);
"内部格式"
"内部格式"只是一个PHP数组。它用于库内部,以便在不同格式之间进行转换。
Array
(
[0] => Array
(
[start] => 137.44
[end] => 140.375
[lines] => Array
(
[0] => Senator, we're making
[1] => our final approach into Coruscant.
)
)
[1] => Array
(
[start] => 140.476
[end] => 142.501
[lines] => Array
(
[0] => Very good, Lieutenant.
)
)
)
[start] - when to start showing text (float - seconds)
[end] - when to stop showing text (float -seconds)
[lines] - one or more text lines (array)
运行测试
./vendor/bin/phpunit
贡献
并非所有原始转换器都已修正。请从“todo”文件夹中转换一个,并提交一个PR!
报告错误
如果某些文件与库不兼容,请创建一个问题并附加该文件。