在Android上以编程方式将WhatsApp视频作为Gif共享 [英] WhatsApp video as Gif sharing on Android programatically

查看:211
本文介绍了在Android上以编程方式将WhatsApp视频作为Gif共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将mp4视频文件转换为WhatsApp gif文件(在应用程序UI中简单显示为gif,但内部是特定的mp4格式)以用于Android共享意图,被whatsapp识别为这种媒体类型聊天应用程序?

How can i convert a mp4 video file to a WhatsApp gif file (it's simple showed as gif inside app UI but internally is a specific mp4 format) to be use in android share intent, being recognized as this type of media by whatsapp chat app???

我进行了很多搜索,但是我无法从WhatsApp文档中找到任何信息(无论如何他们都没有这种文档)或与i有相同问题的任何开发人员.

I search a lot but i can't find any information from WhatsApp docs (they don't have this kind of doc anyway) or any dev with the same problem as i.

我所拥有的:

我发现,如果您在十六进制编辑器中读取它们,则whatsapp"gif" mp4文件的开头会出现一个 loop 值.删除此值可使whatsapp接收为常规视频(不共享为gif).

I have discovered that at beginning of whatsapp "gif" mp4 files is present a loop value if you read they on hex editor, all files have this. Remove this value make whatsapp receive as regular video (not sharing as gif).

如何使用ffmpeg编码添加此值? (使用此值手动编辑mp4文件会损坏文件,也许我必须修复一些我不知道的mp4标头索引...)

How can i add this value using ffmpeg encoding? (editing my mp4 files manually with this value corrupt the files, maybe i have to fix some mp4 header index that i don't know yet...)


以十六进制表示的前80个字节(从mp4结构的"moov"原子的开头到开头):

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

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 0 00 6C 6F 6F 70 00 00 00 00 00 00 00 08 77 69 64 65 00 00 04 9F 6D 6F 6F 76

WhatsApp在内部(在应用程序中)生成的简短mp4文件显示为Gif(具有不同的UI):

A short mp4 file generated by WhatsApp that internally (at app) was showed as a Gif (with different UI):

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

推荐答案

"...问题是我无法编辑另一个MP4文件来添加此原子 不会损坏文件.

"...The problem is that I can't edit another MP4 file to add this atom without corrupt the file.

测试此 small_VC1edit.mp4 .如果它满足您的要求,请继续阅读...

Test this small_VC1edit.mp4 in WhatsApp. If it does what you want then read on...

制作可播放的MP4 :

使用原始的 small.mp4 作为一个编辑示例(下载文件并使用十六进制编辑器打开).

Using original small.mp4 as an editing example (download file and open with a hex editor).

1)在空白字节数组中,添加所示的WhatsApp风格的MP4标头的前72个字节.

1) In a blank byte array, add the first 72 bytes of the shown 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

您已经显示了80个字节,但是最后8个字节是不需要的(这8个字节的值中的4个对于您的输出文件也必须是不同的).

You've shown 80 bytes but the last 8 bytes are not needed yet (also four of those eight byte's values must be different for your output file).

2)计算增量.

  • 请注意(新的) WhatsApp 标头为72个字节(在moov原子之前).

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

请注意,(原始) Small.mp4 具有160个字节的标头(在moov原子之前).

Note the (original) Small.mp4 has 160 bytes of header (before moov atom).

因此只需使用以下逻辑( a b ):

So just use this logic (a or b):

  • a)如果WhatsApp标头大于输入MP4:
    delta = ( WhatsApp_header - input_MP4_header)

  • a) If WhatsApp header is bigger than input MP4 :
    delta = ( WhatsApp_header - input_MP4_header)

b)如果输入的MP4标头大于WhatsApp:
delta = ( input_MP4_header - WhatsApp_header )

b) If input MP4 header is bigger than WhatsApp :
delta = ( input_MP4_header - WhatsApp_header )

因此对于输入small.mp4,它具有160个标头字节(后跟4个字节的moov的SIZE(如00 00 0D 83),然后又跟着4个字节的moov的名称(如6D 6F 6F 76或utf-8)文本"moov").

So for the input small.mp4 which has 160 header bytes (is followed next by 4 bytes of moov's SIZE (as 00 00 0D 83) and then follows another 4 bytes now of moov's NAME (as 6D 6F 6F 76 or utf-8 text of "moov").

我们可以说:160 MP4 bytes - 72 WhatsApp bytes = Delta of 88.

如果删除这些原始的160个字节,并用较短的72个WhatsApp字节替换它们,则它们将减少88个字节,现在必须在MOOV数据的另一部分中说明.该部分是STCO原子.

If you delete these original 160 bytes and replace them with the shorter 72 WhatsApp bytes, they'll be 88 less bytes which must now be accounted for in the another section of MOOV data. That section is the STCO atom.

3)用新的偏移量更新STCO原子:

3) Update the STCO atom with new offsets:

在small.mp4中,STCO原子始于偏移量1579(与73 74 63 6F相同).前4个字节(偏移量:1575至1578)是stco的SIZE字节(如00 00 00 B8),其十进制值为 184 .字节长度的总SIZE也包括这4个SIZE字节.

In small.mp4 the STCO atom begins at offset 1579 (as 73 74 63 6F). The previous 4 bytes (offsets: 1575 to 1578) are stco's SIZE bytes (as 00 00 00 B8) which is decimal value 184. This total SIZE of bytes length includes accounting for those 4 SIZE bytes too.

stco的NAME字节73 74 63...的起始字节73跳过12个字节,所以跳过这些:

Skip 12 bytes from the starting byte 73 of the stco's NAME bytes 73 74 63... so skip these:

73 74 63 6F 00 00 00 00 00 00 00 2A

现在,您可以使用新的delta值依次更新每个32位整数(4个字节)的偏移量.但是要更新多少偏移量?

Now you reach point to sequentially update every 32-bit integer (4 bytes) of offsets with the new delta value. But how many offsets to update?

atomEditTotal = ( (stco_SIZE - 16) / 4); //gives 42 //PS: Minus by 16 is to trim off non-offset bytes.

因此,有42个条目需要编辑.我们的Delta 88 ,因此对于每个整数,我们读取值,将其减去88,然后在同一位置将其写回新值,再重复41次(使用While循环和条件以break;循环).

So there are 42 entries to edit. Our Delta as 88 so for each integer we read value, minus it by 88, then write it back new value at same place, repeat another 41 times (using While loop with an if condition to break; the loop).

对于测试,给定一个损坏的文件,如果您编辑第一个条目,它应该足以显示视频的第一帧(如果是非音频文件).

For testing, given a corrupt file, if you edit the first entry it should be enough to show frame 1 of video (if non-audio file).

PS::编辑small.mp4的STCO偏移后,只需删除其开始的160个字节,然后将剩余的MP4字节连接/合并到72字节的WhatsApp标头的末尾.将数组另存为新文件并进行测试.

PS: After editing the STCO offsets of small.mp4, just delete its starting 160 bytes and join/concat those remaining MP4 bytes to the back/end of the 72 byte WhatsApp header. Save array as new file and test.

这篇关于在Android上以编程方式将WhatsApp视频作为Gif共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