【问题标题】:Download videos from vimeo从 vimeo 下载视频
【发布时间】:2011-12-19 12:15:03
【问题描述】:

我正在尝试使用 c# 从 vimeo 下载视频。 问题是当我必须接收解码代码 302 进行重定向时,我收到了代码 200(带有显示“权限被拒绝”的页面)。 这是代码:

string url = "http://player.vimeo.com/play_redirect?clip_id=" + clip_id + "&sig=" + request_signature + "&time=" + request_signature_expires + "&quality=" + hd + "&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location=";

_VideoURL = GetHttpSpecial(url, cookies);

public string GetHttpSpecial(string url, CookieContainer cookies)
        {
            string html = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers.Add("Accept-Language: nl,en-us;q=0.7,en;q=0.3");
            request.Headers.Add("Accept-Encoding: gzip, deflate");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0";
            request.KeepAlive = true;
            request.Timeout = 20000;
            request.AllowAutoRedirect = false;
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            foreach (Cookie cookie in response.Cookies)
            {
                cookies.Add(cookie);
            }
            if (response.StatusCode == HttpStatusCode.OK)
            {
                try
                {
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    try
                    {
                        html = reader.ReadToEnd();
                    }
                    finally
                    {
                        reader.Close();
                    }
                }
                finally
                {
                    response.Close();
                }
                return html;
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.Found)
                {
                    return response.Headers["Location"].ToString();
                }
                else
                {
                    return "error: server returned status description:" + response.StatusDescription;
                }
            }
        }

你能帮帮我吗?

谢谢

【问题讨论】:

  • 嗨,Sara,欢迎来到该网站。当您以编程方式尝试下载视频或仅通过网站使用 vimeo 时,您是否看到了这种情况?
  • 我正在尝试使用我的应用程序编程从 vimeo 下载视频

标签: c# video vimeo


【解决方案1】:

此代码对我有用。 我用简单的按钮创建了新的 WPF 项目。这是代码隐藏:

private Dictionary<int, CookieContainer> m_cookieContainer = new Dictionary<int, CookieContainer>();

private void Button_Click(object sender, RoutedEventArgs e)
        {
            HttpWebRequest req = CreateRequest("http://vimeo.com/42082443");
            req.AllowAutoRedirect = false;
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers[HttpRequestHeader.AcceptLanguage] = "ru,en;q=0.8,en-us;q=0.5,uk;q=0.3";
            req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
            req.KeepAlive = true;
            req.Timeout = 20000;
            req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            using (HttpWebResponse response = GetResponse(req))
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string pageData = new StreamReader(response.GetResponseStream()).ReadToEnd();

                    string clipId = null;
                    if (Regex.Match(pageData, @"clip_id=(\d+)", RegexOptions.Singleline).Success)
                    {
                        clipId = Regex.Match(pageData, @"clip_id=(\d+)", RegexOptions.Singleline).Groups[1].ToString();
                    }
                    else if (Regex.Match(pageData, @"(\d+)", RegexOptions.Singleline).Success)
                    {
                        clipId = Regex.Match(pageData, @"(\d+)", RegexOptions.Singleline).Groups[1].ToString();
                    }

                    string sig = Regex.Match(pageData, "\"signature\":\"(.+?)\"", RegexOptions.Singleline).Groups[1].ToString();
                    string timestamp = Regex.Match(pageData, "\"timestamp\":(\\d+)", RegexOptions.Singleline).Groups[1].ToString();

                    string videoUrl = string.Format("http://player.vimeo.com/play_redirect?clip_id={0}&sig={1}&time={2}&quality=hd&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location=", clipId, sig, timestamp);

                    req = CreateRequest(videoUrl);
                    req.AllowAutoRedirect = false;
                    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
                    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Headers[HttpRequestHeader.AcceptLanguage] = "ru,en;q=0.8,en-us;q=0.5,uk;q=0.3";
                    req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
                    req.KeepAlive = true;
                    req.Referer = "http://a.vimeocdn.com/p/flash/moogaloop/5.2.25/moogaloop.swf?v=1.0.0";
                    req.Timeout = 20000;
                    req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                    using (HttpWebResponse response2 = GetResponse(req))
                    {
                        if (response2.StatusCode == HttpStatusCode.Found)
                        {
                            string location = response2.Headers[HttpResponseHeader.Location];
                            MessageBox.Show(location);
                        }
                    }
                }
            }
        }

        private CookieContainer GetCookieContainerPerThread()
        {
            int managedThreadId = Thread.CurrentThread.ManagedThreadId;
            lock (typeof(MainWindow))
            {
                if (!this.m_cookieContainer.ContainsKey(managedThreadId))
                {
                    CookieContainer container = new CookieContainer();
                    this.m_cookieContainer.Add(managedThreadId, container);
                }
            }
            return this.m_cookieContainer[managedThreadId];
        }

        public HttpWebRequest CreateRequest(string url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.CookieContainer = this.GetCookieContainerPerThread();
            //this.InitProxy(req);
            return req;
        }

        public HttpWebResponse GetResponse(HttpWebRequest req)
        {
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            this.GetCookieContainerPerThread().Add(response.Cookies);
            return response;
        }

