Log in

View Full Version : How to add own, subtitles (resp. pure text) into existing MP4 video?


mike23
9th December 2018, 13:47
Assume I have an existing well-encoded MP4 video WITHOUT subtitles.

Now I want to add the text "foobar testtext" with fontsize=18 and fontfamily Verdana into video beginning at 1min23sec and ending at 3min44sec centralized 30 pixels above lower border.

How can I most easily achieve this?

I possible/necessary the text could be put fix encoded into video and not only as traditional subtitle.

Yanak
17th December 2018, 10:55
Hello,

there is a few problems when it comes to your specific needs: Most players will ignore the fonts and size defined and will use the ones set in the players subtitles options.

MP4 container using a tool like MP4Box to remux is theoretically able to achieve what you need using the subtitles .ttxt format, but i don't have any player able to respect the .ttxt formats so i don't know if it will even work correctly...
anyways : https://gpac.wp.imt.fr/mp4box/ttxt-format-documentation/

The .ttxt file content will look something like this for a video of 1920x1080px resolution:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- GPAC 3GPP Text Stream -->
<TextStream version="1.1">
<TextStreamHeader width="1920" height="1080" layer="0" translation_x="0" translation_y="0">
<TextSampleDescription horizontalJustification="center" verticalJustification="bottom" backColor="0 0 0 0" verticalText="no" fillTextRegion="no" continuousKaraoke="no" scroll="None">
<FontTable>
<FontTableEntry fontName="verdana" fontID="1"/>
</FontTable>
<TextBox top="0" left="0" bottom="1050" right="1920"/>
<Style styles="Normal" fontID="1" fontSize="18" color="ff ff ff ff"/>
</TextSampleDescription>
</TextStreamHeader>
<TextSample sampleTime="00:00:00.000" sampleDescriptionIndex="1" xml:space="preserve"></TextSample>
<TextSample sampleTime="00:01:23.000" sampleDescriptionIndex="1" xml:space="preserve">foobar testtext</TextSample>
<TextSample sampleTime="00:03:44.000" sampleDescriptionIndex="1" xml:space="preserve"></TextSample>
</TextStream>
I think it's the TextBox parameters that will need to be adjusted to place the subtitle 30px above the bottom, here i set it to 1050, like i said i don't have a player able to respect this ( i think it needs something like quicktime player or something like that ) so i can't really test more than this for the .ttxt.

The MP4Box command will be :
MP4Box.exe -add "H:\inputFile.mp4#video" -add "H:\inputFile.mp4#audio:lang=eng:name=" -add "H:\Subtitles.ttxt:txtflags=0xC0000000:lang=eng:name=" -new "H:\Output.mp4"

It will pick video + audio track from your existing mp4 file + add the .ttxt subtitle and create the new output.mp4 video ( the subtitles have the ttxflag to set subs as forced subs )

I tried to add those subs in one video, the ttxt subs work somehow... but the subs settings are nor respected in MPC-HC, MPC-BE or VLC, they are rendered but with the players settings for font and position... Might work with quicktime player or something else idk.


- The easiest solution i think is to still use MP4box to remux your mp4 and add a simple .srt subtitle with this inside it :
1
00:01:23,000 --> 00:03:44,000
foobar testtext

Using the following command ( need to modify files paths of course ) :
MP4Box.exe -add "H:\inputFile.mp4#video" -add "H:\inputFile.mp4#audio:lang=eng:name=" -add "H:\Subtitles.srt:txtflags=0xC0000000:lang=eng:name=" -new "H:\Output.mp4"

Same as earlier, it picks the video + audio from your MP4, add the srt sub and flag it as forced sub and output a Output.mp4 file, no re-coding done, only remuxed.
The font will be default one set in the player, same for position but most of them use something like Arial and bottom of video for the font and position.


Other solution might be to remux the mp4 into a MKV using .ass subtitles formated exactly like you want, I am not much familiar with ass subtitles but i guess they offer more liberties for those specific needs, but i'm not 100% sure if all players will respect the font settings tho...
Sadly MP4 container does not support those .ass if i make no error, at least mp4box does not.

Well... it depends of your needs and how you want to distribute the video since most subtitles parameters will probably be overwritten by the players settings used for the subs...

Very last solution is to hardcode the text but it's really not worth it imo, a simple .srt subtitle ( or the .ttxt subtitle in example ) and file remuxed with MP4Box will be the most efficient and quick thing to do i think.