2026-11-23 · 4 min read

How to Caption Videos in Perl

Dark film strip with the glowing lavender neon caption word PERL on a black background.
This image was generated with AI.

Perl still runs an enormous amount of back-office plumbing, and captioning a batch of videos is exactly the kind of job it handles well. The caption.sh API is plain HTTPS, so HTTP::Tiny, which ships with Perl itself, is all you need.

Submit the job

POST the video URL to the API. Documented styling options go in the nested options object.

use HTTP::Tiny;
use JSON::PP;

my $KEY = $ENV{CAPTION_API_KEY};
my $api = "https://api.caption.sh";

sub submit_job {
    my ($video_url) = @_;
    my $res = HTTP::Tiny->new->post("$api/v1/videos", {
        headers => {
            "Authorization" => "Bearer $KEY",
            "Content-Type"  => "application/json",
        },
        content => encode_json({
            source_url => $video_url,
            options    => { caps => , position => "bottom" },
        }),
    });
    return decode_json($res->{content})->{job_id};
}

The response carries a job_id. One job accepts a video up to 10 minutes long; split longer sources into segments and submit one job per segment.

Poll on a deadline

Jobs move through queued, awaiting_upload, and running, then land on done or error. Poll with a deadline and cover all three exits.

sub wait_for_job {
    my ($job_id, $budget) = @_;
    $budget //= 600;
    my $deadline = time() + $budget;
    while (time() < $deadline) {
        my $res = HTTP::Tiny->new->get("$api/v1/jobs/$job_id", {
            headers => { "Authorization" => "Bearer $KEY" },
        });
        my $st = decode_json($res->{content})->{status};
        return 1 if $st eq "done";
        die "caption job failed\n" if $st eq "error";
        sleep 10;
    }
    die "timed out waiting for $job_id\n";
}

A 429 response means the transcription throughput ceiling is saturated. Wait and retry; it clears on its own.

Fetch the finished video

The result endpoint answers with a redirect to the file. HTTP::Tiny follows redirects by default, so disable that and read the Location header yourself.

sub result_url {
    my ($job_id) = @_;
    my $res = HTTP::Tiny->new(max_redirect => 0)->get(
        "$api/v1/jobs/$job_id/result",
        { headers => { "Authorization" => "Bearer $KEY" } },
    );
    return $res->{headers}{location};
}

Results are stored for 24 hours, then deleted, so download the file in the same script. Pricing is prepaid at $0.10 per minute of video.

Where this fits

  • Nightly cron jobs that caption everything new in a watched directory.
  • Older codebases that already talk to the world through Perl.
  • One-off cleanup scripts for a back catalog of clips.

Burned-in captions cannot be toggled, resized, or translated by the viewer. Where accessibility matters, keep the platform's own caption track enabled alongside the burn-in.

Back to all articles