0

我正在使用这个 tiktok API https://www.tiktok.com/node/share/video/userId/videoId,示例用法是https://www.tiktok.com/node/share/video/@beary730/6976969595781434626

它正在返回下面的 json,但那里的所有 url 都没有显示实际播放的视频 url。它只显示一个灰色的播放器,但没有内容。

那篇文章的视频网址是这个

但是下面json的返回(videoUrl)是这样的

这曾经有效,但不再有效。如何抓取实际的视频网址,以便将其嵌入到我们 html 页面的视频播放器中。我应该使用什么api?

   {"seoProps":{"pageId":"6976969595781434626","pageType":0,"predictedLanguage":"en","metaParams":{"title":"Boys true or nah? | with Music original sound  - blueberry","keywords":"blueberry,beary730,TikTok, ティックトック, tik tok, tick tock, tic tok, tic toc, tictok, тик ток, ticktock","description":"blueberry (@beary730) has created a short video on TikTok with music original sound - blueberry. | Boys true or nah?","canonicalHref":"https://www.tiktok.com/@beary730/video/6976969595781434626","robotsContent":"index, follow","applicableDevice":"pc, mobile"},"videoObject":{},"jsonldList":[["VideoObject",{}],["BreadcrumbList",{}]]},"$language":"en","statusCode":0,"statusMsg":"","itemInfo":{"itemStruct":{"id":"6976969595781434626","desc":"Boys true or nah?","createTime":1624452324,"scheduleTime":0,"video":{"id":"6976969595781434626","height":1024,"width":576,"duration":20,"ratio":"720p","cover":"https://p16-sign-sg.tiktokcdn.com/obj/tos-alisg-p-0037/478261e212f4462bb320decf7d75842a?x-expires=1624629600&x-signature=4dL4QF%2FRORT95%2BbGGggPZ1vDK4A%3D","originCover":"https://p16-sign-sg.tiktokcdn.com/obj/tos-alisg-p-0037/ac1eb481ca9e4ce9b10d507e940e0928_1624452326?x-expires=1624629600&x-signature=vM1gkbIzm%2FrsPWMLlD2UoBdpCBE%3D","dynamicCover":"https://p16-sign-sg.tiktokcdn.com/obj/tos-alisg-p-0037/a68e82de8ea64978919ab6ee85ec90ce_1624452327?x-expires=1624629600&x-signature=Yf%2FMkbNj2MHPeG4hxZoqgnewLQ4%3D","playAddr":"https://v16-web.tiktok.com/video/tos/alisg/tos-alisg-pve-0037/1fba13a569b4467c8bd03d7a410b27e2/?a=1988&br=1154&bt=577&cd=0%7C0%7C1&ch=0&cr=0&cs=0&cv=1&dr=3&ds=3&er=&expire=1624629740&l=202106250802010101152290731B032B28&lr=tiktok&mime_type=video_mp4&net=0&pl=0&policy=2&qs=0&rc=M3A4OXU7aWxsNjMzODgzNEApNWdlOTdlOzs1N2dlZzRpZGdkLS5qXzEwbTZgLS1kLy1zc2MzMS80LmEuYl8tMl5eYWA6Yw%3D%3D&signature=b8ca3031e60f752d0dc6ca1b8581daba&tk=tt_webid_v2&vl=&vr=","downloadAddr":"https://v16-web.tiktok.com/video/tos/alisg/tos-alisg-pve-0037/1fba13a569b4467c8bd03d7a410b27e2/?a=1988&br=1154&bt=577&cd=0%7C0%7C1&ch=0&cr=0&cs=0&cv=1&dr=3&ds=3&er=&expire=1624629740&l=202106250802010101152290731B032B28&lr=tiktok&mime_type=video_mp4&net=0&pl=0&policy=2&qs=0&rc=M3A4OXU7aWxsNjMzODgzNEApNWdlOTdlOzs1N2dlZzRpZGdkLS5qXzEwbTZgLS1kLy1zc2MzMS80LmEuYl8tMl5eYWA6Yw%3D%3D&signature=b8ca3031e60f752d0dc6ca1b8581daba&tk=tt_webid_v2&vl=&vr=","shareCover":["","https://p16-sign-sg.tiktokcdn.com/tos-alisg-p-0037/ac1eb481ca9e4ce9b10d507e940e0928_1624452326~tplv-tiktok-play.jpeg?x-expires=1624629600&x-signature=wgto7OEbdMr6o28tJAlOGMmlSug%3D","https://p16-sign-sg.tiktokcdn.com/tos-alisg-p-0037/ac1eb481ca9e4ce9b10d507e940e0928_1624452326~tplv-tiktokx-share-play.jpeg?x-expires=1624629600&x-signature=f3YyHXYponL%2ByVlYjAjubskbhy8%3D"],"reflowCover":"https://p16-sign-sg.tiktokcdn.com/obj/tos-alisg-p-0037/478261e212f4462bb320decf7d75842a?x-expires=1624629600&x-signature=4dL4QF%2FRORT95%2BbGGggPZ1vDK4A%3D","bitrate":591166,"encodedType":"normal","format":"mp4","videoQuality":"normal","encodeUserTag":""},"author":{"id":"6964021718777185281","shortId":"0","uniqueId":"beary730","nickname":"blueberry","avatarLarger":"https://p16-sign-sg.tiktokcdn.com/aweme/1080x1080/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=Iw5hiQ8ZmsjgebJM3pZGs5znDA0%3D","avatarMedium":"https://p16-sign-sg.tiktokcdn.com/aweme/720x720/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=K38vJ6jc%2BSU3zkABKWdea8DvpU4%3D","avatarThumb":"https://p16-sign-sg.tiktokcdn.com/aweme/100x100/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=J07i634gMXmF0BVBJRwOROhUJbo%3D","signature":"BLM\n\nI'M SO SORRY GUYS IF I CAN'T FOLLOW BACK Y'ALL","createTime":1621437798,"verified":false,"secUid":"MS4wLjABAAAAypC_aQxYhCkEfTtrR3y3WtLZnLSvgjHo9cjahmYG-9Kt1HZd4OT8zLHt3O47K50H","ftc":false,"relation":0,"openFavorite":false,"commentSetting":0,"duetSetting":3,"stitchSetting":3,"privateAccount":false,"secret":false,"isADVirtual":false,"roomId":""},"music":{"id":"6976969486352124674","title":"original sound - blueberry","playUrl":"https://sf16-ies-music-sg.tiktokcdn.com/obj/tiktok-obj/6976969478353537794.mp3","coverLarge":"https://p16-sign-sg.tiktokcdn.com/aweme/1080x1080/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=Iw5hiQ8ZmsjgebJM3pZGs5znDA0%3D","coverMedium":"https://p16-sign-sg.tiktokcdn.com/aweme/720x720/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=K38vJ6jc%2BSU3zkABKWdea8DvpU4%3D","coverThumb":"https://p16-sign-sg.tiktokcdn.com/aweme/100x100/tos-alisg-avt-0068/4ccc0bc773116840e424d5e4d45fb2e8.jpeg?x-expires=1624694400&x-signature=J07i634gMXmF0BVBJRwOROhUJbo%3D","authorName":"blueberry","original":true,"duration":19,"album":"","scheduleSearchTime":0},"challenges":[],"stats":{"diggCount":406500,"shareCount":20300,"commentCount":21000,"playCount":2100000},"isActivityItem":false,"duetInfo":{"duetFromId":"0"},"warnInfo":[],"originalItem":false,"officalItem":false,"textExtra":[],"secret":false,"forFriend":false,"digged":false,"itemCommentStatus":0,"showNotPass":false,"vl1":false,"takeDown":0,"itemMute":false,"effectStickers":[],"authorStats":{"followerCount":21600,"followingCount":288,"heart":659400,"heartCount":659400,"videoCount":23,"diggCount":1507},"privateItem":false,"duetEnabled":false,"stitchEnabled":false,"stickersOnItem":[],"isAd":false,"shareEnabled":true,"comments":[]}}}
