Skip to content

Instantly share code, notes, and snippets.

@RogerRordo
Last active April 5, 2021 06:55
Show Gist options
  • Select an option

  • Save RogerRordo/937d37a6bd19c20f1ce094cfe4fb48ea to your computer and use it in GitHub Desktop.

Select an option

Save RogerRordo/937d37a6bd19c20f1ce094cfe4fb48ea to your computer and use it in GitHub Desktop.

Revisions

  1. RogerRordo renamed this gist Dec 11, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. RogerRordo revised this gist Dec 11, 2020. 2 changed files with 2 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    替换youtube-dl/extractor/youtube.py的_get_automatic_captions函数
    2 changes: 2 additions & 0 deletions python
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #替换youtube-dl/extractor/youtube.py的_get_automatic_captions函数

    def _get_automatic_captions(self, video_id, webpage):
    """We need the webpage for getting the captions url, pass it as an
    argument to speed up the process."""
  3. RogerRordo created this gist Dec 11, 2020.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    替换youtube-dl/extractor/youtube.py的_get_automatic_captions函数
    63 changes: 63 additions & 0 deletions python
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    def _get_automatic_captions(self, video_id, webpage):
    """We need the webpage for getting the captions url, pass it as an
    argument to speed up the process."""
    self.to_screen('%s: Looking for automatic captions' % video_id)

    err_msg = 'Couldn\'t find automatic captions for %s' % video_id

    pattern = r'var\s*ytInitialPlayerResponse\s*=\s*({.+?});'
    response = self._search_regex(pattern,
    webpage,
    'yt initial player response',
    default=None)
    if response:
    yt_initial_player_response = self._parse_json(
    uppercase_escape(response), video_id, fatal=False)
    if not yt_initial_player_response:
    self._downloader.report_warning(err_msg)
    return {}
    else:
    self._downloader.report_warning(err_msg)
    return {}

    try:

    def make_captions(sub_url, sub_langs):
    parsed_sub_url = compat_urllib_parse_urlparse(sub_url)
    caption_qs = compat_parse_qs(parsed_sub_url.query)
    captions = {}
    for sub_lang in sub_langs:
    sub_formats = []
    for ext in self._SUBTITLE_FORMATS:
    caption_qs.update({
    'tlang': [sub_lang],
    'fmt': [ext],
    })
    sub_url = compat_urlparse.urlunparse(
    parsed_sub_url._replace(
    query=compat_urllib_parse_urlencode(
    caption_qs, True)))
    sub_formats.append({
    'url': sub_url,
    'ext': ext,
    })
    captions[sub_lang] = sub_formats
    return captions

    # New captions format as of 22.06.2017
    if yt_initial_player_response:
    renderer = yt_initial_player_response['captions'][
    'playerCaptionsTracklistRenderer']
    base_url = renderer['captionTracks'][0]['baseUrl']
    sub_lang_list = []
    for lang in renderer['translationLanguages']:
    lang_code = lang.get('languageCode')
    if lang_code:
    sub_lang_list.append(lang_code)
    return make_captions(base_url, sub_lang_list)

    # An extractor error can be raise by the download process if there are
    # no automatic captions but there are subtitles
    except (KeyError, IndexError, ExtractorError):
    self._downloader.report_warning(err_msg)
    return {}