使用C#从youtube观看视频

本文关键字:视频 youtube 使用 | 更新日期: 2023-09-27 18:21:39

为了将视频上传到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");
        }
}

使用C#从youtube观看视频

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/

您可以使用MediaSource属性:

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