Adding subtitle from command line MP4Box

9.1k views Asked by At

I am adding a subtitle file to a video using MP4Box. The following command works perfectly from the command line:

c:/GPAC/MP4Box -add c:/test.m4v#audio -add c:/test.m4v#video -add c:/test_en.srt:hdlr=sbtl:lang=en:group=2:layer=-1 -new c:/test2.m4v

However, what I really want to do is to put the command into a .bat file. The following is my command in the batch file:

%1/GPAC/MP4Box -add %2/%3#audio -add %2/%3#video %4 -new %2/%3

As you can see I am trying to pass in "-add c:/test.m4v#video -add c:/test_en.srt:hdlr=sbtl:lang=en:group=2:layer=-1" as the fourth parameter. The reason I want to do this is there may be many subtitles files being added:

"-add c:/test.m4v#video -add c:/test_en.srt:hdlr=sbtl:lang=en:group=2:layer=-1 -add c:/test.m4v#video -add c:/test_ja.srt:hdlr=sbtl:lang=ja:group=2:layer=-1:disabled"

so I don't know ahead of time how many -add commands there need to be so I want to just pass them all in as one parameter. But, mp4box doesn't like this.

I'm not sure if this is a limitation with mp4box or with batch file parameters in general.

4

There are 4 answers

0
Chris On

I know this is an old thread, but for anyone searching in future.

I used the following approach in a batch file, combined with filemenu tools to allow for a simple right-click menu function to initiate the batch process:

for %%a in (*.m4v) do mp4box -add "%%~Na.eng.srt":lang=eng:layout=0x60x0x-1:group=2:hdlr="sbtl:tx3g" "%%a"
0
irrational On

I ended up solving this by writing/rewriting the batch file from code every time I needed to run it. So I would create the batch file with all my arguments. Run it. Then delete the file. This worked great.

0
FeRD On

A note regarding MP4Box syntax,since both the question and the answers here have it... "suboptimally", let's just say.

If you want MP4Box to rewrite an input file — which is its default mode, unless told otherwise — while adding subtitles (or any other tracks/metadata), you should specify the file to be modified first on the command line, and without using -add. So, to rewrite a video file my_video.mp4 with subtitled added from my_video_English.srt and my_video_German.srt, you'd use:

MP4Box my_video.mp4 \
  -add my_video_English.srt \
  -add my_video_German.srt

No need to specify tracks, since you want to use all available tracks from all input files. (Here also, that's the default disposition for track data.)

MP4Box will create a new temporary output file, copy the contents of my_video.mp4 into it, remuxing the original tracks with new tracks created for each subtitle file, and then rename the temporary file back to my_video.mp4, overwriting the original. (If you don't want to overwrite, add -out newname.mp4 to the end of the command line.)

The program help (specifically, MP4Box -h import) especially warns against adding subtitle tracks to the output stream before any video tracks are created, so -add foo.srt should never be before the a/v input file on the command line:

$ MP4Box -h import
[...]
Note: When importing SRT or SUB files, MP4Box will choose 
default layout options to make the subtitle appear at the 
bottom of the video. You SHOULD NOT import such files before 
any video track is added to the destination file, otherwise 
the results will likely not be useful (default SRT/SUB 
importing uses default serif font, fontSize 18 and display 
size 400x60). For more details, check TTXT doc.

It's possible this wouldn't affect file rewriting (because, the video track is already in the output stream before it even begins), but it definirely would affect use of -out. In general, placing the input a/v content first in the arguments is just a good habit to get into when using MP4Box.

0
Teraokay On

Batch files on Windows are quirky and have limited functionality. What you could do is use Cygwin, which allows you to use a real shell (like Bash for example) on Windows.