4

1 回答 1

0

请求视频网址时,您需要:

  • 转发从初始 http 调用中设置的 cookie(特别是tt_webid_v2转发所有 cookie 都可以)
  • RefererHTTP 标头设置为 tiktok 视频 url 的值。此值存在于seoProps.metaParams.canonicalHref字段下的 json 结果中。

请注意,初始 API 调用需要设置用户代理,而视频 api 调用中不需要,但我会指定它以防万一

以下2个示例将视频保存在文件下tiktok.mp4

使用的示例(将 cookie 存储在 cookies.txt 中):

#!/bin/bash

url="https://www.tiktok.com/node/share/video/@beary730/6976969595781434626"
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"

read videoUrl referer < <(echo $(curl -c cookies.txt -s "$url" -H "User-Agent: $userAgent" | 
     jq -r '.itemInfo.itemStruct.video.downloadAddr, .seoProps.metaParams.canonicalHref'))

echo "$videoUrl"
echo "$referer"

curl -s "$videoUrl" -H "Referer: $referer" -H "User-Agent: $userAgent" -b cookies.txt -o tiktok.mp4

中带有的示例:

import requests

url = "https://www.tiktok.com/node/share/video/@beary730/6976969595781434626"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"

s = requests.Session()
r = s.get(url, headers= {
    "User-Agent": userAgent
})
data = r.json()

videoUrl = data["itemInfo"]["itemStruct"]["video"]["downloadAddr"]
referer = data["seoProps"]["metaParams"]["canonicalHref"]

print(videoUrl)
print(referer)

r = s.get(videoUrl, headers= {
    "Referer": referer,
    "User-Agent": userAgent
})
with open('tiktok.mp4', 'wb') as f:
    f.write(r.content)

您将无法在您的网站上按原样嵌入视频 url,因为它需要 cookie 和引用,除非您知道如何伪造tt_webid_v2cookie,但这并不容易,因为 cookie 值似乎取决于expiressignatureurl 参数视频网址。

请注意,它也适用于playAddr字段,它也以相同的方式获取视频馈送数据downloadAddr

于 2021-06-25T22:41:03.270 回答