code: def search_external_subtitles(path, directory=None):
      
      # split path
      dirpath, filename = os.path.split(path)
      dirpath = dirpath or '.'
      fileroot, fileext = os.path.splitext(filename)
  
      # search for subtitles
      subtitles = {}
      for p in os.listdir(directory or dirpath):
          # keep only valid subtitle filenames
          if not p.startswith(fileroot) or not p.endswith(SUBTITLE_EXTENSIONS):
              continue
  
          # extract the potential language code
          language = Language('und')
          language_code = p[len(fileroot):-len(os.path.splitext(p)[1])].replace(fileext, '').replace('_', '-')[1:]
          if language_code:
              try:
                  language = Language.fromietf(language_code)
              except (ValueError, LanguageReverseError):
                  logger.error('Cannot parse language code %r', language_code)
  
          subtitles[p] = language
  
      logger.debug('Found subtitles %r', subtitles)
  
      return subtitles
docs: Search for external subtitles from a video `path` and their associated language.
  
      Unless `directory` is provided, search will be made in the same directory as the video file.
  
      :param str path: path to the video.
      :param str directory: directory to search for subtitles.
      :return: found subtitles with their languages.
      :rtype: dict