我正在尝试使用tweetinvi.file上传图像上传的上传,但相同的代码对视频不起作用(大型视频超过20 MB(
我在这里问,但是现在答案
tweetinvi大型视频上传失败,null参考
所以我寻找另一个解决方案。我对此进行了编码,但是它不起作用,它没有错误,但它不起作用
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);
}
我想说,在处理您的错误后,我能够确定您遇到的问题。
问题是您没有指定上传的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/wiki/upload#upload-status-video
最后,请注意,计划2.2和2.3的上传进一步改进。
祝您有美好的一天,感谢您报告此问题,
欢呼linvi