使用C#观看来自youtube的视频

时间:2012-01-27 10:36:06

标签: c# youtube

为了将视频上传到youtube,我使用以下代码,但在此我将指定文件的路径

private void button1_Click(object sender, EventArgs e)
{
            string user, pass, source;
            user = textBox1.Text;
            pass = textBox2.Text;
            source = textBox3.Text;
            YouTubeRequestSettings settings = new YouTubeRequestSettings("Eclipse", "AI39si554yyVqQDqtZff_kuRyQLg5wp5RjxCtEB_qRmsoF6Cm0AQWG-h_uuCjrmoDAZ3-32JXpAgdSOsJN7VmQzR_2CLHd7NxA", user, pass);
            YouTubeRequest request = new YouTubeRequest(settings);
            Video newVideo = new Video();
        newVideo.Title = "A";// "Smideo Generated Movie";
        newVideo.Tags.Add(new MediaCategory("Travel", YouTubeNameTable.CategorySchema));
        newVideo.Keywords = "Animals";
        newVideo.Description = "TEsting";
        newVideo.YouTubeEntry.Private = false;
        newVideo.Tags.Add(new MediaCategory("Test, Example",YouTubeNameTable.DeveloperTagSchema));
        try
        {
              request.Upload(newVideo);
        }
        catch(InvalidCredentialsException c)
        {
            MessageBox.Show("Error");
        }
}

2 个答案:

答案 0 :(得分:1)

request .Headers.Add("Slug", Path.GetFileName({videoFileName}));

最好创建一个执行上传的方法,而不是将所有代码放在事件处理程序中。

要在您上传视频,您可以使用以下代码:

public bool Upload(string title, string description, Catagory catagory,
              string keywords, string videoFileName, out string error)
{
    bool result = false;
    error = null;

    // Build byte arrays for the header file and footer
    byte[] header = Encoding.UTF8.GetBytes(GetHeader(title, description, catagory, keywords, videoFileName));
    byte[] file = File.ReadAllBytes(videoFileName);
    byte[] footer = Encoding.UTF8.GetBytes(lineTerm + boundary + "--");

    // Combine the byte arrays into one big byte array
    byte[] data = new byte[header.Length + file.Length + footer.Length];
    Array.Copy(header, 0, data, 0, header.Length);
    Array.Copy(file, 0, data, header.Length, file.Length);
    Array.Copy(footer, 0, data, header.Length + file.Length, footer.Length);

    // Using a HttpWebRequest here because it allows us to control the timeout
    HttpWebRequest req = (HttpWebRequest)WebRequest
        .Create(string.Format("http://uploads.gdata.youtube.com/feeds/api/users/{0}/uploads",
                username));
    req.Method = "POST";
    req.ContentType = string.Format("multipart/related; boundary={0};", boundaryheader);
    req.ContentLength = data.Length;
    req.Timeout = timeout;
    req.Headers.Add("Authorization", "GoogleLogin auth=" + authCode);
    req.Headers.Add("X-GData-Client", clientCode); // supposed to be optional
    req.Headers.Add("X-GData-Key", devCode);
    req.Headers.Add("Slug", Path.GetFileName(videoFileName));

    using (Stream postStream = req.GetRequestStream())
    {
        // Send the data to the server
        postStream.WriteTimeout = timeout;
        postStream.Write(data, 0, data.Length);
        postStream.Close();

        try
        {
            // Get the response back from the server
            WebResponse webResponse = req.GetResponse();
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    // Should check the response here
                    reader.Close();
                    result = true;
                }

                responseStream.Close();
            }

            webResponse.Close();
        }
        catch (WebException ex)
        {
            // Got a bad response
            using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))
            {
                error = sr.ReadToEnd();
            }
        }
    }

    return result;
}

更多信息:

http://idevhub.com/programmatically-uploading-videos-to-youtube/

答案 1 :(得分:0)

您可以使用MediaSource属性:

MediaFileSource ms = new MediaFileSource("C:\\test.wmv", "video/x-ms-wmv");
newVideo.YouTubeEntry.MediaSource = ms;