Add SRT subtitle to video with ffmpeg
Asked Answered
A

6

10

I use ffmpeg to encode, and add subtitle to a video by following command

$ ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec copy  hifi.m4v -newsubtitle

Here is the output

ffmpeg version 0.8.git, Copyright (c) 2000-2011 the FFmpeg developers
  built on Aug  4 2011 11:11:39 with gcc 4.5.2
  configuration: --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib --disable-shared --enable-static --enable-gpl --enable-postproc --enable-pthreads --enable-ffplay --disable-ffserver --enable-memalign-hack --enable-nonfree --enable-libfaac --arch=x86 --enable-swscale --enable-libx264 --enable-avfilter --enable-debug=3
  libavutil    51. 11. 1 / 51. 11. 1
  libavcodec   53.  9. 1 / 53.  9. 1
  libavformat  53.  6. 0 / 53.  6. 0
  libavdevice  53.  2. 0 / 53.  2. 0
  libavfilter   2. 27. 5 /  2. 27. 5
  libswscale    2.  0. 0 /  2.  0. 0
  libpostproc  51.  2. 0 / 51.  2. 0
Input #0, avi, from 'hifi.avi':
  Metadata:
    encoder         : VirtualDubMod 1.5.4.1 (build 2178/release)
    IAS1            : English
  Duration: 01:49:02.20, start: 0.000000, bitrate: 897 kb/s
    Stream #0.0: Video: mpeg4 (Simple Profile), yuv420p, 544x304 [SAR 1:1 DAR 34:19], 25 tbr, 25 tbn, 25 tbc
    Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 32 kb/s
[srt @ 0152c100] Estimating duration from bitrate, this may be inaccurate
Input #1, srt, from 'hifi.srt':
  Duration: N/A, start: 56.080000, bitrate: N/A
    Stream #1.0: Subtitle: srt
File 'hifi.m4v' already exists. Overwrite ? [y/N] y
[buffer @ 02548920] w:544 h:304 pixfmt:yuv420p tb:1/1000000 sar:1/1 sws_param:
[scale @ 02558ee0] w:544 h:304 fmt:yuv420p -> w:480 h:240 fmt:yuv420p flags:0x4
[libx264 @ 02547f20] Default settings detected, using medium profile
[libx264 @ 02547f20] using SAR=1/1
[libx264 @ 02547f20] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
[libx264 @ 02547f20] profile High, level 3.0
[libx264 @ 02547f20] 264 - core 115 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=0 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=256 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=10000 vbv_bufsize=10000 nal_hrd=none ip_ratio=1.40 aq=1:1.00
Output #0, ipod, to 'hifi.m4v':
    Stream #0.0: Video: libx264, yuv420p, 480x240 [SAR 1:1 DAR 2:1], q=2-31, 256 kb/s, 90k tbn, 25 tbc
    Stream #0.1: Audio: libfaac, 48000 Hz, 2 channels, s16, 128 kb/s
    Stream #0.2: Subtitle: srt
    Stream #0.3: Subtitle: [0][0][0][0] / 0x0000, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
  Stream #1.0 -> #0.2
  Stream #1.0 -> #0.3
Encoder (codec id 0) not found for output stream #0.3

Can anyone tell what is wrong with my command?

Appling answered 30/8, 2011 at 7:58 Comment(0)
A
14

The MP4 container does not support SubRip subtitles, only a special mov_text format.

Option 1: Convert subtitles to mov_text

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec mov_text hifi.m4v

Option 2: Use an MKV container

iPod will not support this, however on iPod touch you can install third party software to play MKV files.

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec copy hifi.mkv

Option 3: Burn in the subtitles to the video

May require additional steps to work, especially if you're on Windows. See https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo for more information.

ffmpeg -i hifi.avi -vf subtitles=hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k hifi.m4v
Apuleius answered 3/7, 2015 at 16:20 Comment(2)
Nice i was looking what the end subtitle format is, as you stated, its mov_text, is there any other container which supports subrip?Kanishakanji
With both options 1 & 2, I get "File for preset 'ipod640' not found" (ffmpeg 6.0)Olivo
A
4

