mirror of
https://github.com/soimort/you-get.git
synced 2025-03-12 19:07:56 +03:00
add format selection for AcFun
This commit is contained in:
parent
00e2ce3f48
commit
5c9ec6c4f3
@ -1,114 +1,159 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
__all__ = ['acfun_download']
|
|
||||||
|
|
||||||
from ..common import *
|
from ..common import *
|
||||||
|
from ..extractor import VideoExtractor
|
||||||
|
|
||||||
from .le import letvcloud_download_by_vu
|
class AcFun(VideoExtractor):
|
||||||
from .qq import qq_download_by_vid
|
name = "AcFun"
|
||||||
from .sina import sina_download_by_vid
|
|
||||||
from .tudou import tudou_download_by_iid
|
|
||||||
from .youku import youku_download_by_vid
|
|
||||||
|
|
||||||
import json
|
stream_types = [
|
||||||
import re
|
{'id': '2160P', 'qualityType': '2160p'},
|
||||||
import base64
|
{'id': '1080P60', 'qualityType': '1080p60'},
|
||||||
import time
|
{'id': '720P60', 'qualityType': '720p60'},
|
||||||
|
{'id': '1080P+', 'qualityType': '1080p+'},
|
||||||
|
{'id': '1080P', 'qualityType': '1080p'},
|
||||||
|
{'id': '720P', 'qualityType': '720p'},
|
||||||
|
{'id': '540P', 'qualityType': '540p'},
|
||||||
|
{'id': '360P', 'qualityType': '360p'}
|
||||||
|
]
|
||||||
|
|
||||||
def get_srt_json(id):
|
def prepare(self, **kwargs):
|
||||||
url = 'http://danmu.aixifan.com/V2/%s' % id
|
assert re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/(\D|bangumi)/\D\D(\d+)', self.url)
|
||||||
return get_content(url)
|
|
||||||
|
|
||||||
def youku_acfun_proxy(vid, sign, ref):
|
if re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/\D/\D\D(\d+)', self.url):
|
||||||
endpoint = 'http://player.acfun.cn/flash_data?vid={}&ct=85&ev=3&sign={}&time={}'
|
html = get_content(self.url, headers=fake_headers)
|
||||||
url = endpoint.format(vid, sign, str(int(time.time() * 1000)))
|
json_text = match1(html, r"(?s)videoInfo\s*=\s*(\{.*?\});")
|
||||||
json_data = json.loads(get_content(url, headers=dict(referer=ref)))['data']
|
json_data = json.loads(json_text)
|
||||||
enc_text = base64.b64decode(json_data)
|
vid = json_data.get('currentVideoInfo').get('id')
|
||||||
dec_text = rc4(b'8bdc7e1a', enc_text).decode('utf8')
|
up = json_data.get('user').get('name')
|
||||||
youku_json = json.loads(dec_text)
|
self.title = json_data.get('title')
|
||||||
|
video_list = json_data.get('videoList')
|
||||||
|
if len(video_list) > 1:
|
||||||
|
self.title += " - " + [p.get('title') for p in video_list if p.get('id') == vid][0]
|
||||||
|
currentVideoInfo = json_data.get('currentVideoInfo')
|
||||||
|
|
||||||
|
elif re.match("https?://[^\.]*\.*acfun\.[^\.]+/bangumi/aa(\d+)", self.url):
|
||||||
|
html = get_content(self.url, headers=fake_headers)
|
||||||
|
tag_script = match1(html, r'<script>\s*window\.pageInfo([^<]+)</script>')
|
||||||
|
json_text = tag_script[tag_script.find('{') : tag_script.find('};') + 1]
|
||||||
|
json_data = json.loads(json_text)
|
||||||
|
self.title = json_data['bangumiTitle'] + " " + json_data['episodeName'] + " " + json_data['title']
|
||||||
|
vid = str(json_data['videoId'])
|
||||||
|
up = "acfun"
|
||||||
|
currentVideoInfo = json_data.get('currentVideoInfo')
|
||||||
|
|
||||||
yk_streams = {}
|
|
||||||
for stream in youku_json['stream']:
|
|
||||||
tp = stream['stream_type']
|
|
||||||
yk_streams[tp] = [], stream['total_size']
|
|
||||||
if stream.get('segs'):
|
|
||||||
for seg in stream['segs']:
|
|
||||||
yk_streams[tp][0].append(seg['url'])
|
|
||||||
else:
|
else:
|
||||||
yk_streams[tp] = stream['m3u8'], stream['total_size']
|
raise NotImplemented
|
||||||
|
|
||||||
return yk_streams
|
if 'ksPlayJson' in currentVideoInfo:
|
||||||
|
durationMillis = currentVideoInfo['durationMillis']
|
||||||
|
ksPlayJson = ksPlayJson = json.loads( currentVideoInfo['ksPlayJson'] )
|
||||||
|
representation = ksPlayJson.get('adaptationSet')[0].get('representation')
|
||||||
|
stream_list = representation
|
||||||
|
|
||||||
def acfun_download_by_vid(vid, title, output_dir='.', merge=True, info_only=False, **kwargs):
|
for stream in stream_list:
|
||||||
"""str, str, str, bool, bool ->None
|
m3u8_url = stream["url"]
|
||||||
|
size = durationMillis * stream["avgBitrate"] / 8
|
||||||
|
# size = float('inf')
|
||||||
|
container = 'mp4'
|
||||||
|
stream_id = stream["qualityLabel"]
|
||||||
|
quality = stream["qualityType"]
|
||||||
|
|
||||||
Download Acfun video by vid.
|
stream_data = dict(src=m3u8_url, size=size, container=container, quality=quality)
|
||||||
|
self.streams[stream_id] = stream_data
|
||||||
|
|
||||||
Call Acfun API, decide which site to use, and pass the job to its
|
assert self.title and m3u8_url
|
||||||
extractor.
|
self.title = unescape_html(self.title)
|
||||||
"""
|
self.title = escape_file_path(self.title)
|
||||||
|
p_title = r1('active">([^<]+)', html)
|
||||||
|
self.title = '%s (%s)' % (self.title, up)
|
||||||
|
if p_title:
|
||||||
|
self.title = '%s - %s' % (self.title, p_title)
|
||||||
|
|
||||||
#first call the main parasing API
|
|
||||||
info = json.loads(get_content('http://www.acfun.cn/video/getVideo.aspx?id=' + vid, headers=fake_headers))
|
|
||||||
|
|
||||||
sourceType = info['sourceType']
|
def download(self, **kwargs):
|
||||||
|
if 'json_output' in kwargs and kwargs['json_output']:
|
||||||
#decide sourceId to know which extractor to use
|
json_output.output(self)
|
||||||
if 'sourceId' in info: sourceId = info['sourceId']
|
elif 'info_only' in kwargs and kwargs['info_only']:
|
||||||
# danmakuId = info['danmakuId']
|
if 'stream_id' in kwargs and kwargs['stream_id']:
|
||||||
|
# Display the stream
|
||||||
#call extractor decided by sourceId
|
stream_id = kwargs['stream_id']
|
||||||
if sourceType == 'sina':
|
if 'index' not in kwargs:
|
||||||
sina_download_by_vid(sourceId, title, output_dir=output_dir, merge=merge, info_only=info_only)
|
self.p(stream_id)
|
||||||
elif sourceType == 'youku':
|
|
||||||
youku_download_by_vid(sourceId, title=title, output_dir=output_dir, merge=merge, info_only=info_only, **kwargs)
|
|
||||||
elif sourceType == 'tudou':
|
|
||||||
tudou_download_by_iid(sourceId, title, output_dir=output_dir, merge=merge, info_only=info_only)
|
|
||||||
elif sourceType == 'qq':
|
|
||||||
qq_download_by_vid(sourceId, title, True, output_dir=output_dir, merge=merge, info_only=info_only)
|
|
||||||
elif sourceType == 'letv':
|
|
||||||
letvcloud_download_by_vu(sourceId, '2d8c027396', title, output_dir=output_dir, merge=merge, info_only=info_only)
|
|
||||||
elif sourceType == 'zhuzhan':
|
|
||||||
#As in Jul.28.2016, Acfun is using embsig to anti hotlink so we need to pass this
|
|
||||||
#In Mar. 2017 there is a dedicated ``acfun_proxy'' in youku cloud player
|
|
||||||
#old code removed
|
|
||||||
url = 'http://www.acfun.cn/v/ac' + vid
|
|
||||||
yk_streams = youku_acfun_proxy(info['sourceId'], info['encode'], url)
|
|
||||||
seq = ['mp4hd3', 'mp4hd2', 'mp4hd', 'flvhd']
|
|
||||||
for t in seq:
|
|
||||||
if yk_streams.get(t):
|
|
||||||
preferred = yk_streams[t]
|
|
||||||
break
|
|
||||||
#total_size in the json could be incorrect(F.I. 0)
|
|
||||||
size = 0
|
|
||||||
for url in preferred[0]:
|
|
||||||
_, _, seg_size = url_info(url)
|
|
||||||
size += seg_size
|
|
||||||
#fallback to flvhd is not quite possible
|
|
||||||
if re.search(r'fid=[0-9A-Z\-]*.flv', preferred[0][0]):
|
|
||||||
ext = 'flv'
|
|
||||||
else:
|
else:
|
||||||
|
self.p_i(stream_id)
|
||||||
|
else:
|
||||||
|
# Display all available streams
|
||||||
|
if 'index' not in kwargs:
|
||||||
|
self.p([])
|
||||||
|
else:
|
||||||
|
stream_id = self.streams_sorted[0]['id'] if 'id' in self.streams_sorted[0] else self.streams_sorted[0]['itag']
|
||||||
|
self.p_i(stream_id)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if 'stream_id' in kwargs and kwargs['stream_id']:
|
||||||
|
# Download the stream
|
||||||
|
stream_id = kwargs['stream_id']
|
||||||
|
else:
|
||||||
|
stream_id = self.streams_sorted[0]['id'] if 'id' in self.streams_sorted[0] else self.streams_sorted[0]['itag']
|
||||||
|
|
||||||
|
if 'index' not in kwargs:
|
||||||
|
self.p(stream_id)
|
||||||
|
else:
|
||||||
|
self.p_i(stream_id)
|
||||||
|
if stream_id in self.streams:
|
||||||
|
url = self.streams[stream_id]['src']
|
||||||
|
ext = self.streams[stream_id]['container']
|
||||||
|
total_size = self.streams[stream_id]['size']
|
||||||
|
|
||||||
|
|
||||||
|
if ext == 'm3u8' or ext == 'm4a':
|
||||||
ext = 'mp4'
|
ext = 'mp4'
|
||||||
print_info(site_info, title, ext, size)
|
|
||||||
if not info_only:
|
|
||||||
download_urls(preferred[0], title, ext, size, output_dir=output_dir, merge=merge)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(sourceType)
|
|
||||||
|
|
||||||
if not info_only and not dry_run:
|
if not url:
|
||||||
if not kwargs['caption']:
|
log.wtf('[Failed] Cannot extract video source.')
|
||||||
print('Skipping danmaku.')
|
# For legacy main()
|
||||||
|
headers = {}
|
||||||
|
if self.ua is not None:
|
||||||
|
headers['User-Agent'] = self.ua
|
||||||
|
if self.referer is not None:
|
||||||
|
headers['Referer'] = self.referer
|
||||||
|
|
||||||
|
download_url_ffmpeg(url, self.title, ext, output_dir=kwargs['output_dir'], merge=kwargs['merge'])
|
||||||
|
|
||||||
|
if 'caption' not in kwargs or not kwargs['caption']:
|
||||||
|
print('Skipping captions or danmaku.')
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
title = get_filename(title)
|
|
||||||
print('Downloading %s ...\n' % (title + '.cmt.json'))
|
|
||||||
cmt = get_srt_json(vid)
|
|
||||||
with open(os.path.join(output_dir, title + '.cmt.json'), 'w', encoding='utf-8') as x:
|
|
||||||
x.write(cmt)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def acfun_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
|
for lang in self.caption_tracks:
|
||||||
|
filename = '%s.%s.srt' % (get_filename(self.title), lang)
|
||||||
|
print('Saving %s ... ' % filename, end="", flush=True)
|
||||||
|
srt = self.caption_tracks[lang]
|
||||||
|
with open(os.path.join(kwargs['output_dir'], filename),
|
||||||
|
'w', encoding='utf-8') as x:
|
||||||
|
x.write(srt)
|
||||||
|
print('Done.')
|
||||||
|
|
||||||
|
if self.danmaku is not None and not dry_run:
|
||||||
|
filename = '{}.cmt.xml'.format(get_filename(self.title))
|
||||||
|
print('Downloading {} ...\n'.format(filename))
|
||||||
|
with open(os.path.join(kwargs['output_dir'], filename), 'w', encoding='utf8') as fp:
|
||||||
|
fp.write(self.danmaku)
|
||||||
|
|
||||||
|
if self.lyrics is not None and not dry_run:
|
||||||
|
filename = '{}.lrc'.format(get_filename(self.title))
|
||||||
|
print('Downloading {} ...\n'.format(filename))
|
||||||
|
with open(os.path.join(kwargs['output_dir'], filename), 'w', encoding='utf8') as fp:
|
||||||
|
fp.write(self.lyrics)
|
||||||
|
|
||||||
|
# For main_dev()
|
||||||
|
#download_urls(urls, self.title, self.streams[stream_id]['container'], self.streams[stream_id]['size'])
|
||||||
|
keep_obj = kwargs.get('keep_obj', False)
|
||||||
|
if not keep_obj:
|
||||||
|
self.__init__()
|
||||||
|
|
||||||
|
|
||||||
|
def acfun_download(self, url, output_dir='.', merge=True, info_only=False, **kwargs):
|
||||||
assert re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/(\D|bangumi)/\D\D(\d+)', url)
|
assert re.match(r'https?://[^\.]*\.*acfun\.[^\.]+/(\D|bangumi)/\D\D(\d+)', url)
|
||||||
|
|
||||||
def getM3u8UrlFromCurrentVideoInfo(currentVideoInfo):
|
def getM3u8UrlFromCurrentVideoInfo(currentVideoInfo):
|
||||||
@ -162,7 +207,7 @@ def acfun_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
|
|||||||
if not info_only:
|
if not info_only:
|
||||||
download_url_ffmpeg(m3u8_url, title, 'mp4', output_dir=output_dir, merge=merge)
|
download_url_ffmpeg(m3u8_url, title, 'mp4', output_dir=output_dir, merge=merge)
|
||||||
|
|
||||||
|
site = AcFun()
|
||||||
site_info = "AcFun.cn"
|
site_info = "AcFun.cn"
|
||||||
download = acfun_download
|
download = site.download_by_url
|
||||||
download_playlist = playlist_not_supported('acfun')
|
download_playlist = playlist_not_supported('acfun')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user