从vimeo下载视频

本文关键字:视频 下载 vimeo | 更新日期: 2023-09-27 18:18:11

我正在尝试用c#从vimeo下载视频。问题是,当我必须接收代码302来执行重定向时,我接收到代码200(带有一个显示"permission denied"的页面)。这是代码:

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;
                }
            }
        }
你能帮我一下吗?

谢谢

从vimeo下载视频

这段代码适合我。我用简单的按钮创建了新的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;
        }

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";