"... The problem is that I cannot edit another MP4 file to add this atom without damaging the file.
Check out this small_VC1edit.mp4 on WhatsApp. If he does what you need, then read ...
Make playable MP4 :
Using the original small.mp4 as an example of editing (download the file and open it with a hex editor).
1) In an empty byte array, add the first 72 bytes of the specified WhatsApp style MP4 header.
00 00 00 1C 66 74 79 70 6D 70 34 32 00 00 00 01 6D 70 34 31 6D 70 34 32 69 73 6F 6D 00 00 00 18 62 65 61 6D 01 00 00 00 01 00 00 00 00 00 00 00 05 00 00 00 00 00 00 0C 6C 6F 6F 70 00 00 00 00 00 00 00 08 77 69 64 65
You showed 80 bytes, but the last 8 bytes are not needed yet (also four of these eight byte values โโshould be different for your output file).
2) Calculate the delta.
So just use this logic ( a or b ):
a) If the WhatsApp header is larger than the MP4 input:
delta = ( WhatsApp_header - input_MP4_header)
b) If the input header of MP4 is larger than WhatsApp:
delta = ( input_MP4_header - WhatsApp_header )
So, to enter small.mp4, which has 160 header bytes (then 4 bytes of moov SIZE follow (like 00 00 0D 83 ), and then 4 more bytes from moov NAME (like 6D 6F 6F 76 or utf-8 text " moov ").
You can say: 160 MP4 bytes - 72 WhatsApp bytes = Delta of 88 .
If you delete these original 160 bytes and replace them with WhatsApp 72 short bytes, they will be 88 fewer bytes, which should now be counted in another moov data moov . This section is an STCO atom.
3) Update the STCO atom STCO new offsets:
In small.mp4, the STCO atom begins with an offset of 1579 (as 73 74 63 6F ). The previous 4 bytes (offsets: from 1575 to 1578) are STCO SIZE bytes (as 00 00 00 B8 ), which is the decimal value of 184 . This total byte length size includes accounting for these 4 SIZE bytes.
Skip 12 bytes from the start byte 73 of STCO NAME bytes 73 74 63... , so skip the following:
73 74 63 6F 00 00 00 00 00 00 00 2A
Now you reach the point for sequentially updating each 32-bit integer (4 bytes) offsets with a new delta value. But how many corrections to update?
atomEditTotal = ( (stco_SIZE - 16) / 4); //gives 42 //PS: Minus by 16 is to trim off non-offset bytes.
So, editing requires 42 entries. Our delta like 88 , so for each integer we read the value, minus it at 88, then write back the new value in the same place, repeat 41 more times (using the While loop with if condition for break; loop).
For testing, given the damaged file, if you are editing the first record, this should be enough to show frame 1 of the video (if not an audio file).
PS: After editing the STCO offsets small.mp4 just delete its initial 160 bytes and connect / connect the remaining MP4 bytes to the end of the WhatsApp header by 72 bytes. Save the array as a new file and test.