6

YouTube API を使用してダイレクト アップロード経由でサイズの大きな動画をアップロードしようとすると、いつでも発生します。OutOfMemory 例外が発生します。これを取り除くために私にできることはありますか?YouTube API は、直接アップロードを使用したビデオ サイズの制限について何も述べていません。

ダイレクトアップロードは諦めました。今、再開可能なアップロード方法を試しています。私のコードは以下です。

YouTubeRequest request;
YouTubeRequestSettings settings = new YouTubeRequestSettings("YouTube Upload", Client Key, "Username", "Password");
request = new YouTubeRequest(settings);
Video newVideo = new Video();

ResumableUploader m_ResumableUploader = null;
Authenticator YouTubeAuthenticator;

m_ResumableUploader = new ResumableUploader(256); //chunksize 256 kilobyte
m_ResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(m_ResumableUploader_AsyncOperationCompleted);
m_ResumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(m_ResumableUploader_AsyncOperationProgress);

YouTubeAuthenticator = new ClientLoginAuthenticator("YouTubeUploader", ServiceNames.YouTube, "kjohnson@resoluteinnovations.com", "password");

//AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/uploads");
//link.Rel = ResumableUploader.CreateMediaRelation;
//newVideo.YouTubeEntry.Links.Add(link);

System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] chunk = new byte[256000];int count = 1;
while (true) {
    int index = 0;
    while (index < chunk.Length) {
        int bytesRead = stream.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0) {
            break;
        }
        index += bytesRead;
    }
    if (index != 0) { // Our previous chunk may have been the last one
        newVideo.MediaSource = new MediaFileSource(new MemoryStream(chunk), filePath, "video/quicktime");
        if (count == 1) {
            m_ResumableUploader.InsertAsync(YouTubeAuthenticator, newVideo.YouTubeEntry, new MemoryStream(chunk));
            count++;
        }
        else
            m_ResumableUploader.ResumeAsync(YouTubeAuthenticator, new Uri("http://uploads.gdata.youtube.com/resumable/feeds/api/users/uploads"), "POST", new MemoryStream(chunk), "video/quicktime", new object());
    }
    if (index != chunk.Length) { // We didn't read a full chunk: we're done
        break;
    }
}

誰が何が悪いのか教えてもらえますか? 2 GB のビデオがアップロードされません。

4

2 に答える 2

5

403 Forbidden エラーが発生した理由は、渡さなかったという事実によるものでした。

  1. ユーザー名パスワード
  2. 開発者キー

上記のコードのリクエスト変数は、アップロードで使用/送信されていません。そのため、無断アップロードを行っていました。

于 2012-12-18T17:13:04.027 に答える
4

オブジェクトを破棄していない可能性があります。すべての使い捨てオブジェクトが using ステートメント内にあることを確認してください。

たとえば、次のコードは大きな zip ファイルをサーバーにアップロードします。

try
{
    using (Stream ftpStream = FTPRequest.GetRequestStream())
    {
        using (FileStream file = File.OpenRead(ImagesZipFile))
        {
            // set up variables we'll use to read the file
            int length = 1024;
            byte[] buffer = new byte[length];
            int bytesRead = 0;

            // write the file to the request stream
            do
            {
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer, 0, bytesRead);
            }
            while (bytesRead != 0);
        }
    }
}
catch (Exception e)
{
    // throw the exception
    throw e;
}
于 2012-12-13T14:28:31.233 に答える