Extract subtitles from MKV and MP4 with FFmpeg

Run ffmpeg -i in.mkv -map 0:s:0 -c copy out.srt to extract the first subtitle track from an MKV as an SRT file. 0:s:0 counts subtitle tracks only, from zero, and -c copy writes the cues unchanged. Leave out -c copy when the format changes: an MP4's mov_text track, or output to WebVTT or ASS.

First track Stream copy

The file has one subtitle track, or you already know which one you want.

ffmpeg -i in.mkv -map 0:s:0 -c copy out.srt

All 13 English cues, 0:00.000 to 1:54.250. The file is byte for byte the SRT that went into the MKV.

Cues
13 of 13
Against the source SRT
byte for byte
Run time
71 ms
Output
1 KB
By language Stream copy

The file has several languages and you want one by its tag, wherever it sits.

ffmpeg -i in.mkv -map 0:s:m:language:spa -c copy out.srt

The Spanish track, 13 cues in UTF-8, picked by its spa tag. It is the same file -map 0:3 gives, without looking up where the track sits.

Picked
track 0:3, spa
Cues
13 of 13
Run time
68 ms
Output
1 KB

Proof

The extracted files, drawn back on the film

Each extracted file was drawn onto the video with FFmpeg's subtitles filter at the time its cue says, to show the text and the timing survived.
-map 0:s:0 -c copy out.srt Right words at the right time
Sintel close-up at 0:22 with the subtitle Someone very dear? A kindred spirit? drawn from the extracted English SRT
0:22 Cue 7, timed 0:20.000 to 0:24.200 in the extracted file, on the frame at 0:22.
-map 0:s:m:language:spa -c copy out.srt Accents intact, UTF-8
The baby dragon in Sintel's arms at 1:34 with the subtitle Oye, quédate quieto drawn from the extracted Spanish SRT
1:34 Cue 12 of the Spanish test track. The é in quédate came through as written.
ASS track copied vs SRT track converted Copy keeps the styling
The same cue at 0:32 drawn twice: in gold Georgia from the copied ASS track, and in plain white Arial from the SRT track converted to ASS
0:32 Copying the ASS track keeps its Georgia 22, gold, 2 px outline. Converting the SRT track to ASS gives FFmpeg's default style, Arial 16 in white.
out.srt, out.vtt, out.ass Same cues, three formats
The first two cues of the English track as SRT, WebVTT and ASS text
13 cues SRT numbers each cue and uses a comma before the milliseconds. WebVTT starts with WEBVTT and uses a dot. ASS writes one Dialogue line per cue, in hundredths of a second.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Scaled to 854x364 by us. Spanish subtitles translated by us as a test track.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mkv": "https://cdn.rendobar.com/examples/extract-subtitles/in.mkv",
},
params: {
command: "ffmpeg -i in.mkv -map 0:s:0 -c copy out.srt",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mkv": "https://cdn.rendobar.com/examples/extract-subtitles/in.mkv"
},
"params": {
"command": "ffmpeg -i in.mkv -map 0:s:0 -c copy out.srt"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. List the subtitle tracks in a file

    Read only
    ffprobe -v error -select_streams s -show_entries stream=index,codec_name:stream_tags=language,title -of csv=p=0 in.mkv

    Three lines, one per track: 2,subrip,eng,English, 3,subrip,spa,Spanish (test track), 4,ass,eng,English (styled). The first number is the stream index -map 0:2 takes.

    67 ms run

  2. Extract subtitles from an MP4 to SRT

    Re-encode
    ffmpeg -i in.mp4 -map 0:s:0 out.srt

    13 cues, byte for byte the source SRT. The MP4 stores them as mov_text, so FFmpeg converts them, which is why there is no -c copy.

    67 ms run 1 KB

  3. Extract subtitles to WebVTT (.vtt)

    Re-encode
    ffmpeg -i in.mkv -map 0:s:0 out.vtt

    A WEBVTT file with all 13 cues, 101 bytes smaller than the SRT because WebVTT drops the cue numbers and the hours when they are zero.

    71 ms run 1 KB

  4. Extract an ASS track and keep its styling

    Stream copy
    ffmpeg -i in.mkv -map 0:s:2 -c copy out.ass

    13 Dialogue lines and the track's own Style: Default,Georgia,22 line, unchanged. Converting this track to SRT instead turns the style into font tags on every cue.

    82 ms run 2 KB

  5. Convert an SRT track to ASS

    Re-encode
    ffmpeg -i in.mkv -map 0:s:0 out.ass

    13 Dialogue lines under FFmpeg's default style, Arial 16 in white. Restyle every cue at once by editing that one Style: line.

    70 ms run 2 KB

  6. Extract every subtitle track, one file each

    Stream copy
    ffmpeg -i in.mkv -map 0:s:0 -c copy eng.srt -map 0:s:1 -c copy spa.srt -map 0:s:2 -c copy eng.ass

    eng.srt, spa.srt and eng.ass from one read of the file. Each -map applies to the output named after it. -map 0:s out_%d.srt does not do this: it fails with "srt muxer does not support more than one stream of type subtitle".

    71 ms run 3 KB

  7. Extract all subtitle tracks into one file

    Stream copy
    ffmpeg -i in.mkv -map 0:s -c copy subs.mkv

    A 4 KB MKV with only the three subtitle tracks, their language and title tags kept. Name it .mkv: FFmpeg 8.0 stops on subs.mks with "Unable to choose an output format".

    71 ms run 4 KB 114.25 s long

  8. Convert an SRT file to WebVTT

    Re-encode
    ffmpeg -i subs.srt subs.vtt

    13 cues in WebVTT, identical to the file extracted straight from the MKV to .vtt.

    67 ms run 1 KB

Gotchas

Where it goes wrong

Why does -c copy fail on an MP4's subtitles?

MP4 stores text subtitles as mov_text, and an .srt file can only hold SRT. A copy would put mov_text in an SRT file, so FFmpeg refuses:

ffmpeg -i in.mp4 -map 0:s:0 -c copy out.srt

"Unsupported subtitles codec: mov_text", then "Could not write header". The out.srt it leaves behind is empty.

Drop -c copy and FFmpeg converts the cues. From our MP4 that gave an SRT identical, byte for byte, to the one we muxed in. The same goes for any change of format: SRT to WebVTT, SRT to ASS, ASS to SRT.

-map 0:s:1 or -map 0:3, which number picks the track?

Both work, counted differently. 0:3 is the stream index, counting video and audio too, the number ffprobe prints. 0:s:1 counts subtitle tracks only. In our MKV the Spanish track is 0:3 and 0:s:1, and both gave the same file.

Mix them up and the map points past the last track. Taking ffprobe's 3 as a subtitle number:

ffmpeg -i in.mkv -map 0:s:3 -c copy out.srt

"Stream map '' matches no streams", and nothing is written. The MKV has three subtitle tracks, 0:s:0 to 0:s:2.

A language tag avoids the counting: -map 0:s:m:language:spa. When two tracks share the tag, as our English SRT and English ASS do, language:eng maps both and the SRT output fails with "srt muxer does not support more than one stream of type subtitle". Use the index then.

Why are lines missing after converting an SRT?

The file is not UTF-8. FFmpeg reads text subtitles as UTF-8 and silently skips every cue it cannot decode, with exit code 0. Our Latin-1 copy of the Spanish track:

ffmpeg -i latin1.srt out.vtt

7 of 13 cues written. All six cues with á, é, ó or ¿ are gone, and the log says "Invalid UTF-8 in decoded subtitles text; maybe missing -sub_charenc option".

Name the encoding before -i:

ffmpeg -sub_charenc ISO-8859-1 -i latin1.srt out.vtt

13 of 13 cues, written as UTF-8 with every accent in place.

Can FFmpeg turn PGS or DVD subtitles into SRT?

No. PGS (Blu-ray) and VobSub (DVD) tracks are pictures of text, and FFmpeg converts subtitles text to text or picture to picture, never across. It says so when asked for the other direction, a text track into the DVD picture format:

ffmpeg -i in.mkv -map 0:s:0 -c:s dvdsub out.mkv

"Subtitle encoding currently only possible from text to text or bitmap to bitmap". Nothing is written.

Related: Add subtitles, FFprobe video info, Convert to MP4, Strip metadata.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.