Here is the command to Add srt file to video using FFmpeg in Windows Environment

ffmpeg -i Video.wmv -i hi.srt -map 0 -map 1 -c copy -crf 23 video-with-subtitles.mkv
Agustin answered 10/4, 2019 at 4:45 Comment(2)
-crf 23 is ignored when using -c copy, so you can omit -crf 23. You should properly format your command so it looks nicer like the other answers.Ferrara
For a moment it seemed that I solved this subtitles pain ... But it was only because the MKV file that I created had the same main name with my SRT so VLC used it to display subtitles. So, the pain remains. I cannot find a way to show SRT (or other type) subtitles embedded into an MP4 or SubRip subtitles embedded into a MKV file ...Olivo
E
2

It appears that two subtitle tracks are trying to be added, one of which has no codec. More recent versions of ffmpeg do not use -newsubtitle, so that might be your problem.

Try leaving off -newsubtitle.

Excitation answered 5/11, 2011 at 8:4 Comment(1)
thanks, but it doesnt works, I found out that ffmpeg doesnt support text streams in mp4 and m4v container. It does work with mkv. I changed to mplayer/mencoder.Appling
A
2

very first install 'libass' library in your linux. after that configure ffmpeg with the command

./configure --enable-libass --disable-yasm --enable-libfreetype 

after that copy your srt in the ffmpeg directory then use these command

ffmpeg -i subtitle.srt subtitle.ass
ffmpeg -i input.flv -vf ass=subtitle.ass output.flv
Allcot answered 9/4, 2014 at 10:4 Comment(1)
The OP's subtitles are in SubRip format, not Advanced SubStation. However, if burning in the subtitles is desired, subtitles=hifi.srt can be used instead, as mentioned in my answer.Apuleius
F
0

Batch file.

Just drop 2 files on it at the same time - .mp4 and .srt to get a .mkv video with embedded subtitles.

If you want the original/low video quality, just comment/uncomment the appropriate line in the code.

Among all other things, I made an asynchronous call of msgBox. Who needs it - copy it to your sources.

For some reason, it doesn't accept parentheses () in file names. I did not check the rest of the characters.

    @echo off
    if "%~x1"==".mp4" (
        set mp4=%~n1
        set path=%~d1%~p1
    )
    if "%~x2"==".mp4" (
        set mp4=%~n2
        set path=%~d1%~p1
    )
    if "%~x1"==".srt" set srt=%~1
    if "%~x2"==".srt" set srt=%~2
    if not "%mp4%"=="" (
        if not "%srt%"=="" (
            :: If your original ffmpeg.exe file located in system32 directory then replace "%~dp0ffmpeg.exe" to ffmpeg
            :: original quality
            ::"%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -vcodec libx264 -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            :: low quality
            "%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -ab 128k -ac 2 -vcodec libx264 -s 480x240 -b 256k -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            call :msgbox "File %path%%mp4%.mkv completed."
        ) else call :msgbox "File .srt missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    ) else call :msgbox "File .mp4 missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    goto :eof
    :msgBox [msgText]
    C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -Command """"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"""" """"[System.Windows.Forms.MessageBox]::Show(\""""%~1\"""")"""""",0,false:close")
    exit /B
Forbearance answered 21/10, 2020 at 14:19 Comment(0)
S
-1

Better

ffmpeg -i infile.mp4 -f srt -i infile.srt -c:v copy -c:a copy -c:s mov_text outfile.mp4
Seep answered 11/7, 2013 at 2:55 Comment(2)
Why is this "better"?Knob
FYI, a whole bunch of your old answers are popping up in the Low Quality Posts review queue right now, including this one and an exact duplicate answer on another question. Mostly ones that have no context or explanation, just "copy this and trust me".Knob

© 2022 - 2024 — McMap. All rights reserved.