mokhosh/laravel-xml2srt
Composer 安装命令:
composer require mokhosh/laravel-xml2srt
包简介
Work with SRT files and YouTube XML subtitles in Laravel
README 文档
README
You can parse xml timecodes into line by line representation of the caption, and then generate srt files based on the parsed caption.
Installation
You can install the package via composer:
composer require mokhosh/laravel-caption
Concepts
Caption has a Collection of Lines, to which you can add() a new Line.
You can also get all lines() of a Caption.
Each line is a readonly value object consisting of a float start, a float duration, and a text.
You can also use TimecodeConverter's floatToTimecode() to convert floating seconds/miliseconds values to formatted timecode.
Usage
Let's say you need to read subtitles in any custom format and generate SRT subtitles based on its contents.
Here's how we convert an OpenAI transcription Json to an STR file:
use Mokhosh\LaravelCaption\Caption;use Mokhosh\LaravelCaption\Generators\SrtGenerator;use Mokhosh\LaravelCaption\Line; // $response = OpenAI::audio()->transcribe(); $caption = new Caption; foreach ($response->segments as $segment) { $caption->add(new Line( floatval($segment->start), floatval($segment->end) - floatval($segment->start), trim($segment->text), )); } SrtGenerator::load($caption)->export('output.srt');
Or you can use the facade to load a json file containing an OpenAI response:
use Mokhosh\LaravelCaption\Facades\LaravelCaption; // convert to srt and return output path $output = LaravelCaption::openai2srt('input.json', 'output.srt');
You can simply convert a YouTube xml timecode file to a srt subtitle file like so:
use Mokhosh\LaravelCaption\Facades\LaravelCaption; // convert to srt and return output path $output = LaravelCaption::xml2srt('input.xml', 'output.srt');
If you need to chunk your xml into smaller srt files, do this:
use Mokhosh\LaravelCaption\Facades\LaravelCaption; // chunk every 10 lines into chunks/ folder and return an array of chunks' paths $chunks = LaravelCaption::xml2srt('input.xml', 'chunks/', every: 10);
If you need more control you can do this:
use Mokhosh\LaravelCaption\Generators\SrtGenerator;use Mokhosh\LaravelCaption\Parsers\XmlCaptionParser; $caption = XmlCaptionParser::import('input.xml')->parse(); $output = SrtGenerator::load($caption)->export('output.srt');
And for chunking:
use Mokhosh\LaravelCaption\Generators\SrtGenerator;use Mokhosh\LaravelCaption\Parsers\XmlCaptionParser; $caption = XmlCaptionParser::import('input.xml')->parse(); // chunk every 4 lines into chunks folder and prefix chunk files with the word "part" $chunks = SrtGenerator::load($caption)->chunk(4, 'chunks/', 'part');
Testing
./vendor/bin/pest
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
mokhosh/laravel-xml2srt 适用场景与选型建议
mokhosh/laravel-xml2srt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Mo Khosh」 「laravel-xml2srt」 「laravel-caption」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mokhosh/laravel-xml2srt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mokhosh/laravel-xml2srt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mokhosh/laravel-xml2srt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Obfuscate emails and strings in PHP and Laravel
Send sms messages in Laravel with Iranian sms providers
Alfabank REST API integration
download youtube videos in your laravel applications (for personal and educational purposes of course)
Work with SRT files and YouTube XML subtitles in Laravel
Laravel package for Accurate Online API integration.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-23