Skip to content
Theo Kouzelis edited this page Feb 13, 2015 · 12 revisions

Create a WebVTT file from scratch:

<?php

use Captioning\Format\WebvttFile;
use Captioning\Format\WebvttCue;
use Captioning\Format\WebvttRegion;

try {
    $vtt = new WebvttFile();

    // optional: add a WebVTT Region
    $region = new WebvttRegion();
    $region
        ->setId('introduction')
        ->setWidth('40%')
        ->setLines(3)
        ->setRegionAnchor('0%,100%')
        ->setViewportAnchor('10%,90%')
        ->setScroll('up');

    $vtt->addRegion($region);

    // add the cues (2 different ways)
    $vtt
        ->addCue("A long time ago,\nin a galaxy far far away", '00:00:01.234', '00:00:02.345')
        ->addCue(new WebvttCue('00:00:03.456', '00:00:04.567', "It is a period of civil war."));

    // build the file content
    $vtt->build();

    // save the file
    $vtt->save('my_sub.vtt');

} catch(Exception $e) {
    echo "Error: ".$e->getMessage()."\n";
}

Convert a Subrip file (.srt) into a WebVTT file (.vtt):

<?php

use Captioning\Format\SubripFile;
use Captioning\Format\WebvttFile;

try {
    $srt = new SubripFile('my_subs.srt');
    $srt->convertTo('webvtt')->save('my_subs.vtt');
} catch(Exception $e) {
    echo "Error: ".$e->getMessage()."\n";
}