Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE


adding subtitles to mp4 file

Discussions about problems encountered using ffmpeg.
Forum rules
FFmpeg is in a state of perpetual development and we do not currently have the person power to backport fixes to official releases. As such, if you wish to query or report a bug, you must try with the latest development branch revision of FFmpeg to confirm the issue still exists. The latest FFmpeg can be downloaded from here: http://www.ffmpeg.org/download.html

adding subtitles to mp4 file

Postby TaiChiRabbit » Fri Apr 26, 2013 5:16 pm

Hello
ffmpeg newbie here and have made enjoyable progress today learning to convert an MKV file to MP4 file for my PS3, but have become stuck when adding the subtitles to the MP4 file since this file then becomes much smaller (I presume I'm loosing quality, but maybe not....).

The MKV file has three tracks:
Track ID 1: video (V_MS/VFW/FOURCC, XVID)
Track ID 2: audio (A_AC3)
Track ID 3: subtitles (S_TEXT/UTF8)

To create the MP4 file:
Code: Select all
ffmpeg -i file.mkv -vcodec copy -acodec copy newfile.mp4


To extract the subtitles [can i do this with ffmpeg?]:
Code: Select all
mkvextract tracks file.mkv 3:newfile.srt


I then add the SRT to the MP4 file without subtitles, creating an MP4 file with subtitles [detailed output quoted at end of this post]:
Code: Select all
ffmpeg -i newfile.mp4 -vf subtitles=newfile.srt newfilewithsubs.mp4


However, newfilewithsubs.mp4 is considerably smaller (400MB vs 1.4GB for newfile.mp4 and file.mkv) and it looks like the video and audio codecs have been converted (mpeg4 -> libx264,ac3 -> libfaac). Should I be bothered about this and is the conversion merely making the file more efficient and no loss of quality? I've googled around the forums but cannot see what I'm doing wrong (it's probably too simple!) so can anyone help?

Thanks
Guy



ffmpeg -i newfile.mp4 -vf subtitles=newfile.srt newfilewithsubs.mp4
ffmpeg version git-2013-04-26-05f4c05 Copyright (c) 2000-2013 the FFmpeg developers
built on Apr 26 2013 10:01:00 with gcc 4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1)
configuration: --enable-gpl --enable-libass --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-x11grab --enable-libx264 --enable-nonfree --enable-version3
libavutil 52. 27.101 / 52. 27.101
libavcodec 55. 6.100 / 55. 6.100
libavformat 55. 3.100 / 55. 3.100
libavdevice 55. 0.100 / 55. 0.100
libavfilter 3. 60.102 / 3. 60.102
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 3.100 / 52. 3.100
[mpeg4 @ 0x2e61140] Invalid and inefficient vfw-avi packed B frames detected
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'newfile.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf55.3.100
Duration: 01:45:19.31, start: 0.000000, bitrate: 1825 kb/s
Stream #0:0(und): Video: mpeg4 (Advanced Simple Profile) (mp4v / 0x7634706D), yuv420p, 720x400 [SAR 1:1 DAR 9:5], 1628 kb/s, 23.98 fps, 23.98 tbr, 16k tbn, 23.98 tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, mono, fltp, 192 kb/s
Metadata:
handler_name : SoundHandler
Input #1, srt, from 'newfile.srt':
Duration: N/A, bitrate: N/A
Stream #1:0: Subtitle: subrip
[libx264 @ 0x2e881e0] using SAR=1/1
[libx264 @ 0x2e881e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x2e881e0] profile High, level 3.0
[libx264 @ 0x2e881e0] 264 - core 132 r2 3b1f1f7 - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - MISSINGURL - 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 'newfilewithsubs.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf55.3.100
Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 720x400 [SAR 1:1 DAR 9:5], q=-1--1, 24k tbn, 23.98 tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, mono, s16, 128 kb/s
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 -> libx264)
Stream #0:1 -> #0:1 (ac3 -> libfaac)
Press [q] to stop, [?] for help
[mpeg4 @ 0x4172f60] Invalid and inefficient vfw-avi packed B frames detected
frame=151513 fps=117 q=-1.0 Lsize= 478027kB time=01:45:19.29 bitrate= 619.7kbits/s dup=1 drop=0
video:410377kB audio:63412kB subtitle:0 global headers:0kB muxing overhead 0.894491%
[libx264 @ 0x2e881e0] frame I:1066 Avg QP:19.59 size: 22319
[libx264 @ 0x2e881e0] frame P:56378 Avg QP:22.73 size: 5184
[libx264 @ 0x2e881e0] frame B:94069 Avg QP:25.25 size: 1108
[libx264 @ 0x2e881e0] consecutive B-frames: 5.3% 29.6% 18.7% 46.5%
[libx264 @ 0x2e881e0] mb I I16..4: 5.6% 86.5% 7.9%
[libx264 @ 0x2e881e0] mb P I16..4: 1.3% 5.8% 0.5% P16..4: 52.0% 14.1% 8.5% 0.0% 0.0% skip:17.9%
[libx264 @ 0x2e881e0] mb B I16..4: 0.0% 0.2% 0.0% B16..8: 41.8% 2.1% 0.3% direct: 0.7% skip:54.8% L0:39.6% L1:55.8% BI: 4.6%
[libx264 @ 0x2e881e0] 8x8 transform intra:78.3% inter:85.2%
[libx264 @ 0x2e881e0] coded y,uvDC,uvAC intra: 65.8% 58.1% 10.7% inter: 14.7% 17.6% 0.1%
[libx264 @ 0x2e881e0] i16 v,h,dc,p: 37% 23% 12% 27%
[libx264 @ 0x2e881e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 18% 32% 4% 4% 5% 4% 5% 5%
[libx264 @ 0x2e881e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 19% 14% 5% 8% 8% 7% 6% 4%
[libx264 @ 0x2e881e0] i8c dc,h,v,p: 53% 19% 24% 4%
[libx264 @ 0x2e881e0] Weighted P-Frames: Y:3.5% UV:0.9%
[libx264 @ 0x2e881e0] ref P L0: 61.3% 16.2% 16.3% 6.0% 0.2%
[libx264 @ 0x2e881e0] ref B L0: 88.6% 9.9% 1.5%
[libx264 @ 0x2e881e0] ref B L1: 95.5% 4.5%
[libx264 @ 0x2e881e0] kb/s:531.99
TaiChiRabbit
 
Posts: 2
Joined: Fri Apr 26, 2013 4:42 pm

Re: adding subtitles to mp4 file

Postby burek » Sat Apr 27, 2013 11:36 am

Hi,

One correction, so that other people can properly understand you, when you say "to convert an MKV file to MP4 file", you actually wanted to say "to remux an MKV file format/container to MP4 file format/container", because you just changed the container that holds your audio/video streams (you didn't change/re-encode your audio/video streams, just copy/pasted them).

From your post, I concluded that you take the file "file.mkv" and you want to remove your subtitle stream from it and burn that sub onto your video stream, keeping your audio stream intact, right? If that's correct, you could do something like this:
Code: Select all
ffmpeg -i file.mkv -vf subtitle=file.mkv -map 0:v -map 0:a -c:a copy -c:v libx264 -crf 23 newfilewithsubs.mp4


However, newfilewithsubs.mp4 is considerably smaller (400MB vs 1.4GB for newfile.mp4 and file.mkv) and it looks like the video and audio codecs have been converted (mpeg4 -> libx264,ac3 -> libfaac). Should I be bothered about this and is the conversion merely making the file more efficient and no loss of quality? I've googled around the forums but cannot see what I'm doing wrong (it's probably too simple!) so can anyone help?

Hm, it appears you are missing something here. When you burn your subtitles in the video, obviously, you need to re-encode your video (audio can stay the same). Also, since you didn't specify any encoding parameters, ffmpeg used some defaults and that's what you are experiencing.

When you write something like this:
Code: Select all
ffmpeg -i newfile.mp4 -vf subtitles=newfile.srt newfilewithsubs.mp4

ffmpeg will interpret that as this (because you didn't specify any encoding options and you are specifically asking for the re-encoding, using subtitles filter):
Code: Select all
ffmpeg -i newfile.mp4 -vf subtitles=newfile.srt -vcodec libx264 -crf 23 -acodec libfaac newfilewithsubs.mp4


So, if you want to do it properly, you could try like this:
Code: Select all
ffmpeg -i newfile.mp4 -vf subtitles=newfile.srt -vcodec libx264 -crf 23 -acodec copy newfilewithsubs.mp4


The option "-crf" is used to control the quality of the video encoding. The higher the number, the worse the quality ("-crf 50" = the worst quality, "-crf 0" = lossless).
burek
 
Posts: 868
Joined: Mon May 09, 2011 10:16 pm
Location: Serbia

Re: adding subtitles to mp4 file

Postby TaiChiRabbit » Sat Apr 27, 2013 3:19 pm

Thanks for taking the time to give a very clear explanation. All makes sense now......
TaiChiRabbit
 
Posts: 2
Joined: Fri Apr 26, 2013 4:42 pm

Re: adding subtitles to mp4 file

Postby Makaveli84 » Wed Sep 11, 2013 6:06 pm

I'm not sure what embedded subtitles PS3 supports, but in general, you can do the whole thing losslessly and in 1 pass like this:

Code: Select all
ffmpeg -i in.mkv -c:v copy -c:a copy -c:s mov_text -map 0 out.mp4


This will work assuming the video and audio codecs are supported within the mp4 container, and that the subtitles in the original mkv can be decoded and converted to timed text format (mov_text) by ffmpeg. This is usually the case for subrip (srt) subtitles.
Makaveli84
 
Posts: 1
Joined: Wed Sep 11, 2013 6:01 pm


Return to ffmpeg

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests