0

我正在尝试使用 tweetinvi 进行文件上传。文件上传适用于图像,但相同的代码不适用于视频(超过 20 mb 的大视频)

我在这里问过,但现在回答

TweetInvi 大型视频上传失败,引用为空

所以我寻找另一种解决方案。有 tweetinvi 分块上传我对此进行了编码但它不起作用,它没有给出错误但它不起作用

  if (file.ContentType.Contains("video"))//video
                        {

                            var video1 = System.IO.File.ReadAllBytes(path);
                            var chunk = Upload.CreateChunkedUploader(); //Create an instance of the ChunkedUploader class (I believe this is the only way to get this object)

                            using (FileStream fs = System.IO.File.OpenRead(path))
                            {
                                chunk.Init("video/mp4", (int)fs.Length); //Important! When initialized correctly, your "chunk" object will now have a type long "MediaId"
                                byte[] buffer = new byte[video1.Length]; //Your chunk MUST be 5MB or less or else the Append function will fail silently.
                                int bytesRead = 0;

                                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    byte[] copy = new byte[bytesRead];
                                    Buffer.BlockCopy(buffer, 0, copy, 0, bytesRead);
                                    TimeSpan s = new TimeSpan();

                                    chunk.Append(copy, chunk.NextSegmentIndex.ToString()); //The library says the NextSegment Parameter is optional, however I wasn't able to get it to work if I left it out. 
                                }
                            }

                            var video = chunk.Complete(); //This tells the API that we are done uploading.
                            listMedia.Add(video); 


                        }
4

1 回答 1

0

我想说的是,在解决了您的错误之后,我能够确定您遇到的问题。

问题是您没有指定media_category上传的内容。

除此之外,您还需要等待 Twitter 处理媒体。

在 Tweetinvi 2.1 中,该过程应该变得更容易。请使用以下代码:

var binary = File.ReadAllBytes(@"video_path");
var media = Upload.UploadVideo(binary);

// The upload is completed but it does not mean it succeeded!
if (!media.HasBeenUploaded)
{
    // Something went wrong during the upload. 
    // Please retry or check the video type/settings.
    return;
}

// Just wait for Twitter to have processed the upload (RECOMMENDED)
Upload.WaitForMediaProcessingToGetAllMetadata(media);

// Now the media is ready to be used in a Tweet
var tweet = Tweet.PublishTweet("hello", new PublishTweetOptionalParameters
{
    Medias = { media }
});

您可以在更新的文档中阅读有关上传的更多信息:https ://github.com/linvi/tweetinvi/wiki/Upload#upload-status-video

最后请注意,我们计划在 2.2 和 2.3 版本中进一步改进上传。

祝您有美好的一天,感谢您报告此问题,

干杯林维

于 2017-10-10T19:48:30.760 回答