diff options
Diffstat (limited to 'youtube_dl/utils.py')
| -rw-r--r-- | youtube_dl/utils.py | 213 | 
1 files changed, 209 insertions, 4 deletions
| diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 52d198fa3..a2746b2d1 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -1841,7 +1841,10 @@ def srt_subtitles_timecode(seconds):  def dfxp2srt(dfxp_data): -    _x = functools.partial(xpath_with_ns, ns_map={'ttml': 'http://www.w3.org/ns/ttml'}) +    _x = functools.partial(xpath_with_ns, ns_map={ +        'ttml': 'http://www.w3.org/ns/ttml', +        'ttaf1': 'http://www.w3.org/2006/10/ttaf1', +    })      def parse_node(node):          str_or_empty = functools.partial(str_or_none, default='') @@ -1849,9 +1852,9 @@ def dfxp2srt(dfxp_data):          out = str_or_empty(node.text)          for child in node: -            if child.tag in (_x('ttml:br'), 'br'): +            if child.tag in (_x('ttml:br'), _x('ttaf1:br'), 'br'):                  out += '\n' + str_or_empty(child.tail) -            elif child.tag in (_x('ttml:span'), 'span'): +            elif child.tag in (_x('ttml:span'), _x('ttaf1:span'), 'span'):                  out += str_or_empty(parse_node(child))              else:                  out += str_or_empty(xml.etree.ElementTree.tostring(child)) @@ -1860,7 +1863,7 @@ def dfxp2srt(dfxp_data):      dfxp = xml.etree.ElementTree.fromstring(dfxp_data.encode('utf-8'))      out = [] -    paras = dfxp.findall(_x('.//ttml:p')) or dfxp.findall('.//p') +    paras = dfxp.findall(_x('.//ttml:p')) or dfxp.findall(_x('.//ttaf1:p')) or dfxp.findall('.//p')      if not paras:          raise ValueError('Invalid dfxp/TTML subtitle') @@ -1879,6 +1882,208 @@ def dfxp2srt(dfxp_data):      return ''.join(out) +class ISO639Utils(object): +    # See http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt +    _lang_map = { +        'aa': 'aar', +        'ab': 'abk', +        'ae': 'ave', +        'af': 'afr', +        'ak': 'aka', +        'am': 'amh', +        'an': 'arg', +        'ar': 'ara', +        'as': 'asm', +        'av': 'ava', +        'ay': 'aym', +        'az': 'aze', +        'ba': 'bak', +        'be': 'bel', +        'bg': 'bul', +        'bh': 'bih', +        'bi': 'bis', +        'bm': 'bam', +        'bn': 'ben', +        'bo': 'bod', +        'br': 'bre', +        'bs': 'bos', +        'ca': 'cat', +        'ce': 'che', +        'ch': 'cha', +        'co': 'cos', +        'cr': 'cre', +        'cs': 'ces', +        'cu': 'chu', +        'cv': 'chv', +        'cy': 'cym', +        'da': 'dan', +        'de': 'deu', +        'dv': 'div', +        'dz': 'dzo', +        'ee': 'ewe', +        'el': 'ell', +        'en': 'eng', +        'eo': 'epo', +        'es': 'spa', +        'et': 'est', +        'eu': 'eus', +        'fa': 'fas', +        'ff': 'ful', +        'fi': 'fin', +        'fj': 'fij', +        'fo': 'fao', +        'fr': 'fra', +        'fy': 'fry', +        'ga': 'gle', +        'gd': 'gla', +        'gl': 'glg', +        'gn': 'grn', +        'gu': 'guj', +        'gv': 'glv', +        'ha': 'hau', +        'he': 'heb', +        'hi': 'hin', +        'ho': 'hmo', +        'hr': 'hrv', +        'ht': 'hat', +        'hu': 'hun', +        'hy': 'hye', +        'hz': 'her', +        'ia': 'ina', +        'id': 'ind', +        'ie': 'ile', +        'ig': 'ibo', +        'ii': 'iii', +        'ik': 'ipk', +        'io': 'ido', +        'is': 'isl', +        'it': 'ita', +        'iu': 'iku', +        'ja': 'jpn', +        'jv': 'jav', +        'ka': 'kat', +        'kg': 'kon', +        'ki': 'kik', +        'kj': 'kua', +        'kk': 'kaz', +        'kl': 'kal', +        'km': 'khm', +        'kn': 'kan', +        'ko': 'kor', +        'kr': 'kau', +        'ks': 'kas', +        'ku': 'kur', +        'kv': 'kom', +        'kw': 'cor', +        'ky': 'kir', +        'la': 'lat', +        'lb': 'ltz', +        'lg': 'lug', +        'li': 'lim', +        'ln': 'lin', +        'lo': 'lao', +        'lt': 'lit', +        'lu': 'lub', +        'lv': 'lav', +        'mg': 'mlg', +        'mh': 'mah', +        'mi': 'mri', +        'mk': 'mkd', +        'ml': 'mal', +        'mn': 'mon', +        'mr': 'mar', +        'ms': 'msa', +        'mt': 'mlt', +        'my': 'mya', +        'na': 'nau', +        'nb': 'nob', +        'nd': 'nde', +        'ne': 'nep', +        'ng': 'ndo', +        'nl': 'nld', +        'nn': 'nno', +        'no': 'nor', +        'nr': 'nbl', +        'nv': 'nav', +        'ny': 'nya', +        'oc': 'oci', +        'oj': 'oji', +        'om': 'orm', +        'or': 'ori', +        'os': 'oss', +        'pa': 'pan', +        'pi': 'pli', +        'pl': 'pol', +        'ps': 'pus', +        'pt': 'por', +        'qu': 'que', +        'rm': 'roh', +        'rn': 'run', +        'ro': 'ron', +        'ru': 'rus', +        'rw': 'kin', +        'sa': 'san', +        'sc': 'srd', +        'sd': 'snd', +        'se': 'sme', +        'sg': 'sag', +        'si': 'sin', +        'sk': 'slk', +        'sl': 'slv', +        'sm': 'smo', +        'sn': 'sna', +        'so': 'som', +        'sq': 'sqi', +        'sr': 'srp', +        'ss': 'ssw', +        'st': 'sot', +        'su': 'sun', +        'sv': 'swe', +        'sw': 'swa', +        'ta': 'tam', +        'te': 'tel', +        'tg': 'tgk', +        'th': 'tha', +        'ti': 'tir', +        'tk': 'tuk', +        'tl': 'tgl', +        'tn': 'tsn', +        'to': 'ton', +        'tr': 'tur', +        'ts': 'tso', +        'tt': 'tat', +        'tw': 'twi', +        'ty': 'tah', +        'ug': 'uig', +        'uk': 'ukr', +        'ur': 'urd', +        'uz': 'uzb', +        've': 'ven', +        'vi': 'vie', +        'vo': 'vol', +        'wa': 'wln', +        'wo': 'wol', +        'xh': 'xho', +        'yi': 'yid', +        'yo': 'yor', +        'za': 'zha', +        'zh': 'zho', +        'zu': 'zul', +    } + +    @classmethod +    def short2long(cls, code): +        """Convert language code from ISO 639-1 to ISO 639-2/T""" +        return cls._lang_map.get(code[:2]) + +    @classmethod +    def long2short(cls, code): +        """Convert language code from ISO 639-2/T to ISO 639-1""" +        for short_name, long_name in cls._lang_map.items(): +            if long_name == code: +                return short_name + +  class PerRequestProxyHandler(compat_urllib_request.ProxyHandler):      def __init__(self, proxies=None):          # Set default handlers | 
