chaostangent/php-ass
最新稳定版本:v1.0.4
Composer 安装命令:
composer require chaostangent/php-ass
包简介
A library for reading Advanced Sub Station Alpha subtitle files
README 文档
README
A library for reading Advanced Substation Alpha subtitle files.
Specification
The ASS file specs are available in various parts in various places:
- Wikipedia has a good overview
- The original format in Microsoft Word .doc format
- How the files are incorporated into Matroska (MKV) files
In short: ASS files are an advanced version of the original SSA (Sub Station Alpha) subtitle files and include several improvements in terms of styling and effects.
A valid script file starts with [Script Info] and contains several sections in INI style format.
Quick start
Install using composer:
composer require chaostangent/php-ass
Then start using:
require __DIR__.'/vendor/autoload.php'; use ChaosTangent\ASS\Reader; $reader = new Reader(); $script = $reader->fromFile(__DIR__.'/examples/example.ass'); foreach ($script as $block) { echo $block->getId().PHP_EOL; foreach ($block as $line) { echo $line->getKey().': '.$line->getValue(); } }
Parts
Script
The ChaosTangent\ASS\Script class represents the root object of an ASS script.
You instantiate a Script with the content of a script as well as an optional filename:
$script = new Script('[Script Info]', 'mytestscript.ass');
Once instantiated you can check whether what's been passed looks like a valid ASS script:
if ($script->isASSScript()) { // do more processing }
This only checks the first few bytes for the "[Script Info]" string, it doesn't guarantee that a passed script is valid or readable.
To parse the passed script:
$script->parse();
This will go through the content passed when creating the script and parse it into blocks and lines.
To get the current collection of blocks, you can call getBlocks() or treat the script as an iterator:
foreach ($script as $block) { // block processing }
To check if a script has a block:
if ($script->hasBlock('Script Info')) { $script->getBlock('Script Info'); }
Block
Every ASS script is comprised of a few different blocks. The ChaosTangent\ASS\Block\Block abstract class represents one of these blocks.
At the moment php-ass supports the following blocks:
- Script Info as
ChaosTangent\ASS\Block\ScriptInfo - V4+ Styles as
ChaosTangent\ASS\Block\Styles - Events as
ChaosTangent\ASS\Block\Events
Any other kind of block (e.g. "Aegisub Project Garbage", "Fonts") are silently ignored when parsing.
ScriptInfo blocks provide functions for common fields:
$scriptInfoBlock->getTitle(); $scriptInfoBlock->getWrapStyle(); $scriptInfoBlock->getScriptType();
Otherwise you can just treat blocks as containers for lines. You can use array access to get a specific line:
$scriptInfoBlock[123]; // line 124 of this block
Or treat the block as an iterator:
foreach ($scriptInfoBlock as $line) { // line processing }
Line
Lines are the core of a script file. Any line that isn't a comment (not to be confused with a comment event line) uses the base class ChaosTangent\ASS\Line\Line.
Lines in some blocks are mapped according to a special "Format" line. These are represented by ChaosTangent\ASS\Line\Format. Format lines have a special getMapping() method that returns an array that can be used to parse other lines.
If all of this sounds a bit complicated, you mostly won't have to worry about it if parsing files as it's all taken care of for you. All it means is that for Dialogue and Style lines, you can use methods to get the different parts:
$styleLine->getName(); $styleLine->getPrimaryColour(); $dialogueLine->getLayer(); $dialogueLine->getText();
Dialogue lines also have an extra method for getting the text of a line without any style override codes in it:
$dialogueLine->getTextWithoutStyleOverrides();
For all lines you can use the generic getKey() and getValue() methods which will return the key of the line (e.g. "Dialogue", "Format", "Style") and its unparsed value:
$dialogueLine->getKey(); // == 'Dialogue' $dialogueLine->getValue(); // e.g. 0,0:00:00.98,0:00:05.43,ED_English,,0,0,0,,{\fad(100,200)\blur5\c&H000010&\3c&H80A0C0&}My destiny,
If you only want lines of a specific type, just do an instanceof check when iterating through:
foreach ($block as $line) { if ($line instanceof Dialogue) { echo $line->getTextWithoutStyleOverrides().PHP_EOL; } }
Tests
There is a growing test suite for this library that you can use phpunit to validate against. Any esoteric or known broken scripts would be a welcome addition.
TODO
- Allow reading of embedded information (images, fonts etc.)
- Allow construction and writing of ASS files
- More line type support
- More block type support
- Test completion
chaostangent/php-ass 适用场景与选型建议
chaostangent/php-ass 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 217 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mp4」 「subtitles」 「subtitle」 「ssa」 「ass」 「mkv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chaostangent/php-ass 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chaostangent/php-ass 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chaostangent/php-ass 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP-Client which enables you to seamlessly integrate the Bitmovin API into your existing projects
Easily deal with Timecode SMPTE format in PHP
Implementation of subtitles converter with following supported formats: srt, webvtt, ttaf1, txt, tabtxt
A wrapper around james-heinrich/getid3 to extract various information from media files.
FFMpeg video/audio/subs conversions, thumbnails, audio extraction, query...
A Laravel package for downloading videos from the net by simply passing a URL
统计信息
- 总下载量: 217
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Creative
- 更新时间: 2015-08-02