mantas-done / subtitles
PHP的子标题转换器和生成器
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^8.0
- dev-master
- v1.0.22
- v1.0.21
- v1.0.20
- v1.0.19
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.3.10
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-tests
- dev-vtt
- dev-cli
- dev-kocoten1992-patch-1
- dev-scrutinizer-patch-1
This package is auto-updated.
Last update: 2024-09-23 10:28:09 UTC
README
🥳🎉👏 可能是最佳的子标题解析器 🥳🎉👏
💣 在数千个用户提交的文件上进行了测试 🤯
🔥 几乎100%的单元测试覆盖率 💥
支持的格式
命令行
可以从命令行使用,将.srt转换为.vtt
php subtitles.phar input.srt output.vtt
subtitles.phar文件可在此处找到 - https://github.com/mantas-done/subtitles/releases
安装(支持PHP 7.4+)
composer require mantas-done/subtitles
使用方法
将.srt文件转换为.vtt
// add namespace use \Done\Subtitles\Subtitles; Subtitles::convert('subtitles.srt', 'subtitles.vtt');
// if no input format is specified, library will determine file format by its content // if third parameter is specified, library will convert the file to specified format. // list of formats are in Subtitle::$formats, they are: ass, dfxp, sbv, srt, stl, sub, ttml, txt_quicktime, vtt Subtitles::convert('subtitles1', 'subtitles2', ['output_format' => 'vtt']);
手动创建文件
$subtitles = new Subtitles(); $subtitles->add(0, 5, 'This text is shown in the beggining of video for 5 seconds'); $subtitles->save('subtitles.vtt');
从现有文件加载子标题
$subtitles = Subtitles::loadFromFile('subtitles.srt');
从字符串加载子标题
$string = " 1 00:02:17,440 --> 00:02:20,375 Senator, we're making our final approach "; $subtitles = Subtitles::loadFromString($string, 'srt');
保存子标题到文件
$subtitles->save('subtitler.vtt');
获取文件内容而不保存到文件
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', ]); // Add styles to VTT file format // Only VTT supports styles currently $subtitles->add(0, 5, 'text', ['vtt_cue_settings' => 'position:50% line:15% align:middle']);
移除子标题
$subtitles->remove(0, 5); // from 0, till 5 seconds
修剪子标题
$subtitles->trim(3, 4); // get only from 3, till 4 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);
异常
库将抛出UserException,其消息可以显示给用户。
try { (new \Done\Subtitles\Subtitles())->add(0, 1, 'very long text... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')->content('scc'); } catch (\Done\Subtitles\Code\UserException $e) { echo $e->getMessage(); // SCC file can't have more than 4 lines of text each 32 characters long. This text is too long: <text from user file that triggered this error> }
默认情况下,库尝试检测用户可以显示的错误,以便他们可以修复它们。如果您想放宽规则并允许库转换某些不合法的文件,请使用['strict' => false]
Subtitles::convert($input, $output, ['strict' => false]); Subtitles::loadFromString($string, ['strict' => false]); Subtitles::loadFromFile($input, ['strict' => false]);
如何添加新的子标题格式?
您需要实现ConverterContract.php接口。它有两个方法。
fileContentToInternalFormat($file_content) internalFormatToFileContent($internal_format)
基本来说,您的实现应该能够将子标题文件转换为“内部库的格式”,并将内部库的格式转换回子标题文件。
“内部库的”格式用作中间格式,以便能够在不同格式之间进行转换。
最好的例子是查看SrtConverter.php的实现。
这是.srt文件的示例。
“内部格式”
“内部格式”只是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)
运行测试
php vendor/bin/phpunit
贡献
您可以通过任何方式贡献。如果您需要一些指导,请从以下表格中选择
目前库应该只支持基本功能(几行文本)。无需支持不同的文本样式或文本位置。
报告错误
如果某些文件与库不兼容,请创建问题并附加文件。