File size: 735 Bytes
6073e55
23fdbc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2025 Ye Liu. Licensed under the BSD-3-Clause License.

import pysrt
from decord import VideoReader


def time_to_seconds(t):
    return (t.hour * 60 + t.minute) * 60 + t.second + t.microsecond / 1000000


def load_subtitle(path):
    subs = pysrt.open(path)

    parsed = []
    for sub in subs:
        s = time_to_seconds(sub.start.to_time())
        e = time_to_seconds(sub.end.to_time())
        parsed.append((s, e, sub.text))

    return parsed


def get_duration(path, num_threads=1):
    # sometimes the video is loaded as a list of frames
    if isinstance(path, list):
        return len(path)

    vr = VideoReader(path, num_threads=num_threads)
    duration = len(vr) / vr.get_avg_fps()
    return duration