Spaces:
Running
Running
Commit
•
bb077a1
1
Parent(s):
8b4615f
Cleaned
Browse files- video_to_text.py +99 -86
video_to_text.py
CHANGED
@@ -1,86 +1,99 @@
|
|
1 |
-
import argparse
|
2 |
-
from moviepy.editor import VideoFileClip
|
3 |
-
import whisper
|
4 |
-
import os
|
5 |
-
import re
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
video_clip
|
13 |
-
video_clip.
|
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 |
-
return
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
import whisper
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Extracts audio from video
|
8 |
+
def extract_audio(video_path, audio_dir='./audio'):
|
9 |
+
os.makedirs(audio_dir, exist_ok=True)
|
10 |
+
base_filename = os.path.splitext(os.path.basename(video_path))[0]
|
11 |
+
audio_filename = os.path.join(audio_dir, base_filename + '.wav')
|
12 |
+
video_clip = VideoFileClip(video_path)
|
13 |
+
video_clip.audio.write_audiofile(audio_filename)
|
14 |
+
video_clip.close()
|
15 |
+
return audio_filename
|
16 |
+
|
17 |
+
# Transcribe audio .wav file
|
18 |
+
def transcribe_audio(audio_path, model_type='base', transcribed_dir='./transcribed'):
|
19 |
+
model = whisper.load_model(model_type)
|
20 |
+
result = model.transcribe(audio_path)
|
21 |
+
|
22 |
+
os.makedirs(transcribed_dir, exist_ok=True)
|
23 |
+
base_filename = os.path.splitext(os.path.basename(audio_path))[0]
|
24 |
+
transcribed_filename = os.path.join(transcribed_dir, base_filename + '.txt')
|
25 |
+
|
26 |
+
with open(transcribed_filename, 'w') as file:
|
27 |
+
for segment in result['segments']:
|
28 |
+
start = segment['start']
|
29 |
+
end = segment['end']
|
30 |
+
text = segment['text']
|
31 |
+
file.write(f"[{start:.2f}-{end:.2f}] {text}\n")
|
32 |
+
|
33 |
+
return transcribed_filename, result['text']
|
34 |
+
|
35 |
+
# Merge lines in file that are part of the same sentence
|
36 |
+
def merge_lines(file_path):
|
37 |
+
timestamp_pattern = re.compile(r'\[(\d+\.\d+)-(\d+\.\d+)\]')
|
38 |
+
|
39 |
+
with open(file_path, 'r') as file:
|
40 |
+
lines = file.readlines()
|
41 |
+
|
42 |
+
merged_lines = []
|
43 |
+
i = 0
|
44 |
+
|
45 |
+
while i < len(lines):
|
46 |
+
line = lines[i].strip()
|
47 |
+
match = timestamp_pattern.match(line)
|
48 |
+
|
49 |
+
if match:
|
50 |
+
start_time = float(match.group(1))
|
51 |
+
text = line[match.end():].strip()
|
52 |
+
|
53 |
+
# Check if line doesnt end
|
54 |
+
if not (text.endswith('.') or text.endswith('?')):
|
55 |
+
# Merge with the next line
|
56 |
+
if i + 1 < len(lines):
|
57 |
+
next_line = lines[i + 1].strip()
|
58 |
+
next_match = timestamp_pattern.match(next_line)
|
59 |
+
|
60 |
+
if next_match:
|
61 |
+
end_time = float(next_match.group(2))
|
62 |
+
next_text = next_line[next_match.end():].strip()
|
63 |
+
merged_text = text + ' ' + next_text
|
64 |
+
merged_line = f"[{start_time:.2f}-{end_time:.2f}] {merged_text}\n"
|
65 |
+
merged_lines.append(merged_line)
|
66 |
+
i += 1
|
67 |
+
else:
|
68 |
+
end_time = float(match.group(2))
|
69 |
+
merged_lines.append(f"[{start_time:.2f}-{end_time:.2f}] {text}\n")
|
70 |
+
|
71 |
+
i += 1
|
72 |
+
|
73 |
+
# Overwrite original file with merged lines
|
74 |
+
with open(file_path, 'w') as file:
|
75 |
+
file.writelines(merged_lines)
|
76 |
+
|
77 |
+
return file_path
|
78 |
+
|
79 |
+
# Driver function
|
80 |
+
def convert_video_to_text(video_file_path, model_type='base'):
|
81 |
+
# Extract audio
|
82 |
+
audio_path = extract_audio(video_file_path)
|
83 |
+
|
84 |
+
# Transcribe audio
|
85 |
+
transcribed_path, _ = transcribe_audio(audio_path, model_type)
|
86 |
+
|
87 |
+
# Merge lines
|
88 |
+
merge_lines(transcribed_path)
|
89 |
+
|
90 |
+
return transcribed_path
|
91 |
+
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
parser = argparse.ArgumentParser(description="Transcribe audio from video")
|
95 |
+
parser.add_argument("video_file", help="Path to the video file")
|
96 |
+
parser.add_argument("--model", help="Size of the whisper model (e.g., tiny, base, small, medium, large, huge).", default="base")
|
97 |
+
args = parser.parse_args()
|
98 |
+
|
99 |
+
convert_video_to_text(args.video_file, args.model)
|