Media Upload

Twitter allows developers to upload files on their servers so that they can be used in tweets. Twitter has a limited file format that they support:

  • Images : PNG, JPEG, WEBP - max size = 5MB

  • GIFs - max size = 15MB

  • Videos : MP4 - max size = 15MB

If you want to learn more about the supported formats and video recommendations visit the Twitter upload documentation. Restrictions to the media also apply, check this documentation for more information.

Images

var tweetinviLogoBinary = File.ReadAllBytes("./tweetinvi-logo-purple.png");

// for tweets
var uploadedImage = await userClient.Upload.UploadTweetImageAsync(tweetinviLogoBinary);

// for messages
var uploadedImage = await userClient.Upload.UploadMessageImageAsync(tweetinviLogoBinary);

Video

var videoBinary = File.ReadAllBytes("./video.mp4");

// for tweets
var uploadedVideo = await userClient.Upload.UploadTweetVideoAsync(videoBinary);

// for messages
var uploadedVideo = await userClient.Upload.UploadMessageVideoAsync(videoBinary);
Videos are different from images as they need to be processed by Twitter before becoming usable...

Video Processing Status

You can request the status of the video processing and do this until you receive Succeeded or Failed.

var status = await userClient.Upload.GetVideoProcessingStatusAsync(uploadedVideo);
// processing state can be : Pending, InProgress, Succeeded, Failed
var processingCompleted = status.ProcessingInfo.ProcessingState == ProcessingState.Succeeded;

Automatic wait

If you only care about knowing when the video processing has completed you use WaitForMediaProcessingToGetAllMetadata.

await userClient.Upload.WaitForMediaProcessingToGetAllMetadataAsync(uploadedVideo);

Parameters

Tweetinvi includes various parameters to customize the experience and behaviour of upload.

var uploadedVideo = await userClient.Upload.UploadBinaryAsync(new UploadTweetVideoParameters(binary)
{
    // Defines a timeout after which the operation gets cancelled
    Timeout = TimeSpan.FromMinutes(1),

    // Size of request sent to Twitter. Restrictions applies from twitter.
    MaxChunkSize = 500,

    // Event that informed the state of the upload
    UploadStateChanged = stateChange =>
    {
        _logger.WriteLine("Upload progress changed to " + stateChange.Percentage + "%");
    }
});

Add metadata

Twitter let you add metadata to media after they have been uploaded.

var media = await userClient.Upload.UploadTweetImageAsync(binary);
await userClient.Upload.AddMediaMetadataAsync(new MediaMetadata(media)
{
    AltText = "Hello",
});