我正在尝试使用tweetinvi文件上传.File上传图片工作,但相同的代码不适用于视频(大型视频超过20 MB)
我在这里问过,但现在是answear
TweetInvi Large Video Upload Failing With Null Reference
所以我寻找另一个解决方案。有tweetinvi chunked上传 我对此进行了编码,但它不起作用,它不会出错,但它不起作用
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);
}
答案 0 :(得分: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计划进一步改进上传。
祝您有个美好的一天,并感谢您报告此问题,
干杯 Linvi