mokhosh/laravel-xml2srt

Convert youtube xml subtitles to srt format

v1.1.2 2024-01-07 13:13 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 represenation 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-xml2srt

Usage

You can simply convert a youtube xml timecode file to a srt subtitle file like so:

use Mokhosh\LaravelXmlToSrt\Facades\LaravelXmlToSrt;

// convert to srt and return output path
$output = LaravelXmlToSrt::convert('input.xml', 'output.srt');

If you need to chunk your xml into smaller srt files, do this:

use Mokhosh\LaravelXmlToSrt\Facades\LaravelXmlToSrt;

// chunk every 10 lines into chunks/ folder and return an array of chunks' paths
$chunks = LaravelXmlToSrt::chunk('input.xml', 10, 'chunks/');

If you need more control you can do this:

use Mokhosh\LaravelXmlToSrt\XmlCaptionParser;
use Mokhosh\LaravelXmlToSrt\SrtGenerator;

$caption = XmlCaptionParser::import('input.xml')->parse();
$output = SrtGenerator::load($caption)->export('output.srt');

And for chunking:

use Mokhosh\LaravelXmlToSrt\XmlCaptionParser;
use Mokhosh\LaravelXmlToSrt\SrtGenerator;

$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');

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.

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.