91

I am trying to extract subtitle from video as .srt file, I used the following command:

ffmpeg -i mytestmovie.mkv -vn -an -codec:s:0.1 srt sub.srt

But, I got an error as Unrecognized option codec:s:0:1 So, can you tell me the exact command and how to extract a subtitle as .srt file in video?

0

7 Answers 7

160

Simple:

ffmpeg -i Movie.mkv -map 0:s:0 subs.srt
  • -i: Input file URL/path.
  • -map: Designate one or more input streams as a source for the output file.
  • s:0: Select the subtitle stream.
9
  • 40
    This would download the first subtitle track. If there are several, use 0:s:1 to download the second one, 0:s:2 to download the third one, and so on. Commented Nov 10, 2016 at 19:06
  • 7
    @jm3 Would you happen to know any way to automatically extract all subtitle streams from a file, naming them after their language identifier (e.g. eng, fre, dut etc.)?
    – Fr.
    Commented Dec 1, 2017 at 0:30
  • 2
    why is it so slow when extracting from big MKV container (~4 gb)?
    – user25
    Commented Jun 2, 2018 at 16:24
  • 17
    To extract more subtitles at once you have to duplicate the -map parameters for each file. Also include -c copy so that it just extracts the file without trying to process it: ffmpeg -i Movie.mkv -c copy -map 0:s:0 subs.01.srt -c copy -map 0:s:1 subs.02.srt. Anyway it will take long time because ffmpeg must read whole video file to find all parts of the subtitle streams.
    – Radek Pech
    Commented Apr 20, 2019 at 8:39
  • 3
    @x-yuri FFMPEG's default action is to process (convert) everything on input. Which means if you don't use -c parameter, it will automatically select a codec (or output format for subtitles - and FFMPEG actually CAN convert subtitle formats!) and process it. It may look OK to convert SRT into SRT, but it may mess up the input file if it contains something bad or unknown (wrong encoding, typo in timecode, overlapping or too short subtitles, etc.). So you should always include -c copy if want a 100% copy of the input.
    – Radek Pech
    Commented Dec 17, 2021 at 8:16
16
ffmpeg -i video.mkv subs.srt

Wicked, right?

Worked for me just now, for a bunch of MKVs containing a single SRT stream. Apparently this is the default behavior. My ffmpeg version is 20170516, from ubuntu 16.4

13

-codec:s:0:1 is incorrect. If you use -codec:s:0 then ffmpeg will use the stated codec for the first subtitle stream being passed to the output, if you use -codec:s:1 then it will use it for the second subtitle stream, etc.

You can also use -codec:s to select all output subtitle streams, or -codec:2 to select the third output stream, regardless of what it is.

You are probably confused because the -map option behaves in a different way - there, you have to select which input the selected stream comes from. (so, -map 0:s:0 would take the first subtitle stream from the first input, and feed it to the output). However, -map is for selecting which streams you want to take from the inputs; whereas most of the other options that use stream mapping are for use on the streams after they have been selected (so there's no need to specify which input file they're from), as they are passed to the output.

7
  • evilsoup:i would using the following command E:\FFMpeg_Latest>ffmpeg -i E:\Routine\routine.mkv -vn -an -map 0:s:0 srt E:\Routine\sub.srt,it seems an error i got Unable to find a suitable output format for 'srt',can u tell me the command for extracting an subtitle in video...
    – vijay
    Commented Apr 16, 2013 at 9:38
  • 2
    Try: ffmpeg -i E:\Routine\routine.mkv -map 0:s:0 E:\Routine\sub.srt (ffmpeg should detect that you want srt subtitles from the output file name)
    – evilsoup
    Commented Apr 16, 2013 at 14:31
  • 1
    I used: ffmpeg -i film.mp4 -vn -an -codec:s srt film.srt that should copy all the subtitles to the srt file.
    – Stuart
    Commented Apr 5, 2016 at 19:05
  • @Stuart it doesn't extract all subtitles
    – user25
    Commented Jun 2, 2018 at 17:28
  • 1
    @evilsoup -codec:s is equal to -codec:s:0 so it doesn't select all subtitles... it will extract first text track
    – user25
    Commented Jun 2, 2018 at 18:15
5

Case 1 - a single subtitle in the video:

# as by Berry Tsakala
> ffmpeg -i video.mkv subs.srt

Case 2 - multiple subtitles:

> ffprobe video.mkv
Stream #0:0(eng): Video: h264 (Main)...
Stream #0:1(rus): Audio: ac3, 48000 Hz...
Stream #0:2(eng): Audio: eac3, 48000 Hz...
Stream #0:3(rus): Subtitle: subrip
Stream #0:4(eng): Subtitle: subrip <-- wanted subs
Stream #0:5(eng): Subtitle: subrip
Stream #0:6(eng): Subtitle: subrip

Then if you want stream 0:4, then just use 0:4:

ffmpeg -i video.mkv -map 0:4 subs.srt

The 0:s:0 notion looks pretty useless in this case for me, because it makes us calculating in our heads.

2

Made this bash script for a console phobic friend. It presents a zenity file chooser for video file selection. It then uses ffmpeg to pull all the subtitle tracks out as srt files named "VIDEO_NAME-TRACK-LANG.srt". Added a desktop file so it could be started from the apps menu. Feel free to use as you wish:

1

If the subtitles are separately muxed into the MP4, and not hardcoded, I always use mkvmerge -o output.mkv theinput.mp4. Then use mkvextract tracks output.mkv 2:thetrackidof.vob.

Then use vobsub2srt thetrackidof (with "thetrackidof" being the file name of the subtitle files without IDX or VOB file extensions). Works every time.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 9, 2023 at 14:49
-1

updated the code to v6 ver.

work pretty good.

usage:

ffmpegSubExtract Mp4 Srt

ffmpegSubExtract mkv Srt 1

@echo off
cls
title ffmpegSubExtract TOOL

if "%1"=="" (
echo.
echo Usage:
echo ffmpegSubsubExtract mp4 srt
echo ffmpegSubsubExtract mp4 srt 1
goto :eof )

if "%2"=="" (
echo.
echo Usage:
echo ffmpegSubsubExtract mp4 srt
echo ffmpegSubsubExtract mp4 srt 1
goto :eof )

set fleExt=%1
set subExt=%2
set stream=%3

for %%i in ("*.%fleExt%") do (
    set fname=%%i
    call :process
    )
    
goto :eof

:process
if exist "%fname:~0,-4%.%subExt%" (
    del "%fname:~0,-4%.%subExt%" )

if "%stream%"=="" (
    set stream=0 )


if "%subExt%" == "srt" (
    title ffmpegSubExtract TOOL ~ SubRip mode
    ffmpeg -i "%fname%" -map 0:s:%stream% "%fname:~0,-4%.%subExt%"
) else (
    title ffmpegSubExtract TOOL ~ "%subExt%" mode
    ffmpeg -i "%fname%" -c copy -map 0:s:%stream% "%fname:~0,-4%.%subExt%" 
)

goto :eof

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .