What is WebVTT?
For those unfamiliar, WebVTT (Web Video Text Tracks) is a standard format used for displaying subtitles, captions, or chapter markers in web videos. It’s widely supported by modern browsers like Chrome and Firefox and plays a crucial role in making video content accessible, especially for people with hearing impairments.
Why vtt-parser?
The vtt-parser library is a lightweight, dependency-free PHP tool designed to simplify working with WebVTT files.
Key Features
- Load and parse WebVTT files
- Extract timing information
- Validate subtitle structure
Getting Started
To start using vtt-parser, install it via Composer:
composer require webrium/vtt-parser
How to Use It
Let’s walk through a simple example to see how vtt-parser works in action.
Step 1: Include the Library
First, include Composer’s autoloader and import the TextParser class:
require 'vendor/autoload.php';
use Webrium\VttParser\TextParser;
Step 2: Load a WebVTT File
Create an instance of TextParser and load your WebVTT file using the openFile method:
$parser = new TextParser();
$filePath = '/path/to/your/subtitles.vtt';
$fileName = 'subtitles.vtt';
if ($parser->openFile($filePath, $fileName)) {
// File loaded successfully
} else {
// Handle error
}
Step 3: Parse the File
Use the parse method to extract subtitle data:
$subtitles = $parser->parse();
This returns an array of subtitle blocks, like so:
[
[
'time' => [
'start' => '00:00:01.000',
'end' => '00:00:04.000'
],
'text' => 'Hello, world!'
],
// Other blocks...
]
Step 4: Validate the Structure
To check if your WebVTT file is properly formatted, use the textValidation method:
$validation = $parser->textValidation();
if ($validation['ok']) {
echo "Subtitles are valid.";
} else {
echo "Errors: " . implode(', ', $validation['errors']);
}
Use Cases:
- Developing subtitle editing tools
- Analyzing subtitle data for research
Top comments (0)