예제 #1
0
 def test_aspect_string_to_float(self):
     """Tests if string contains an aspect ratio"""
     self.assertAlmostEqual(bibencode_utils.aspect_string_to_float("4:3"),
                            1.333,
                            places=2)
     self.assertAlmostEqual(bibencode_utils.aspect_string_to_float("16:9"),
                            1.777,
                            places=2)
예제 #2
0
def determine_resolution_preserving_aspect(input_file, width=None,
                                           height=None, aspect=None):
    """ Determines the right resolution for a given width or height while
    preserving the aspect ratio.
    @param input_file: full path of the video
    @type input_file: string
    @param width: The proposed width for the new size.
    @type width: int
    @param height: The proposed height for the new size
    @type height: int
    @param aspect: Override aspect ratio determined from the input file
    @type aspect: float or "4:3" like string
    @return: An FFMPEG compatible size string '640x480'
    @rtype: string
    """

    def _make_even(number):
        """ Resolutions need to be even numbers for some video encoders.
        We simply increase the resulution by one pixel if it is not even.
        """
        if number % 2 != 0:
            return number+1
        else:
            return number

    if aspect:
        if type(aspect) == type(str()):
            aspect_ratio = aspect_string_to_float(aspect)
        elif type(aspect) == type(float()):
            aspect_ratio = aspect
        else:
            raise ValueError
    else:
        aspect_ratio_tuple = determine_aspect(input_file)
        if aspect_ratio_tuple[0] is None:
            aspect_ratio = float(aspect_ratio_tuple[1]) / float(aspect_ratio_tuple[2])
        else:
            aspect_ratio = aspect_string_to_float(aspect_ratio_tuple[0])

    nresolution = None
    if width and not height:
        ## The resolution hast to fit exactly the width
        nheight = int(width / aspect_ratio)
        nheight = _make_even(nheight)
        nresolution = "%dx%d" % (width, nheight)
    elif height and not width:
        ## The resolution hast to fit exactly the height
        nwidth = int(height * aspect_ratio)
        nwidth = _make_even(nwidth)
        nresolution = "%dx%d" % (nwidth, height)
    elif width and height:
        ## The resolution hast to be within both parameters, seen as a maximum
        nwidth = width
        nheight = height
        new_aspect_ratio = float(width) / float(height)
        if aspect_ratio > new_aspect_ratio:
            nheight = int(width / aspect_ratio)
        else:
            nwidth = int(height * aspect_ratio)
        nheight = _make_even(nheight)
        nwidth = _make_even(nwidth)
        nresolution = "%dx%d" % (nwidth, nheight)
    else:
        ## Return the original size in square pixels
        ## original height * aspect_ratio
        nwidth = aspect_ratio_tuple[2] * aspect_ratio
        nwidth = _make_even(nwidth)
        nresolution = "%dx%d" % (nwidth, aspect_ratio_tuple[2])
    return nresolution
예제 #3
0
 def test_aspect_string_to_float(self):
     """Tests if string contains an aspect ratio"""
     self.assertAlmostEqual(bibencode_utils.aspect_string_to_float("4:3"), 1.333, places=2)
     self.assertAlmostEqual(bibencode_utils.aspect_string_to_float("16:9"), 1.777, places=2)