Delphi 通过TNetHTTPClient访问http,最新解析快手无水印视频地址链接方法

  一、解析快手无水印视频链接原理

共分三个步骤:

1.通过视频分享获得视频地址短链接;如:

https://www.kuaishou.com/f/X7tIV0jIivYUyTk

2.通过TNetHTTPClient重定向获得视频地址长链接;如:

https://www.kuaishou.com/short-video/3xvx3jg7ffi3xrs?authorId=3xcjmgxvqvwr65y&streamSource=profile&area=profilexxnull

3.通过地址长链接得到视频ID;如:

本视频的ID为:3xvx3jg7ffi3xrs

4.通过TNetHTTPClient发送一个包含视频ID的post请求。请求成功服务器返回(response)一个包含无水印视频地址,封面,标题等信息的JSON数据;

5.解析response的JSON数据,得到无水印视频地址,就可以下载了。

二、解析无水印视频代码

  介绍了原理,废话不多说,直接上代码:

unit uKuaishou;

interface
uses
  windows,classes,System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent,
  System.SysUtils,strutils,System.RegularExpressions,system.JSON;
const
  wm_user=$0400;
  wm_downfile=wm_user+100+1;

  //定义UserAgent为计算机操作系统:
  USER_AGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';

  //定义Cookies为你本人的Cookies,可以从浏览器的取得;不懂的+V:byc6352
  KUAISHOU_COOKIES='你本人的Cookies';

  //这个API_URL为发送post请求的服务器地址:
  API_URL='https://video.kuaishou.com/graphql';

  //KUAISHOU_ARGS为发送post请求的数据体,其中 photoId为视频唯一ID;
  KUAISHOU_ARGS='{"operationName":"visionVideoDetail","variables":{"photoId":"%s","page":"detail","webPageArea":"profilexxnull"},'+
  '"query":"query visionVideoDetail($photoId: String, $type: String, $page: String, $webPageArea: String) {visionVideoDetail(photoId: $photoId, type: $type, page: $page, webPageArea: $webPageArea) {photo{id duration timestamp caption coverUrl photoUrl}}}"}';

type
  TKuaishou=class(TThread)
   private
     FId:cardinal;            //线程编号;
     Furl:string;             //视频分享地址(短地址);
     FRedirectedUrl:string;   //重定向后的地址(长地址);
     Fvideourl:string;        //解析成功后的视频地址;
     FvideoId:string;         //视频ID;
     Ffilename:string;        //保存到本机的视频文件名;
     FvideoTitle:string;      //视频标题;
     FcoverUrl:string;        //视频封面地址;
     Fmsg:string;             //解析成功与否的消息;
     class var Fform: HWND;   //发送数据到窗体的HWND;
     procedure SetId(id:cardinal);    //设置线程编号;
     class procedure SetForm(const hForm: HWND); static;   //设置窗体句柄;



   protected
     procedure Execute; override;
   public
     //id:为线程编号;url为视频分享短链接地址;
     constructor Create(id:cardinal;url:string);   
     destructor Destroy;
     property id:cardinal read FId write SetId;
     property url:string read Furl;
     property msg:string read Fmsg;
     property filename:string read Ffilename;
     property videourl:string read Fvideourl;
     property videoTitle:string read FvideoTitle;
     property RedirectedUrl:string read FRedirectedUrl;
     property videoId:string read FvideoId;
     property coverUrl:string read FcoverUrl;

     //向服务器发送post请求,返回视频信息的JSON数据;
     function getPostResult(data:string):string;

     //向服务器发送GET请求,获取重定向长链接地址;
     function getRedirectedUrl(url:string):string;

     //从长链接解析出视频ID;
     function getVideoId(txt:string):string;

     //获取无水印视频地址,封面链接,视频标题;
     function getVideoUrl():string;

     //解析服务器返回的JSON数据
     function parseJson(jo:string):string;
     class property form: HWND read Fform write SetForm;

  end;



implementation
constructor TKuaishou.Create(id:cardinal;url:string);
begin
  inherited Create(True);
  FId:=id;
  Furl:=url;
end;
destructor TKuaishou.Destroy;
begin
  inherited Destroy;
end;

//放在线程中执行,不影响主窗体响应;
procedure TKuaishou.Execute;
begin
  getVideoUrl();
end;

//获取无水印视频地址,封面链接,视频标题;
function TKuaishou.getVideoUrl():string;
var
  args,jo:string;
begin
  result:='';

  //向服务器发送GET请求,获取重定向长链接地址;
  FRedirectedUrl:=getRedirectedUrl(Furl);
  if(FRedirectedUrl)='' then exit;

  //从长链接解析出视频ID;
  FvideoId:=getVideoId(FRedirectedUrl);
  if(FvideoId)='' then exit;

  //构造post请求的data数据体;
  args:=format(KUAISHOU_ARGS,[FvideoId]);

  //向服务器发送post请求,获得视频信息的json;
  jo:=getPostResult(args);
  if(jo)='' then exit;
  Fmsg:=jo;

  //解析json获取无水印地址;
  result:=parseJson(jo);
end;

//解析服务器返回的JSON数据
function TKuaishou.parseJson(jo:string):string;
var
  json,j1: TJSONObject;
begin
  result:='';
  if(pos('errors',jo)>0)then exit;