【讨论】:

  • 我试过你的答案,但我只能看到“位置”:如何使用该位置下载视频?谢谢
  • @Marco Location 应该是远程服务器上物理视频文件的直接链接。将其传递给 WebClient.DownloadFile
  • 嗯,它适用于您使用的视频链接。但是,如果我尝试使用thisthis other,则位置为空。你有什么主意吗?非常感谢
  • 好的,问题解决了。如果您愿意,可以将此信息添加到您的帖子中:您在撰写 string videoUrl = ... 时正在设置 quality=hd;如果视频不是高清,则返回的位置将为空。只需删除该部分&amp;quality=hd 即可返回位置。因此,也许您可​​以先尝试检查高清视频;然后,如果没有找到,切换到正常的...
  • @Marco 是的,我很久以前用过,忘记了,你可以检查响应状态码和null,如果失败,重复相同的操作,但没有质量参数
【解决方案2】:

Sara,请使用 Fiddler 检查请求/响应,以确保您发送的是相同的。当我使用该网站正常观看播放视频时,有一个 vimeo cookie 来回传递,而不是当您直接调用播放链接时。

也许尝试先向主页发出请求以获取 vimeo cookie(来自 Repsonse.GetResponseHeader("Set-Cookie") 并在您的进一步调用中使用它。

这可能是因为 Vimeo 不允许未从其网站引用的请求,因此您可能还必须执行

request.Referer =
            "http://a.vimeocdn.com/[[IMPORT]]/player.vimeo.com/assets/flash/moogaloop/5.2.8/controllers/videoController.swf";

【讨论】:

  • 我已经用 Fiddle 检查过了,请求/响应是:
     # Result Protocol Host URL Body Caching Content-Type Process Comments Custom 118 302 HTTP player.vimeo.com / play_redirect?clip_id=33735037&amp;sig=85b60dd8d19ec06b9307fbc1e733048d&amp;time=1324303943&amp;quality=hd&amp;codecs=H264,VP8,VP6&amp;type=moogaloop_local&amp;embed_location= 20 no-store, no-cache, must-revalidate text/html;Fri, 25:200018:01GMT charset=UTF-8 avp:3544 119 200 HTTP av.vimeo.com /38970/772/76932486.mp4?token=1324304859_f553bbbfb98f07551487cefe95ec3135 55.608.510 video/mp4 avp:3544 
                    
                  
                    
                
  • 使用 c# 中的代码,我从未收到 302 响应。我收到一个 200 代码,但权限被拒绝。我尝试过使用推荐人,但我无法重定向。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多