How to Add Text to Video using PHP?

Because a large percentage of videos are viewed with the sound muted, putting text in videos might be a good idea. That's especially true if you're creating social media videos. In PHP, this can be accomplished through the use of a video editing API. The following example illustrates how to do this in PHP using only a few lines of code:

Input Code
// 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' => [
   [
    'type' => 'video',
    'track' => 1,
    'source' => 'https://cdn.creatomate.com/demo/mountains.mp4'
   ],
   [
    'type' => 'text',
    'track' => 2,
    'y' => '63.12%',
    'width' => '90.48%',
    'height' => '15.12%',
    'x_alignment' => '50%',
    'y_alignment' => '50%',
    'text' => 'Add your custom text here. ✍️',
    'font_family' => 'Roboto',
    'font_weight' => '500',
    'font_size_maximum' => '10 vmin',
    'background_color' => '#ffffff',
    'background_x_padding' => '52%',
    'background_border_radius' => '26%'
   ]
  ]
 ])
]);

var_dump($output);
Output Video

How it works: Install the Creatomate library from Packagist. After that, build a scene with two elements. The video element should be placed on track 1, while the text element should be located on track 2. The reason for this is that we want the elements to appear at the same time, so we have to use separate track numbers.

To ensure that the text adjusts to the dimensions of the background video, use relative units (%) for the position attributes. This way, the text will automatically adapt based on the size of the video.

You can change the font properties to get the desired text style. For example, if you want to replicate the popular Instagram aesthetic, give the font a rounded background. If you want the TikTok style, go for a thick black text stroke. Additionally, the text element supports emojis, so feel free to include them in your text.

To experiment with different styles and animations, use the online code video editor. This allows you to create custom video scenes that can then be imported into PHP.

Edit this Code Example in the Video Editor

Easily design your own video templates using the online editor. Then, export your templates as JSON and integrate them into your PHP application to create any kind of video – completely through code.

Related PHP Video Editing Questions

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free