WhatsApp video as sharing Gif on Android programmatically

How can I convert a mp4 video file to a WhatsApp gif file (it just displays as a gif inside the application's user interface, but internally is a specific mp4 format) that should be used in the intention of share android, being recognized as this type of media by whatsapp chat application ???

I searched a lot, but I canโ€™t find any information from WhatsApp documents (they donโ€™t have this type of document at all) or any developer with the same problem as me.

WHAT I HAVE:

I found that at the beginning of whatsapp "gif" mp4 files have a loop value, if you read them in a hex editor, all files have this. Remove this value so that whatsapp gets as a regular video (without sharing as a gif).

How can I add this value using ffmpeg encoding? (manually editing mp4 files with this value will damage the files, maybe I need to fix some mp4 header index, which I don't know yet ...)

enter image description here

FIRST 80 BYTES in hexadecimal order (from start to start of the "moov" atom from the mp4 structure):

00 00 00 1C 66 74 79 70 6D 70 34 32 00 00 00 6 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 0C 6C 6F 6F 70 00 00 00 00 00 00 00 08 77 69 64 65 00 00 04 9F 6D 6F 6F 76

The short mp4 file created by WhatsApp, which inside (in the application) was shown as Gif (with a different user interface):

https://www.dropbox.com/s/kpynmx1bg3z76lz/VID-20171024-WA0009.mp4?dl=0

+8
android ffmpeg video whatsapp mp4
source share
1 answer

"... 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.

  • Note that the WhatsApp header is 72 bytes (before moov atom).

  • Please note: (original) Small.mp4 has 160 bytes of header (before moov atom).

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.

+1
source share

All Articles