I have a bunch of JPEG frames that I use ImageMagick to stitch together into a GIF. The final product has the following type:
GIF image data, version 89a, 1280 x 720
This gives the effect of an animated GIF. My file sizes range anywhere between 6-8MB.
If I upload this media manually to Twitter, it works great. Using TwitterOAuth
PHP library found here when I attempt the following:
$media = $twitterConnection->upload('media/upload', ['media' => 'my_file.gif');
I get the following error:
Image file size must be <= 5242880 bytes
I have also tried uploading the file using Twitter’s recommended Python large video library and that failed with the following:
INIT
Media ID: 1362940800456351744
APPEND
4194304 of 7685061 bytes uploaded
APPEND
7685061 of 7685061 bytes uploaded
Upload chunks complete.
FINALIZE
{'media_id': 1362940800456351744, 'media_id_string': '1362940800456351744', 'media_key': '7_1362940800456351744', 'size': 7685061, 'expires_after_secs': 86400, 'processing_info': {'state': 'pending', 'check_after_secs': 1}}
Media processing status is pending
Checking after 1 seconds
STATUS
Media processing status is failed
I am willing to use any platform/utility to get my video files uploaded to Twitter. What is my best bet to either:
1: Fix my file type to adhere to Twitter’s requirements. Should it not be a GIF? Should I be converting my still shot JPEG files to another format?
2: Is there an API that Twitter has available that will allow the GIFs of these sizes to be uploaded? Again, I can upload these files via the regular Twitter web UI client, but I cannot automate it via their API.
How do I upload my GIF to Twitter using their API?
3
Answers
You can’t there is a limit of 5MB for gif files. There is your issue the docs are quoted below.
Supported image media types: JPG, PNG, GIF, WEBP
Image size <= 5 MB, animated GIF size <= 15 MB
The file size limit above is enforced by the media upload endpoint. In addition, there is a separate product entity specific file size limit which is applied when calling the Tweet creation (or similar) endpoints with media_id. The file size limit and other constraints may vary depending on the media_category parameter.
https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/uploading-media/media-best-practices#:~:text=Dimensions%20must%20be%20between%2032×32,must%20not%20exceed%20512%20mb
In answer to your two other questions.
Yes try compression on the files.
No API script will allow it. I’m not sure if twitter still renders files from image hosting like twitpic used to you could try that.
try reducing the number of colors with a palette
ffmpeg -i yourGif.gif -lavfi "palettegen=max_colors=255:stats_mode=diff" palette.png
ffmpeg -i yourgif.gif -i palette.png -lavfi "paletteuse=dither" -c:v gif -pix_fmt yuv420p paletteGif.gif
you can reduce the max_colors option for less space but lower quality
change the dither value to see which result is better
otherwise the only solution is to downscale your gif
try to encode the gif again,with ffmpeg use this guide to encode a serie of images with the -f gif
https://en.m.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence
use a palette because GIFs have 256 colors as jpegs and use the -pixel_format yuv420p or some users will have problems, if the jpegs are the same keep the scaling for last you may save some space.