DEV Community

Adam King
Adam King

Posted on Originally published at pennycompute.com

Transcode, resize and trim video to MP4 with one API call

Users upload a 4K phone clip, a screen recording at a strange frame rate, or a 20-minute file when you only need 30 seconds. You want one H.264 MP4 at a predictable size, and you don't want to run an FFmpeg worker fleet to get it. This walkthrough uses PennyCompute's ffmpeg.transcode.v1 job to convert, resize and trim a video with curl. At the end it uses ffmpeg.frames.v1 to make a thumbnail. The same flow is on the transcode video guide. Every path and field below comes from the public OpenAPI document at https://api.pennycompute.com/openapi.json (buyer API 1.10.2).

What you need

  • A public https link to the source video, up to 1 GB, or an input_url from POST /v1/assets. The renderer fetches it with a plain GET, so keep the link readable until the job has ended.
  • A PennyCompute account and a server API key from https://app.pennycompute.com. Send it as Authorization: Bearer $PENNY_API_KEY on every API call.

A new account starts with $5 in its prepaid balance. Failed, cancelled and expired jobs are not charged.

The transcode fields

ffmpeg.transcode.v1 requires only input_url. The optional fields used in this post are:

Field What it does
height Output height in pixels (1 to 2160). Omit it to keep the source height.
fps Output frame rate (1 to 120).
keep_fps_at_least Only with fps: a source whose own rate is at least this value keeps its rate.
start_seconds Keep one part of the source, starting here. It can be sent without duration_seconds.
duration_seconds How much of the source to keep.
video_codec h264_videotoolbox (default; hardware encoding on the renderer) or libx264 (software).
audio_codec aac (default) or copy.
strip_audio true gives an output with no sound stream.

The spec has a few more fields: overlay_image_url for a full-frame watermark image, replace_audio_url and replace_audio_in_seconds for a new soundtrack, and pad_to_duration_seconds to hold the last frame up to a set length. audio_codec: "copy" works with a cut, a frame rate and an overlay. It is refused together with replace_audio_url, and with pad_to_duration_seconds unless strip_audio is set, because both of those write new sound.

What a transcode costs

Transcoding is billed as FFmpeg output. The live rates on https://pennycompute.com/ffmpeg-api-pricing are $0.0075 per minute of HD output, $0.00375 per minute of SD and $0.015 per minute of 4K, billed by the second with a 10-second minimum per output. The rate follows the output's shorter side: SD is below 720 lines, HD is up to 1080 lines, and 4K is above that. So height sets your price as well as your file size.

Worked from that rate card:

  • A 3-minute source resized to 720p is HD: 3 × $0.0075 = $0.0225.
  • The same source as a 480p preview is SD: 3 × $0.00375 = $0.01125.
  • A 30-second trim at 1080p is HD: 0.5 × $0.0075 = $0.00375.

These numbers are arithmetic, not benchmarks. Queue time is not charged. POST /v1/jobs/estimate takes the same body as a job and creates nothing. For FFmpeg its price_is_final is false, because the finished job is billed for the length of its actual output.

1. Estimate the job

export API=https://api.pennycompute.com

curl -sS -X POST "$API/v1/jobs/estimate" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "service_id": "ffmpeg.transcode.v1",
    "input": {
      "input_url": "https://example.com/upload.mov",
      "height": 720
    }
  }'
Enter fullscreen mode Exit fullscreen mode

The response includes price_micros (millionths of a dollar), estimated_render_seconds, free_slots, queued_jobs and a low/high estimated_complete_seconds.

2. Submit: convert to a 720p H.264 MP4

POST /v1/jobs requires an Idempotency-Key header of 8 to 128 characters. If you resend the same key with the same body, you get the original job back. No second job is created or charged, so a submit that timed out is safe to retry.

curl -sS -X POST "$API/v1/jobs" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: transcode-720p-demo-001" \
  -d '{
    "service_id": "ffmpeg.transcode.v1",
    "input": {
      "input_url": "https://example.com/upload.mov",
      "height": 720
    }
  }'
Enter fullscreen mode Exit fullscreen mode

