抖音去水印视频下载

  public class AnalysisUrl
    {
        public int ErrorCode { get; private set; }
        public string VideoUrl { get; private set; }
        private HttpClient httpClient { get; set; }
        public AnalysisUrl(string content)
        {
            using (httpClient = new HttpClient())
            {
                //var content = "#周杰伦 #陈赫开的火锅店 陈赫在长沙招待我们杰迷#长沙贤合庄五一广场asdf  https://v.douyin.com/JhdV1kM/ a复制此链接,打开【抖音短视频】,直接观看视频!";
                var url = GetUrlFromText(content).Result;
                Console.WriteLine(url);
                if (string.IsNullOrWhiteSpace(url))
                {
                    Console.WriteLine("无法提取url");
                    this.ErrorCode = -1;
                    return;
                }
                var fullurl = GetFullUrl(url).Result;
                Console.WriteLine(fullurl);
                if (fullurl == null)
                {
                    Console.WriteLine("无法提取fullurl");
                    this.ErrorCode = -2;
                    return;
                }
                var id = GetId(fullurl.ToString()).Result;
                Console.WriteLine(id);
                if (string.IsNullOrWhiteSpace(id))
                {
                    Console.WriteLine("无法提取id");
                    this.ErrorCode = -3;
                    return;
                }

                var videoUrl = GetVideoUrl(id).Result;
                if (string.IsNullOrWhiteSpace(videoUrl))
                {
                    Console.WriteLine("无法提取VideoUrl");
                    this.ErrorCode = -4;
                    return;
                }

                this.ErrorCode = 0;
                this.VideoUrl = videoUrl;
            }
        }


        public async Task<string> GetUrlFromText(string content)
        {
            Match match = Regex.Match(content, @".*http([a-zA-Z0-9:/\._-]+).*");
            if (match.Success)
            {
                return await Task.FromResult($"http{match.Groups[1].Value}");
            }
            return "";
        }

        public async Task<Uri> GetFullUrl(string shortUrl)
        {
            var response = await httpClient.GetAsync(shortUrl);
            return response.RequestMessage.RequestUri;
        }


        public async Task<string> GetId(string fullurl)
        {
            Match match = Regex.Match(fullurl, @".*video/([\d]+).*");
            if (match.Success)
            {
                return await Task.FromResult($"{match.Groups[1].Value}");
            }
            return "";
        }

        public async Task<string> GetVideoUrl(string id)
        {
            var url = $"https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={id}";
            var response = await httpClient.GetStringAsync(url);
            if (string.IsNullOrWhiteSpace(response))
            {
                return "";
            }
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(response);
            var videoUrl = (string)data.item_list[0].video.play_addr.url_list[0];
            videoUrl = videoUrl.Replace("playwm", "play");
            var requestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(videoUrl)
            };
            requestMessage.Headers.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1");
            requestMessage.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            requestMessage.Headers.Add("accept-encoding", "gzip, deflate, br");
            requestMessage.Headers.Add("accept-language", "zh-CN,zh;q=0.9");
            requestMessage.Headers.Add("Connection", "keep-alive");
            var video_response = await httpClient.SendAsync(requestMessage);
            return video_response.Headers.Location.AbsoluteUri.Replace("http","https");

        }
    }

 

  写了一个抖音去水印的微信小程序,感兴趣的可以玩下

 

 

合作交流:QQ1642487217

 

posted @ 2020-08-26 15:53  王立新  阅读(339)  评论(0编辑  收藏  举报