mokhosh/laravel-xml2srt

This package is abandoned and no longer maintained. The author suggests using the mokhosh/laravel-captions package instead.

Work with SRT files and YouTube XML subtitles in Laravel

v3.0.0 2024-08-09 09:06 UTC

This package is auto-updated.

Last update: 2024-08-09 09:08:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.