Hardcoding FFMPEG subtitles on MP4 / MKV

ffmpeglibavsubtitlesvideo

I am using FFMPEG to hard code subs into a video. Having read many posts on the best approach, and whether to use mkv or mp4, but am still none the wiser. The code below uses an mp4 file. The subtitles appear to be processed, judging by the output, but the size 0kb seems to show that nothing is actually rendered, and the output file has audio and video, but no subtitles.

Metadata in VLC shows that a subtitle file is present – but empty.

Here is an example of my SRT file (not putting an empty line in 1 gives an input error msg):

1
00:00:00,000 --> 00:00:00,001

2
00:00:00,001 –-> 00:00:04,761
He drives to school every morning

3
00:00:04,761 –-> 00:00:13,061
Cause love comes slow and it goes so fast

My command line code is:

`ffmpeg -i #{inputfile} -i #{srt_file}  -filter_complex "[0:v][1:s]overlay" #{outputfile} -y`

I have also tried:

`ffmpeg -i #{inputfile} -i #{srt_file}  -c:v libx264 -preset ultrafast \
-c:s mov_text -map 0 -map 1 #{outputfile}`

And the response:

ffmpeg version 2.5.4 Copyright (c) 2000-2015 the FFmpeg developers
built on Feb 25 2015 15:52:19 with Apple LLVM version 6.0 (clang-600.0.54) 
(based on LLVM3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.4 --enable-shared --enable-pthreads
--enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --
host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc 
--enable-libxvid --enable-libass --enable-vda
libavutil      54. 15.100 / 54. 15.100
libavcodec     56. 13.100 / 56. 13.100
libavformat    56. 15.102 / 56. 15.102
libavdevice    56.  3.100 / 56.  3.100
libavfilter     5.  2.103 /  5.  2.103
libavresample   2.  1.  0 /  2.  1.  0
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 
'/Users/JW/Dropbox/T10/SBRI/_code/videos_final/3rd person present tense (303).mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf56.15.102
Duration: 00:00:43.17, start: 0.036281, bitrate: 862 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x406 [SAR 406:405 DAR 
16:9], 732 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
Metadata:
  handler_name    : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s    
(default)
Metadata:
  handler_name    : SoundHandler
Input #1, srt, from '/Users/JW/Dropbox/T10/SBRI/_code/subs/3rd person present tense (303)/srt_file.srt':
Duration: N/A, bitrate: N/A
Stream #1:0: Subtitle: subrip
[srt @ 0x7fd082005c00] sub2video: using 720x576 canvas
[libx264 @ 0x7fd082801200] using SAR=406/405
[libx264 @ 0x7fd082801200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fd082801200] profile High, level 3.0
[libx264 @ 0x7fd082801200] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC codec - Copyleft     
2003-2014 - 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=6 
lookahead_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=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf 
mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00

Output #0, mp4, to '/Users/JW/Dropbox/T10/SBRI/_code/videos_final/3rd person present tense 
(303)_subs.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf56.15.102
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 720x406 [SAR 406:405 
DAR 16:9], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default)
Metadata:
  encoder         : Lavc56.13.100 libx264
Stream #0:1(und): Audio: aac (libvo_aacenc) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, 
s16, 128 kb/s (default)
Metadata:
  handler_name    : SoundHandler
  encoder         : Lavc56.13.100 libvo_aacenc

Stream mapping:
Stream #0:0 (h264) -> overlay:main (graph 0)
Stream #1:0 (srt) -> overlay:overlay (graph 0)
overlay (graph 0) -> Stream #0:0 (libx264)
Stream #0:1 -> #0:1 (aac (native) -> aac (libvo_aacenc))

Press [q] to stop, [?] for help
sub2video: non-bitmap subtitle
frame= 1029 fps= 71 q=-1.0 Lsize=    4319kB time=00:00:43.17 bitrate= 819.6kbits/s
video:3612kB audio:675kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:     
0.751962%
[libx264 @ 0x7fd082801200] frame I:44    Avg QP:17.59  size: 16172
[libx264 @ 0x7fd082801200] frame P:495   Avg QP:22.62  size:  5016
[libx264 @ 0x7fd082801200] frame B:490   Avg QP:23.28  size:  1027
[libx264 @ 0x7fd082801200] consecutive B-frames: 24.2% 34.4%  7.6% 33.8%
[libx264 @ 0x7fd082801200] mb I  I16..4: 27.2% 43.8% 28.9%
[libx264 @ 0x7fd082801200] mb P  I16..4:  5.9% 14.8%  2.9%  P16..4: 31.1% 10.7%  4.3%  0.0%  
0.0%    skip:30.3%
[libx264 @ 0x7fd082801200] mb B  I16..4:  0.2%  0.5%  0.2%  B16..8: 26.2%  3.3%  0.6%   
direct: 1.1%  skip:67.8%  L0:40.7% L1:52.4% BI: 6.9%
[libx264 @ 0x7fd082801200] 8x8 transform intra:57.6% inter:72.8%
[libx264 @ 0x7fd082801200] coded y,uvDC,uvAC intra: 41.3% 60.2% 22.0% inter: 9.1% 14.5% 0.6%
[libx264 @ 0x7fd082801200] i16 v,h,dc,p: 41% 25% 11% 22%
[libx264 @ 0x7fd082801200] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 20% 23%  4%  5%  6%  5%  5%  4%
[libx264 @ 0x7fd082801200] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 20% 12%  5%  8%  9%  7%  6%  4%
[libx264 @ 0x7fd082801200] i8c dc,h,v,p: 53% 20% 21%  6%
[libx264 @ 0x7fd082801200] Weighted P-Frames: Y:14.1% UV:9.9%
[libx264 @ 0x7fd082801200] ref P L0: 68.6% 14.5% 12.3%  4.2%  0.4%
[libx264 @ 0x7fd082801200] ref B L0: 91.7%  7.5%  0.8%
[libx264 @ 0x7fd082801200] ref B L1: 98.4%  1.6%
[libx264 @ 0x7fd082801200] kb/s:689.28

What am I doing wrong?

Best Answer

sub2video: non-bitmap subtitle

That's your error. You have text-based subtitles, so you need to burn them with the subtitles filter.

ffmpeg -i input.mp4 -filter:v subtitles=subtitles.srt output.mp4

The overlay filter only works when your subtitles are images, which is the case for dvdsub, for example.

Furthermore, the problem seems to be that your subtitle file is not formatted correctly. Your time stamp definition contains an en-dash () instead of a normal minus-hyphen (-):

–->

Replace it with a correct minus-hyphen:

-->
Related Question