PHPackages                             mokhosh/laravel-caption - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mokhosh/laravel-caption

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mokhosh/laravel-caption
=======================

Work with SRT files and YouTube XML subtitles in Laravel

v3.0.0(1y ago)213[3 PRs](https://github.com/mokhosh/laravel-caption/pulls)MITPHPPHP ^8.2CI passing

Since Dec 23Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/mokhosh/laravel-caption)[ Packagist](https://packagist.org/packages/mokhosh/laravel-caption)[ Docs](https://github.com/mokhosh/laravel-caption)[ RSS](/packages/mokhosh-laravel-caption/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (13)Versions (10)Used By (0)

Work with SRT files and YouTube XML subtitles in Laravel
========================================================

[](#work-with-srt-files-and-youtube-xml-subtitles-in-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/65a49e8d0aa01bdbff463ec20a4fc1bc52a6734136cab2ffb236bce6f3c15adf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f6b686f73682f6c61726176656c2d63617074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mokhosh/laravel-caption)[![GitHub Tests Action Status](https://camo.githubusercontent.com/075248b7dac4128f617e4aa148461631d8bc198a4dcd1619b4fc7378d3d36531/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f6b686f73682f6c61726176656c2d63617074696f6e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/mokhosh/laravel-caption/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7624835e7b3f93256b7e81e526061ca53b308a98e1faa7e3f04941dec60aea8b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f6b686f73682f6c61726176656c2d63617074696f6e2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/mokhosh/laravel-caption/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6c934768a5fdf62e96131826c420757130a8fc71114ca8239569478459521913/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f6b686f73682f6c61726176656c2d63617074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mokhosh/laravel-caption)

You can parse xml timecodes into line by line representation of the caption, and then generate srt files based on the parsed caption.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mokhosh/laravel-caption
```

Concepts
--------

[](#concepts)

`Caption` has a `Collection` of `Line`s, 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
-----

[](#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
-------

[](#testing)

```
./vendor/bin/pest
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Mo Khosh](https://github.com/mokhosh)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance66

Regular maintenance activity

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 79.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~46 days

Recently: every ~54 days

Total

6

Last Release

639d ago

Major Versions

v1.1.2 → v2.0.02024-08-08

v2.0.0 → v3.0.02024-08-09

PHP version history (2 changes)v1.0.0PHP ^8.1

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/939919859a4e4f8e56b5771bc355135f44d98d8a3926baa6ce4358d2dc090edf?d=identicon)[mokhosh](/maintainers/mokhosh)

---

Top Contributors

[![mokhosh](https://avatars.githubusercontent.com/u/6499685?v=4)](https://github.com/mokhosh "mokhosh (66 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")

---

Tags

laravelMo Khoshlaravel-xml2srtlaravel-caption

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/mokhosh-laravel-caption/health.svg)

```
[![Health](https://phpackages.com/badges/mokhosh-laravel-caption/health.svg)](https://phpackages.com/packages/mokhosh-laravel-caption)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M626](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
