示例代码:
import re
import sys
import requests
DEBUG = False
headers = {
'authority': 'v.douyin.com',
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 '
'(KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1',
}
# dy_url = input('请输入抖音直播链接或19位room_id:')
dy_url = "https://v.douyin.com/MUnygKj/"
if re.match(r'\d{19}', dy_url):
room_id = dy_url
else:
try:
dy_url = re.search(r'(https.*)', dy_url).group(1)
response = requests.head(dy_url, headers=headers)
dy_url = response.headers['location']
room_id = re.search(r'\d{19}', dy_url).group(0)
except Exception as e:
if DEBUG:
print(e)
print('获取room_id失败')
sys.exit(1)
print('room_id', room_id)
try:
headers.update({
'authority': 'webcast.amemv.com',
# 换成自己的cookie
'cookie': 'ttwid=1|Ds7gVOTErqmpb5XOo7-G5_oKCftIDUGigBJhRBN31L0|'
'1666164618|e6ea3392abddb66ed4ba4d16467cc9d929ff329ea8ac378ffa177f78896f6a58; '
'home_can_add_dy_2_desktop="0"; strategyABtestKey=1666164791.535; '
'passport_csrf_token=d2ffe114d656ec3aef9eff5867aa2fba;'
' passport_csrf_token_default=d2ffe114d656ec3aef9eff5867aa2fba; '
'csrf_session_id=8a6b3fb5a12e4f3f86e085b9a68d7476; '
'odin_tt=8d4eb0c736f21b88f12f19104acc781af9a97ce820c867778893df4539e9d5cc4'
'e064c0654589cec48f393900bb0b73fffb04045dd3cb264e19f1bc4d40c6c18b67e9b5d9'
'dde8e4f1fd5208b3a2902c3; xgplayer_user_id=331707708752; ttcid=f696608888c748bb9c17'
'e79528de26dc10; __ac_nonce=0634fa7a70079f8c3a7d4; __ac_signature'
'=_02B4Z6wo00f013s5M7wAAIDD-zvJ.qTHRp97GTcAAL2NzFAkZpehV1A-1pMi7436GHUuGAvTT'
'lBLznSIekIw-TuZrj-dGI17PBhPUGD4paiw9C3-uLgauuiHtj3rXDf-Nms.EqG37vUnC1KC6b; '
'live_can_add_dy_2_desktop="1"; msToken=zgQnLjoQgMbLgs16HNUGCciEHJYEvPCgIHkXi2dj_IRzi7H4xHL'
'swbOzIJtnI2XZ_a5CBtUrLYynHPmidwrqg_bWSzmsEvMnnLCK0kX_BMmz7aeuwhUy; msToken=7IzH_HHgu8tdKiPVk'
'ffk7KI95VpvtAaBnEAIhjcggs-TTQGnpJnFanlIQ1XGHguUCDJcWrhJoaOq6BBR-auJnLKkcGtnnBb-az12YfnihJmHQWCYRypR;'
' tt_scid=yZ.ZZCrBjn51Zz.WEO15cZZUOBrjrvDdN4n7d6k15td83buxr09nMlRERLZnfPou409e',
})
response = requests.get(
'https://webcast.amemv.com/webcast/room/reflow/info/?verifyFp='
'&type_id=0&live_id=1&room_id={}&sec_user_id=&app_id=1128&msToken=&X-Bogus='.format(
room_id), headers=headers, ).json()
print(response)
rtmp_pull_url = response['data']['room']['stream_url']['rtmp_pull_url']
hls_pull_url = response['data']['room']['stream_url']['hls_pull_url']
print(rtmp_pull_url)
print(hls_pull_url)
except Exception as e:
if DEBUG:
print(e)
print('获取real url失败')