Ejemplo n.º 1
0
 def test_validate(self):
     vs = VideoSequence(1,10)
     s1 = Subregion(0, 0.1)
     vs.add_subregion(s1)
     self.assertTrue(vs.validate(Subregion(0.2, 0.3)))
     self.assertTrue(vs.validate(Subregion(0.4, 0.8)))
     self.assertTrue(vs.validate(Subregion(0.9, 1.0)))
Ejemplo n.º 2
0
 def test_add_subregion(self):
     vs = VideoSequence(1,4)
     self.assertEqual(len(vs.subregions), 0)
     vs.add_subregion(Subregion(0,0.1))
     self.assertEqual(len(vs.subregions), 1)
     vs.add_subregion(Subregion(0.1,0.2))
     self.assertEqual(len(vs.subregions), 2)
     vs.add_subregion(Subregion(0.2,0.3))
     self.assertEqual(len(vs.subregions), 3)
     vs.add_subregion(Subregion(0.9,1))
     self.assertEqual(len(vs.subregions), 4)
Ejemplo n.º 3
0
 def test_validate_fails_intersects(self):
     vs = VideoSequence(1,10)
     s1 = Subregion(0.1,0.2)
     s2 = Subregion(0.15,0.25)
     vs.add_subregion(s1)
     with self.assertRaises(RuntimeError):
         vs.add_subregion(s2)
     vs = VideoSequence(5,1)
     s1 = Subregion(1,1)
     s2 = Subregion(0,5)
     with self.assertRaises(RuntimeError):
         vs.add_subregion(s1)
         vs.add_subregion(s2)
     with self.assertRaises(RuntimeError):
         vs.add_subregion(s2)
         vs.add_subregion(s1)
Ejemplo n.º 4
0
 def x_intersects(self, x, s1_a, s1_b, s2_a, s2_b):
     s1 = Subregion(0, 0)
     s2 = Subregion(0, 0)
     if x == 'time':
         setattr(s1, 'ta', s1_a)
         setattr(s1, 'tb', s1_b)
         setattr(s2, 'ta', s2_a)
         setattr(s2, 'tb', s2_b)
     elif x == 'fr':
         setattr(s1, 'fa', s1_a)
         setattr(s1, 'fb', s1_b)
         setattr(s2, 'fa', s2_a)
         setattr(s2, 'fb', s2_b)
     else:
         return
     intersect_method = getattr(s2, '{}_intersects'.format(x))
     return (intersect_method(s1))
Ejemplo n.º 5
0
 def test_add_subregion_ordering(self):
     vs = VideoSequence(1,10)
     s1 = Subregion(0, 0.1)
     s2 = Subregion(0.3, 0.4)
     s3 = Subregion(0.4, 0.5)
     s4 = Subregion(0.5, 0.6)
     s5 = Subregion(0.9, 1)
     vs.add_subregion(s5)
     vs.add_subregion(s1)
     vs.add_subregion(s4)
     vs.add_subregion(s2)
     vs.add_subregion(s3)
     self.assertEqual(vs.subregions[0], s1)
     self.assertEqual(vs.subregions[1], s2)
     self.assertEqual(vs.subregions[2], s3)
     self.assertEqual(vs.subregions[3], s4)
     self.assertEqual(vs.subregions[4], s5)
Ejemplo n.º 6
0
 def test_validate_fails_out_of_bounds(self):
     vs = VideoSequence(1,1)
     s = Subregion(0,1.1)
     with self.assertRaises(RuntimeError):
         vs.add_subregion(s)
     s = Subregion(0,2)
     s.fa = 1
     s.fb = 2
     with self.assertRaises(RuntimeError):
         vs.add_subregion(s)
Ejemplo n.º 7
0
def sequence_from_input_str(s, src_duration, src_frs):
    seq = VideoSequence(src_duration, src_frs)
    if not s:
        seq.subregions[0].skip = False
        return seq
    src_duration = str(datetime.timedelta(seconds=src_duration / 1000.0))
    partition = list(src_duration.partition('.'))
    partition[-1] = partition[-1][:3]  # keep secs.xxx...
    src_duration = ''.join(partition)
    s = re.sub(sr_ful_pattern,
               'a=0,b={}'.format(src_duration), re.sub(sr_end_pattern,
                                                       src_duration, s))
    subs = re.split(':a', s)
    new_subs = []
    if len(subs) > 1:
        for sub in subs:
            if not sub.startswith('a'):
                new_subs.append('a'+sub)
            else:
                new_subs.append(sub)
        subs = new_subs
    for sub in subs:
        match = re.match(sr_pattern, sub)
        if match:
            substr = str(sub)
            try:
                sub = Subregion(
                           time_str_to_milliseconds(match.groupdict()['tm_a']),
                           time_str_to_milliseconds(match.groupdict()['tm_b']))
            except AttributeError as e:
                raise AttributeError("Bad subregion: {} ({})".format(substr, e))
            target = match.groupdict()['target']
            val = match.groupdict()['val']
            if target == 'fps':
                val = rate_from_input_str(val, -1)
            elif target == 'dur':
                val = float(val)*1000.0
            elif target == 'spd':
                val = float(val)
            setattr(sub, 'target_'+target, val)
            try:
                seq.add_subregion(sub)
            except ValueError as e:
                raise ValueError("Bad subregion: {} ({})".format(substr, e))
        else:
            raise ValueError('Unknown subregion syntax: {}'.format(sub))
    return seq
