To auto-generate subtitles with PHP, you can use speech-to-text AI capable of transcribing media files. A video editing API like Creatomate can help you with that. Using its PHP library, you can generate animated, word-by-word captions, directly from your Laravel or any other PHP-based projects:
// Don't forget to install the required library using Composer:
// composer require creatomate/creatomate
$client = new Creatomate\Client('Your API Key');
$output = $client->render([
'source' => new Creatomate\Source([
'output_format' => 'mp4',
'elements' => [
new Creatomate\Elements\Video([
'id' => '17ca2169-786f-477f-aaea-4a2598bf24eb',
'source' => 'https://cdn.creatomate.com/demo/the-daily-stoic-podcast.mp4'
]),
new Creatomate\Elements\Text([
'transcript_source' => '17ca2169-786f-477f-aaea-4a2598bf24eb',
'transcript_effect' => 'highlight',
'transcript_maximum_length' => 14,
'y' => '82%',
'width' => '81%',
'height' => '35%',
'x_alignment' => '50%',
'y_alignment' => '50%',
'fill_color' => '#ffffff',
'stroke_color' => '#000000',
'stroke_width' => '1.6 vmin',
'font_family' => 'Montserrat',
'font_weight' => '700',
'font_size' => '9.29 vmin',
'background_color' => 'rgba(216,216,216,0)',
'background_x_padding' => '31%',
'background_y_padding' => '17%',
'background_border_radius' => '31%'
])
],
])
]);
var_dump($output);
How it works: First, install the Creatomate package from Packagist using Composer. This library provides the interface between your application and the video API, making it possible to run video editing tasks from your PHP code.
In order to create the videos, we call the "render" function and pass instructions to the API about how to compose the video. As shown in this example, we provide a video and a text element. The video element contains the clip that we want to generate subtitles for, whereas the text element is a container for the subtitle captions.
Auto-transcription is enabled with the "transcript_source" attribute of the text element. Specifying the ID of the video element tells the API which video element to use as a source for the auto-generated subtitles.
The remaining attributes are used to style, position, and animate the captions. You can adjust a variety of attributes to customize the subtitles in a particular way. For setting up a custom video template, try out the online video editor below to explore the different subtitle options.
We've also written a step-by-step tutorial on making videos with AI-generated voiceovers and matching subtitles. Even if you're looking to generate subtitles for other types of video or audio files, this guide is helpful for setting up a subtitle generation template.
Auto-transcription is just one of Creatomate's features. With its developer-driven approach, you can automate any video editing task to create dynamic videos, right from your PHP apps. To get started, follow this quick tutorial or check out the API documentation.