ytdl-org / youtube-dl

Command-line program to download videos from YouTube.com and other video sites
http://ytdl-org.github.io/youtube-dl/
The Unlicense
131.73k stars 9.97k forks source link

Get cid from bilibili #20555

Open awei78 opened 5 years ago

awei78 commented 5 years ago

Please follow the guide below


Make sure you are using the latest version: run youtube-dl --version and ensure your version is 2019.04.01. If it's not, read this FAQ entry and update. Issues with outdated version will be rejected.

In the file bilibili.py, line 116, the code of get "cid" is: cid = self._search_regex( r'\bcid(?:["\']:|=)(\d+)', webpage, 'cid', default=None ) or compat_parse_qs(self._search_regex( [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)', r'EmbedPlayer\([^)]+,\s*\\"([^"]+)\\"\)', r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'], webpage, 'player parameters'))['cid'][0]

If there is only one video on the page, it works fine. However, if the video is part of the list, such as url: https://www.bilibili.com/video/av4050443/?p=86 It's cid is: 6534658. part of it's data is: {"cid":6534658,"page":86,"from":"vupload"...}

Use Regex pattern r'\bcid(?:["\']:|=)(\d+)', it only get the cid of the first video in the list, it is wrong. Then I found, if there is only one video on the page, it video data like this: {"cid":45915709,"page":1,"from":"vupload"...}, That is, it has a page number.

So, I think, can pattern be changed? add a page number limit, suck as:

page_number = xxx  #get from url or set it = 1
r'\bcid(?:["\']:|=)(\d+),page":%d' % page_number

In this way, the correct cid can be achieved, and the whole playlist can be downloaded.

I am looking forward to it.

awei78 commented 5 years ago

@dstftw Can you modify it? thanks

        if 'anime/' not in url:
            page_number = int(self._search_regex(
                r'/\?p=(\d+)$', url, 'page_number', '0'))
            pattern = r'\bcid(?:["\']:|=)(\d+)' if page_number == 0 else r'\bcid(?:["\']:|=)(\d+),"page":%d' % page_number
            cid = self._search_regex(
                pattern, webpage, 'cid',
                default=None
            ) or compat_parse_qs(self._search_regex(
                [r'EmbedPlayer\([^)]+,\s*"([^"]+)"\)',
                 r'EmbedPlayer\([^)]+,\s*\\"([^"]+)\\"\)',
                 r'<iframe[^>]+src="https://secure\.bilibili\.com/secure,([^"]+)"'],
                webpage, 'player parameters'))['cid'][0]
        else:
omega13a commented 5 years ago

@awei78 , I think it would be quicker if you go and edit the file in the code repository yourself which would automatically create a pull request that the maintainers can approve. We really need this bug fix applied. Also, a little note about this bug fix, if you want to download more then one page of a multi-page video (IE https://www.bilibili.com/video/av4050443/?p=85 and https://www.bilibili.com/video/av4050443/?p=86), you need to do each separately and use the -o parameter to give them different titles so youtube-dl doesn't think you are trying to download the same video again.