Spaces:
Running
on
Zero
Running
on
Zero
Delete single_video.py
Browse files- single_video.py +0 -70
single_video.py
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
from pathlib import Path
|
2 |
-
|
3 |
-
from lutils import openf, writef
|
4 |
-
|
5 |
-
from src.data.chapters import sec_to_hms
|
6 |
-
from tools.extract.asr import ASRProcessor
|
7 |
-
|
8 |
-
|
9 |
-
class SingleVideo:
|
10 |
-
"""
|
11 |
-
A simplified implementation of the src.data.chapters.Chapters interface for single video inference.
|
12 |
-
|
13 |
-
This class mimics the behavior of the ChaptersASR class but is designed to work with
|
14 |
-
a single video file rather than a dataset. It provides the necessary methods
|
15 |
-
required by the PromptASR class for generating chapter timestamps and titles.
|
16 |
-
|
17 |
-
Note: This class is intended for inference only and should not be used for
|
18 |
-
training or evaluation purposes.
|
19 |
-
"""
|
20 |
-
|
21 |
-
def __init__(self, video_path: Path):
|
22 |
-
self.video_path = video_path
|
23 |
-
self.video_ids = [video_path.stem]
|
24 |
-
assert video_path.exists(), f"Video file {video_path} not found"
|
25 |
-
self.asr, self.duration = get_asr(video_path, overwrite=True)
|
26 |
-
|
27 |
-
def __len__(self):
|
28 |
-
return len(self.video_ids)
|
29 |
-
|
30 |
-
def __iter__(self):
|
31 |
-
return iter(self.video_ids)
|
32 |
-
|
33 |
-
def __contains__(self, vid_id):
|
34 |
-
return vid_id in self.video_ids
|
35 |
-
|
36 |
-
def get_duration(self, vid_id, hms=False):
|
37 |
-
assert vid_id == self.video_ids[0], f"Invalid video ID: {vid_id}"
|
38 |
-
if hms:
|
39 |
-
return sec_to_hms(self.duration)
|
40 |
-
return self.duration
|
41 |
-
|
42 |
-
def get_asr(self, vid_id):
|
43 |
-
assert vid_id == self.video_ids[0], f"Invalid video ID: {vid_id}"
|
44 |
-
return self.asr
|
45 |
-
|
46 |
-
|
47 |
-
def get_asr(video_path: Path, overwrite=False):
|
48 |
-
output_dir = Path(f"outputs/inference/{video_path.stem}")
|
49 |
-
asr_output = output_dir / "asr.txt"
|
50 |
-
duration_output = output_dir / "duration.txt"
|
51 |
-
if asr_output.exists() and duration_output.exists() and not overwrite:
|
52 |
-
asr = openf(asr_output)
|
53 |
-
asr = "\n".join(asr) + "\n"
|
54 |
-
|
55 |
-
duration = openf(duration_output)
|
56 |
-
assert isinstance(duration, list) and len(duration) == 1, (
|
57 |
-
f"Duration is not a list of length 1: {duration}"
|
58 |
-
)
|
59 |
-
duration = float(duration[0])
|
60 |
-
assert duration > 0, f"Duration is not positive: {duration}"
|
61 |
-
return asr, duration
|
62 |
-
|
63 |
-
print(f"\n=== 🎙️ Processing ASR for {video_path} ===")
|
64 |
-
asr_processor = ASRProcessor()
|
65 |
-
asr, duration = asr_processor.get_asr(video_path)
|
66 |
-
print(f"=== ✅ ASR processing complete for {video_path} ===\n")
|
67 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
68 |
-
writef(asr_output, asr)
|
69 |
-
writef(duration_output, str(duration))
|
70 |
-
return asr, duration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|