Example #1
0
def AutoDetectSubtitle(pathvideofile, sub_list=None):
    """ will try to guess the subtitle for the given filepath video """
    log.debug("----------------")
    log.debug("AutoDetectSubtitle started with: %r, %r" %
              (pathvideofile, sub_list))

    if os.path.isfile(pathvideofile):
        videofolder = os.path.dirname(pathvideofile)
        filename1_noextension = without_extension(
            os.path.basename(pathvideofile))
    else:
        log.debug("AutoDetectSubtitle argument must be a complete video path")
        return ""

    # 1st METHOD , EXACT FILENAME THAN THE VIDEO WITHOUT EXTENSION
    log.debug("1st method starting...")
    for ext in subtitlefile.SUBTITLES_EXT:
        possiblefilenamesrt = filename1_noextension + "." + ext
        if sub_list:
            # print sub_list
            try:
                # check if subtitle is in our list
                if isinstance(sub_list, list):
                    sub_list.index(possiblefilenamesrt)
                    return possiblefilenamesrt
            except ValueError as e:
                log.debug(e)
            except AttributeError as e:
                log.debug(e)
        elif os.path.exists(os.path.join(videofolder, possiblefilenamesrt)):
            return os.path.join(videofolder, possiblefilenamesrt)

    # 2nd METHOD FIND THE AVI NAME MERGED INTO THE SUB NAME
    log.debug("2nd method starting...")
    cleaned_file = clear_string(filename1_noextension.lower())
    filesfound = []
    if sub_list:
        search_list = sub_list
    else:
        search_list = os.listdir(videofolder)
    for filename in search_list:
        for ext in subtitlefile.SUBTITLES_EXT:
            try:
                if filename.lower().endswith("." + ext):
                    filesfound.append(filename)  # To be used in the 4th method
                    cleaned_found = clear_string(
                        without_extension(filename.lower()))
                    if "srt" in subtitlefile.SUBTITLES_EXT and cleaned_found.find(cleaned_file) != -1:
                        if sub_list:
                            return filename
                        else:
                            return os.path.join(videofolder, filename)
                    elif cleaned_file.find(cleaned_found) != -1:
                        if sub_list:
                            return filename
                        else:
                            return os.path.join(videofolder, filename)
            except AttributeError as e:
                log.error(e)

    # 3rd METHOD SCORE EVERY SUBTITLE (this needs the sub_list) (by searching
    # the filename of the video in the content of the subtitle)
    if sub_list:
        log.debug("3rd method starting...")
        sub_scores = score_subtitles(pathvideofile, sub_list)
        best_sub = None
        best_sub_score = -1
        for sub in sub_scores:
            if sub_scores[sub] > best_sub_score:
                best_sub = sub
                best_sub_score = sub_scores[sub]
        if best_sub is not None:
            return best_sub
    else:
        log.debug("3rd was skipped")

    # 4th METHOD WE TAKE THE SUB IF THERE IS ONLY ONE
    log.debug("4th method starting...")
    if len(filesfound) == 1:
        if sub_list:
            return filesfound[0]
        else:
            return os.path.join(videofolder, filesfound[0])

    return ""
Example #2
0
            #print sub_list
            try:
                # check if subtitle is in our list
                if isinstance(sub_list, list):
                    sub_list.index(possiblefilenamesrt)
                    return possiblefilenamesrt
            except ValueError, e:
                log.debug(e)
            except AttributeError, e:
                log.debug(e)
        elif os.path.exists(os.path.join(videofolder, possiblefilenamesrt)):
            return os.path.join(videofolder, possiblefilenamesrt)
 
    #2nd METHOD FIND THE AVI NAME MERGED INTO THE SUB NAME
    log.debug("2nd method starting...")
    cleaned_file = clear_string(filename1_noextension.lower())
    filesfound = []
    if sub_list:
        search_list = sub_list
    else:
        search_list = os.listdir(videofolder)
    for filename in search_list:
        for ext in subtitlefile.SUBTITLES_EXT:
            try:
                if filename.lower().endswith("."+ext):
                    filesfound.append(filename) #To be used in the 4th method
                    cleaned_found = clear_string(without_extension(filename.lower()))
                    if "srt" in subtitlefile.SUBTITLES_EXT and cleaned_found.find(cleaned_file) != -1:
                        if sub_list:
                            return filename
                        else: