File size: 2,463 Bytes
62a013b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pytest
from utils.vtt_parser import parse_vtt_offsets

def test_parse_simple_vtt():
    """Test parsing a simple VTT file with one timestamp and coordinates"""
    vtt_content = """WEBVTT

00:00:05.000 --> 00:00:10.000
xywh=100,200,300,400
"""
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 1
    left, top, right, bottom, time = result[0]
    assert left == 100
    assert top == 200
    assert right == 300
    assert bottom == 400
    assert time == 5.0

def test_parse_multiple_entries():
    """Test parsing multiple timestamps and coordinates"""
    vtt_content = """WEBVTT

00:00:05.000 --> 00:00:10.000
xywh=100,200,300,400

00:01:30.500 --> 00:01:35.000
xywh=150,250,350,450
"""
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 2
    
    # First entry
    left, top, right, bottom, time = result[0]
    assert (left, top, right, bottom) == (100, 200, 300, 400)
    assert time == 5.0
    
    # Second entry
    left, top, right, bottom, time = result[1]
    assert (left, top, right, bottom) == (150, 250, 350, 450)
    assert time == 90.5  # 1 minute 30.5 seconds

def test_parse_empty_vtt():
    """Test parsing an empty VTT file"""
    vtt_content = "WEBVTT\n"
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 0

def test_parse_invalid_format():
    """Test parsing VTT with invalid format should not yield results"""
    vtt_content = """WEBVTT

00:00:05.000 --> 00:00:10.000
invalid_line
"""
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 0

def test_parse_hour_timestamp():
    """Test parsing timestamp with hours"""
    vtt_content = """WEBVTT

01:30:05.000 --> 01:30:10.000
xywh=100,200,300,400
"""
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 1
    left, top, right, bottom, time = result[0]
    assert time == 5405.0  # 1 hour + 30 minutes + 5 seconds

def test_parse_missing_coordinates():
    """Test that entries without coordinates are skipped"""
    vtt_content = """WEBVTT

00:00:05.000 --> 00:00:10.000
Some text content

00:00:10.000 --> 00:00:15.000
xywh=100,200,300,400
"""
    result = list(parse_vtt_offsets(vtt_content.encode('utf-8')))
    assert len(result) == 1
    left, top, right, bottom, time = result[0]
    assert time == 10.0
    assert (left, top, right, bottom) == (100, 200, 300, 400)