105

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?

Giacomo1968
  • 58,727
vijay
  • 1,061

7 Answers7

179

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.
Giacomo1968
  • 58,727
jm3
  • 2,105
  • 1
  • 13
  • 8
16

works in some circumstances:

ffmpeg -i video.mkv subs.srt

e.g. for a bunch of MKVs containing a single SRT stream. 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.

evilsoup
  • 14,056
9

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.

Pavel Vlasov
  • 1,931
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:

Giacomo1968
  • 58,727
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

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

Mr.X
  • 11