Search code examples
twittertweetinvi

Getting error when uploading video using Tweetinvi


I am trying to upload files larger than 5MB but less than 15MB. In this case its 10MB... sample video from sample-videos.com

I am using Tweetinvi, and it works great with files less than 5MB, but fails on chunked uploads. I've tried the easy and the hard way.

Easy way:

        var video = File.ReadAllBytes(@"D:\Projects\SampleVideo_1280x720_10mb.mp4");
        var media = Upload.UploadVideo(video); // Error here... Invalid Content
        var tweet = user.PublishTweet(message, new PublishTweetOptionalParameters
        {
            Medias = { media }
        });

I've pulled in the Tweetinvi solution from Git (currently 0.9.13.0 repository here) and see that the above is getting an error "Invalid Content" when calling Upload.UploadVideo(...). It appears to fail on the command FINALIZE.

Tried the hard way:

        using (var fileStream = File.OpenRead(@"D:\Projects\SampleVideo_1280x720_10mb.mp4"))
        {
            var initSucceeded = uploader.Init("video/mp4", (int)fileStream.Length);

            byte[] buffer = new byte[4900000]; //Your chunk MUST be 5MB or less or else the Append function will fail silently.
            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                byte[] copy = new byte[bytesRead];
                Buffer.BlockCopy(buffer, 0, copy, 0, bytesRead);
                var appendResult = uploader.Append(new ChunkUploadAppendParameters(copy, "video/mp4", null) { SegmentIndex = uploader.NextSegmentIndex });
            }
            var video = uploader.Complete(); // Fails here... Returned error: Segments do not add up to provided total file size
            var tweet = user.PublishTweet(message, new PublishTweetOptionalParameters()
            {
                //Medias = { video }
                MediaIds = { video.MediaId.Value }
            });
        }

The above fails on upload.Complete() with the Twitter API returning "Segments do not add up to provided total file size"

What am I missing?

TIA


Solution

  • I think the problem you have is with the video file. The video seems to be using a 6 channels audio and Twitter Public Upload API only allow developers to upload video with mono or stereo audio.

    enter image description here

    Source : https://dev.twitter.com/rest/media/uploading-media

    I am no expert in video properties so feel free to prove me wrong if I am.

    The above fails on upload.Complete() with the Twitter API returning "Segments do not add up to provided total file size"

    This error means that you are not actually sending all the byte that you promised Twitter. During the INIT, you tell Twitter the size of your media, if what it receives in the combined APPEND is not equals to the value you specified in the INIT, the error you described is thrown.

    PS : I have tried using a 2 channels 14.8 MB mp4 and it works properly. var media = Upload.UploadVideo(binary);