代码拉取完成,页面将自动刷新
同步操作将从 Johnserf-Seed/TikTokDownload 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Description:懒得优化这一块
@Date :2020/12/28 13:14:29
@Author :JohnserfSeed
@version :1.0
@License :(C)Copyright 2017-2020, Liugroup-NLPR-CASIA
@Mail :johnserfseed@gmail.com
'''
import requests,re,json,sys,getopt
from retrying import retry
def printUsage():
print ('''
使用方法: 1、添加为环境变量 2、输入命令
-u<url 抖音复制的链接:https://v.douyin.com/JtcjTwo/>
-m<music 是否下载音频,默认为yes可选no>
例如:TikTokDownload.exe -u https://v.douyin.com/JtcjTwo/ -m yes
''')
#TikTokDownLoad.exe --url=<抖音复制的链接> --music=<是否下载音频,默认为yes可选no>
def Find(string):
# findall() 查找匹配正则表达式的字符串
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)
return url
def main():
urlarg=""
musicarg="yes"
try:
opts, args = getopt.getopt(sys.argv[1:],"hu:m:",["url=","music="])
except getopt.GetoptError:
printUsage()
sys.exit(-1)
try:
if opts == []:
printUsage()
urlarg = str(input("请输入抖音链接:"))
return urlarg,musicarg
except:
pass
for opt,arg in opts:
if opt == '-h':
printUsage()
sys.exit(-1)
elif opt in ("-u", "--url"):
urlarg=arg
elif opt in ("-m","--music"):
musicarg=arg
return urlarg,musicarg
@retry(stop_max_attempt_number=3)
def download(video_url,music_url,video_title,music_title,headers,musicarg):
#视频下载
if video_url == '':
print('该视频可能无法下载哦~')
return
else:
r=requests.get(url=video_url,headers=headers)
if video_title == '':
video_title = '此视频没有文案_%s' % music_title
with open(f'{video_title}.mp4','wb') as f:
f.write(r.content)
if music_url == '':
input('下载出错,按任意键退出...')
return
else:
#原声下载
if musicarg != 'yes':
input('下载完成,按任意键退出...')
return
else:
r=requests.get(url=music_url,headers=headers)
with open(f'{music_title}.mp3','wb') as f:
f.write(r.content)
input('下载完成,按任意键退出...')
return
def video_download(urlarg,musicarg):
headers = {
'user-agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.66'
}
r = requests.get(url = Find(urlarg)[0])
key = re.findall('video/(\d+)?',str(r.url))[0]
jx_url = f'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={key}' #官方接口
js = json.loads(requests.get(url = jx_url,headers=headers).text)
try:
video_url = str(js['item_list'][0]['video']['play_addr']['url_list'][0]).replace('playwm','play') #去水印后链接
except:
print('视频链接获取失败')
video_url = ''
try:
music_url = str(js['item_list'][0]['music']['play_url']['url_list'][0])
except:
print('该音频目前不可用')
music_url = ''
try:
video_title = str(js['item_list'][0]['desc'])
music_title = str(js['item_list'][0]['music']['author'])
except:
print('标题获取失败')
video_title = '视频走丢啦~'
music_title = '音频走丢啦~'
download(video_url,music_url,video_title,music_title,headers,musicarg)
if __name__=="__main__":
urlarg,musicarg = main()
video_download(urlarg,musicarg)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。