Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benlipp/srt-parser
A PHP library to parse SRT files
https://github.com/benlipp/srt-parser
composer php srt srt-parser srt-subtitles
Last synced: 3 months ago
JSON representation
A PHP library to parse SRT files
- Host: GitHub
- URL: https://github.com/benlipp/srt-parser
- Owner: benlipp
- License: other
- Created: 2017-03-28T16:13:58.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-08-28T23:08:59.000Z (over 4 years ago)
- Last Synced: 2024-10-10T14:41:10.179Z (4 months ago)
- Topics: composer, php, srt, srt-parser, srt-subtitles
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 21
- Watchers: 3
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
SRT Parser
=
[![Build Status](https://travis-ci.org/benlipp/srt-parser.svg?branch=master)](https://travis-ci.org/benlipp/srt-parser)A PHP library to parse SRT files.
Built by Ben Lippincott for [LiveTech](http://www.liveteched.com/)Installation / Requirements
-
Run
`composer require "benlipp/srt-parser"`
and let Composer do the work.PHP 7+ is **REQUIRED**! This isn't amateur hour.
Usage
-
Import the `Parser` class: `use Benlipp\SrtParser\Parser;`Use it:
````
$parser = new Parser();$parser->loadFile('/path/to/srtfile.srt');
$captions = $parser->parse();
````
or
````
$parser = new Parser();$parser->loadString($formatted_caption_input);
$captions = $parser->parse();
`````parse()` returns an array of captions. Use them like so:
````
foreach($captions as $caption){
echo "Start Time: " . $caption->startTime;
echo "End Time: " . $caption->endTime;
echo "Text: " . $caption->text;
}
````
A caption can be returned as an array instead of an object, if you prefer. The array is `snake_case` for compatibility with Laravel's attributes.
````
foreach($captions as $caption){
$caption = $caption->toArray();
echo "Start Time: " . $caption['start_time'];
echo "End Time: " . $caption['end_time'];
echo "Text: " . $caption['text'];
}
````
For Laravel usage with a model:
````
$url = "https://youtu.be/dQw4w9WgXcQ";
$video = new Video($url);
foreach ($captions as $caption) {
$data = new VideoMetadata($caption->toArray());
$video->videoMetadata()->save($data);
}
````You can also chain the `parse()` method:
````
$parser = new Parser();
$captions = $parser->loadFile($srtPath)->parse();
````Contributing
-
Run PHPUnit on your changes, pretty please. If you add a new feature, add tests for that feature.