Joining MP4 files with mp4box

Recently I need to join some mp4 files (parts of a movie) back into one single file.  The best result that I got was from a program called MP4Box.  Because what I need is just to join these files together into a single file without changing format, MP4Box did a very good job.  The voice and the picture are perfectly in sync in the final result.  The command I used is:

mp4box -cat 0.mp4 -cat 1.mp4 -cat 2.mp4 ... -new result.mp4

However, there is one small problem.  The duration information for the final result is not correct.  It is much longer than the actual duration of the movie.  This may not be a problem if you are going to watch the movie from begin to end.  It does become a problem when you want to seek to the middle of the movie. I tried many different options in mp4box without any luck. I finally decided to look into the raw data of the result mp4 file. After a few trial and error, I finally found out where the duration information is stored. It appeared in two places (at file offset 0x34 and 0xC1) in the header of the mp4 file. The number was stored as a 4-byte integer with the higher significant digit stored in lower offset. I then used a Hex editor to modify the number to correct duration. When the result file was opened in a player, the duration was correct and the problem was fixed.

For example, let’s say that the correct duration is 1:24:35.013 (scale is 1000), then the number I need to put in 0x34 and 0xC1 is

= 5075013 = 0x004D7045

Thus, the number 0x004D7045 will be placed into 0x34 and 0xC1 with higher significant digit first as follows:

Address: 34 35 36 37
Content: 00 4D 70 45

I am thinking of writing a simple program to automate the process whether than edit the file by hand.