'Select the requested subtitles and their format'

def process_subtitles(self, video_id, normal_subtitles, automatic_captions):

    available_subs = {}
    if (normal_subtitles and self.params.get(u'writesubtitles')):
        available_subs.update(normal_subtitles)
    if (automatic_captions and self.params.get(u'writeautomaticsub')):
        for (lang, cap_info) in automatic_captions.items():
            if (lang not in available_subs):
                available_subs[lang] = cap_info
    if (((not self.params.get(u'writesubtitles')) and (not self.params.get(u'writeautomaticsub'))) or (not available_subs)):
        return None
    if self.params.get(u'allsubtitles', False):
        requested_langs = available_subs.keys()
    elif self.params.get(u'subtitleslangs', False):
        requested_langs = self.params.get(u'subtitleslangs')
    elif (u'en' in available_subs):
        requested_langs = [u'en']
    else:
        requested_langs = [list(available_subs.keys())[0]]
    formats_query = self.params.get(u'subtitlesformat', u'best')
    formats_preference = (formats_query.split(u'/') if formats_query else [])
    subs = {}
    for lang in requested_langs:
        formats = available_subs.get(lang)
        if (formats is None):
            self.report_warning((u'%s    subtitles    not    available    for    %s' % (lang, video_id)))
            continue
        for ext in formats_preference:
            if (ext == u'best'):
                f = formats[(-1)]
                break
            matches = list(filter((lambda f: (f[u'ext'] == ext)), formats))
            if matches:
                f = matches[(-1)]
                break
        else:
            f = formats[(-1)]
            self.report_warning((u'No    subtitle    format    found    matching    "%s"    for    language    %s,    using    %s' % (formats_query, lang, f[u'ext'])))
        subs[lang] = f
    return subs