try
  json := TJSONObject.ParseJSONValue(jo) as TJSONObject;
  if json = nil then exit;
  j1:=json.GetValue('data') as TJSONObject;
  j1:=j1.GetValue('visionVideoDetail') as TJSONObject;
  j1:=j1.GetValue('photo') as TJSONObject;
  FvideoTitle:=j1.GetValue('caption').Value;
  FcoverUrl:=j1.GetValue('coverUrl').Value;
  FvideoUrl:=j1.GetValue('photoUrl').Value;

  result:='#100#'+FvideoUrl+'#'+FcoverUrl+'#'+FvideoTitle;
finally
  if json <> nil then json.Free;
end;
end;

//向服务器发送post请求,返回视频信息的JSON数据;
function TKuaishou.getPostResult(data:string):string;
var
  client: TNetHTTPClient;
  ss,args: TStringStream;
begin
  result:='';
try
  client := TNetHTTPClient.Create(nil);
  SS := TStringStream.Create('', TEncoding.UTF8);
  ss.Clear;
  args := TStringStream.Create(data, TEncoding.UTF8);
  with client do
  begin
    ConnectionTimeout := 10000; // 10秒
    ResponseTimeout := 10000; // 10秒
    AcceptCharSet := 'utf-8';
    UserAgent := USER_AGENT; //1
    client.AllowCookies:=true;
    client.HandleRedirects:=true;
    Accept:='*/*';
    client.ContentType:='application/json'; //2

    //这儿写入你电脑的cookies,技术支持:+V:byc6352
    client.CustomHeaders['Cookie'] := KUAISHOU_COOKIES;
    client.CustomHeaders['Referer'] := Furl;
    //发送post请求:
    client.Post(API_URL,args,ss);
    result:=ss.DataString;
  end;
finally
  ss.Free;
  args.Free;
  client.Free;
end;
end;

//向服务器发送GET请求,获取重定向长链接地址;
function TKuaishou.getRedirectedUrl(url:string):string;
var
  client: TNetHTTPClient;
  ss: TStringStream;
  s,id:string;
  AResponse:IHTTPResponse;
  i:integer;
begin
try
  client := TNetHTTPClient.Create(nil);
  SS := TStringStream.Create('', TEncoding.UTF8);
  ss.Clear;
  with client do
  begin
    ConnectionTimeout := 2000; // 2秒
    ResponseTimeout := 2000; // 2秒
    AcceptCharSet := 'utf-8';
    UserAgent := USER_AGENT;
    client.AllowCookies:=true;
    client.HandleRedirects:=false;   //这儿一定要设置不允许重定向;
    Accept:='*/*';
    try
        AResponse:=Get(url, ss);

        s:=AResponse.HeaderValue['Location'];   //重定向地址在Location头中;
        if(s='')then exit;

        result:=s;
    except
      on E: Exception do
        Log(e.Message);

    end;
  end;
finally
  ss.Free;
  client.Free;
end;
end;

//从长地址获取视频ID;
function TKuaishou.getVideoId(txt:string):string;
var
 m:TMatch;
  i:integer;
begin
  result:='';
  if(length(txt)<15)then exit;
  result:=midstr(txt,14,15);
end;

//------------------------------------------属性方法-------------------------------------
 procedure TKuaishou.SetId(Id:cardinal);
 begin
   FId:=Id;
 end;
 class procedure TKuaishou.SetForm(const hForm: HWND);
 begin
   Fform:=hForm;
 end;
end.

  以上类TKuaishou可以直接使用;

三、解析类使用方法:

procedure TfMain.btnParseClick(Sender: TObject);
var
  kuaishou:TKuaishou;
  url,result:string;
begin
  url:=trim(edtUrl.Text);
  kuaishou:=TKuaishou.Create(1,url);

  //为了演示简单,这儿没有使用线程解析。
  result:=kuaishou.getVideoUrl();
  if(result='')then
  begin
    memo1.Text:=kuaishou.msg;
    edit3.Text:='';
    showmessage('解析失败!');
    exit;
  end;
  //视频连接
  edit1.Text:=kuaishou.videourl;
  //封面链接
  edit2.Text:=kuaishou.coverurl;
  edit3.Text:=result;
  memo1.Text:=kuaishou.msg;
end;

四、解析程序

1、解析后无水印视频链接地址

https://txmov2.a.kwimgs.com/upic/2023/01/30/05/BMjAyMzAxMzAwNTAwNDZfMjQ2NzExNzg0N185NDk2MTQ0MDk0NF8wXzM=_b_B8055d1d7054f2de85f9bc56e6f73e335.mp4?pkey=AAXY4734ntsAaSz82LuzbXlFNObOF_zOPnH7puUnD1DaNrANsUUAjmGa6Yxz7jAnAszT2e4PdnlBWHxl2dwkhMMGOIZ9qkKliU3yDy_BXQZmJbdV2d6F7_9sEiwpTA9U8gk&tag=1-1675240486-unknown-0-tfsgzikc3z-de39f632a2a7c7cf&clientCacheKey=3xvx3jg7ffi3xrs_b.mp4&di=6a385727&bp=10004&tt=b&ss=vp

2、程序界面:

Delphi 通过TNetHTTPClient访问http,最新解析快手无水印视频地址链接方法_第1张图片

3、程序下载:

http://helpnow.top/ks 

4、另外推荐一款(tubeget高清视频下载器),支持全球1000多个视频站点:

http://helpnow.top/tubeget/

你可能感兴趣的:(delphi,windows,http,json,https)