A new job is created as queued. Save its id. The same body also accepts max_wait_seconds (default 900, maximum 86400), client_reference, priority and webhook_endpoint_id.

Variant: trim a clip, cap it at 30 fps, drop the audio

This body keeps 30 seconds starting at 0:12 and outputs 1080p at 30 fps with no sound, encoded in software:

curl -sS -X POST "$API/v1/jobs" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: transcode-trim-demo-001" \
  -d '{
    "service_id": "ffmpeg.transcode.v1",
    "input": {
      "input_url": "https://example.com/upload.mov",
      "start_seconds": 12,
      "duration_seconds": 30,
      "height": 1080,
      "fps": 30,
      "video_codec": "libx264",
      "strip_audio": true
    }
  }'
Enter fullscreen mode Exit fullscreen mode

To normalise only sources that are slower than 30 fps, add "keep_fps_at_least": 30 next to fps. A 60 fps source then keeps its 60 fps.

3. Poll until the status is final

JOB_ID=your-job-id

curl -sS "$API/v1/jobs/$JOB_ID" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json"
Enter fullscreen mode Exit fullscreen mode

status is one of queued, active, cancelling, succeeded, failed, cancelled or expired. Keep polling until it is one of the last four. Only succeeded is charged, and charged_fee_micros shows the final amount. When the job finishes, result reports the measured duration_seconds, width and height. If you would rather not poll, register a webhook with POST /v1/webhook-endpoints and pass its id as webhook_endpoint_id.

4. Download the MP4

curl -sS "$API/v1/jobs/$JOB_ID/output" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json"
Enter fullscreen mode Exit fullscreen mode

The response is {"url": "https://..."}. That link needs no API key. The file is kept for 3 days after the render finishes, and after that the link answers 404. Copy the file to your own storage if you need it longer. Before the job succeeds, this call returns 409 output_not_ready.

5. Bonus: a thumbnail from the new MP4

ffmpeg.frames.v1 turns frames into one PNG, JPEG or WebP. If you send one cell and no cell size, you get a plain frame grab. at can be first, middle (the default), last, or a time in seconds. max_width scales the image down, never up.

curl -sS -X POST "$API/v1/jobs" \
  -H "Authorization: Bearer $PENNY_API_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: thumb-demo-001" \
  -d '{
    "service_id": "ffmpeg.frames.v1",
    "input": {
      "cells": [
        { "kind": "video", "url": "https://example.com/your-transcoded.mp4", "at": 4.5 }
      ],
      "format": "jpg",
      "quality": 85,
      "max_width": 1280
    }
  }'
Enter fullscreen mode Exit fullscreen mode

For a contact sheet, send several cells with columns, cell_width and cell_height. You can use up to 64 cells, each letterboxed on black. The live price for one image is $0.000625 (FFmpeg pricing). When the job finishes, result lists the second each frame was taken from under cells.

Optional: upload a local file first

Any public https link works without an upload. If the file is only on your machine, call POST /v1/assets with the file's SHA-256 and content type. Then PUT the bytes to the returned upload_url, and use the returned input_url as the job's input_url. The PUT needs no API key. A single-file upload can be up to 100 MB; pass a larger source by its own public link, up to 1 GB. Uploaded files stop being served 24 hours after their PUT.

If something goes wrong

  • 400 invalid_request: the issues list names each field that was refused.
  • 402 insufficient_balance: add funds in the dashboard and submit again.
  • 409 no_capacity: no renderer can take the job right now; submit again later.
  • failed with failure_code download_failed: the renderer could not fetch input_url. failure_message names the link.
  • expired with failure_code no_capacity: the job waited max_wait_seconds without starting and was not charged.

What this job does not do: ffmpeg.transcode.v1 sets height, not a crop, so it keeps the source's aspect ratio. Check GET /v1/services and the API changes page for the current list of services.

OpenAPI: https://api.pennycompute.com/openapi.json. Docs: https://pennycompute.com/docs. Pricing: https://pennycompute.com/pricing. Billing terms: https://pennycompute.com/billing-terms.

Disclosure: I build PennyCompute.

— Adam King, PennyCompute

Top comments (0)