Ejemplo n.º 8
0
def sequence_from_input_str(s, src_dur, src_nfrs):
    seq = VideoSequence(src_dur, src_nfrs)
    if not s:
        return seq
    src_dur = str(datetime.timedelta(seconds=src_dur / 1000.0))
    partition = list(src_dur.partition('.'))
    partition[-1] = partition[-1][:3]  # keep secs.xxx...
    src_dur = ''.join(partition)
    s = re.sub(sr_ful_pattern, 'a=0,b={}'.format(src_dur),
               re.sub(sr_end_pattern, src_dur, s))
    subs = re.split(':a', s)
    new_subs = []
    if len(subs) > 1:
        for sub in subs:
            if not sub.startswith('a'):
                new_subs.append('a' + sub)
            else:
                new_subs.append(sub)
        subs = new_subs
    for sub in subs:
        match = re.match(sr_pattern, sub)
        if match:
            sub = Subregion(
                time_str_to_milliseconds(match.groupdict()['tm_a']),
                time_str_to_milliseconds(match.groupdict()['tm_b']))
            target = match.groupdict()['target']
            val = match.groupdict()['val']
            if target == 'fps':
                val = rate_from_input_str(val, -1)
            elif target == 'dur':
                val = float(val) * 1000.0
            elif target == 'spd':
                val = float(val)
            setattr(sub, 'target_' + target, val)
            seq.add_subregion(sub)
        else:
            raise RuntimeError
    return seq
Ejemplo n.º 9
0
 def test_add_subregion(self):
     cnt_skip_subs = lambda x: \
         len([s for s in x.subregions if s.skip])
     cnt_usr_subs = lambda x: \
         len(x.subregions) - cnt_skip_subs(x)
     vs = VideoSequence(1, 1)
     self.assertEqual(cnt_usr_subs(vs), 0)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs.add_subregion(Subregion(0, 0.1))
     self.assertEqual(cnt_usr_subs(vs), 1)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs = VideoSequence(1, 1)
     vs.add_subregion(Subregion(0.9, 1))
     self.assertEqual(cnt_usr_subs(vs), 1)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs = VideoSequence(1, 1)
     vs.add_subregion(Subregion(0, 1))
     self.assertEqual(cnt_usr_subs(vs), 1)
     self.assertEqual(cnt_skip_subs(vs), 0)
     vs = VideoSequence(1, 2)
     vs.add_subregion(Subregion(0, 0.5))
     vs.add_subregion(Subregion(0.5, 0.6))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs = VideoSequence(1, 2)
     vs.add_subregion(Subregion(0.5, 0.6))
     vs.add_subregion(Subregion(0, 0.1))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs = VideoSequence(1, 3)
     vs.add_subregion(Subregion(0, 0.1))
     self.assertEqual(cnt_usr_subs(vs), 1)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs.add_subregion(Subregion(0.9, 1))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs.add_subregion(Subregion(0.66, 0.66))
     self.assertEqual(cnt_usr_subs(vs), 3)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs = VideoSequence(1, 3)
     vs.add_subregion(Subregion(0.66, 0.66))
     self.assertEqual(cnt_usr_subs(vs), 1)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs.add_subregion(Subregion(0, 0.1))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs.add_subregion(Subregion(0.9, 1))
     self.assertEqual(cnt_usr_subs(vs), 3)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs = VideoSequence(1, 4)
     vs.add_subregion(Subregion(0.25, 0.5))
     vs.add_subregion(Subregion(0.5, 0.75))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs.add_subregion(Subregion(0, 0.25))
     self.assertEqual(cnt_usr_subs(vs), 3)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs.add_subregion(Subregion(0.75, 1))
     self.assertEqual(cnt_usr_subs(vs), 4)
     self.assertEqual(cnt_skip_subs(vs), 0)
     vs = VideoSequence(1, 5)
     vs.add_subregion(Subregion(0.2, 0.4))
     vs.add_subregion(Subregion(0.6, 0.8))
     self.assertEqual(cnt_usr_subs(vs), 2)
     self.assertEqual(cnt_skip_subs(vs), 3)
     vs.add_subregion(Subregion(0.4, 0.6))
     self.assertEqual(cnt_usr_subs(vs), 3)
     self.assertEqual(cnt_skip_subs(vs), 2)
     vs.add_subregion(Subregion(0.8, 1))
     self.assertEqual(cnt_usr_subs(vs), 4)
     self.assertEqual(cnt_skip_subs(vs), 1)
     vs.add_subregion(Subregion(0, 0.2))
     self.assertEqual(cnt_usr_subs(vs), 5)
     self.assertEqual(cnt_skip_subs(vs), 0)
Ejemplo n.º 10
0
 def test_init(self):
     self.assertIsInstance(Subregion(0, 1), Subregion)
     self.assertIsInstance(Subregion(1, 1), Subregion)
     self.assertIsInstance(Subregion(1, 2), Subregion)