-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdescriptpy.py
More file actions
119 lines (109 loc) · 3.62 KB
/
descriptpy.py
File metadata and controls
119 lines (109 loc) · 3.62 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import eel
import shutil
import whisper_timestamped as whisper
import wx
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
def reduce_strikethrough_words(strikethrough_words:list, gap:float=0.1)->list:
"""
Reduces the list of strikethrough words by merging consecutive words
that are closer than `gap` seconds to each other.
"""
reduced_words = []
current_start = None
current_end = None
for word in strikethrough_words:
if current_start is None:
current_start = float(word["start"])
current_end = float(word["end"])
else:
if float(word["start"]) - current_end <= gap:
current_end = float(word["end"])
else:
reduced_words.append({"start": current_start, "end": current_end})
current_start = float(word["start"])
current_end = float(word["end"])
reduced_words.append({"start": current_start, "end": current_end})
return reduced_words
@eel.expose
def getpath()->str:
"""
Shows a file open dialog and returns the selected file path.
If no file is selected, returns None.also copies the file into
./public/temp.mp4
"""
wildcard="*"
app = wx.App(None)
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(None, 'Open', wildcard=wildcard, style=style)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = None
dialog.Destroy()
shutil.copyfile(path,"./public/temp.mp4")
return path
@eel.expose
def transcribe(size)->list:
"""
Transcribes the audio from the video file in `./public/temp.mp4`.
Returns the transcribed words along with their start and end times.
"""
mdl =size
audio = whisper.load_audio("./public/temp.mp4")
model = whisper.load_model(mdl, device="cpu")
result = whisper.transcribe(model, audio)
seg = result["segments"]
out = []
for i in seg:
words = i["words"]
for word in words:
dct = {}
dct["text"] = word["text"]
dct["start"] = word["start"]
dct["end"]= word["end"]
out.append(dct)
result = []
for i in range(len(out) - 1):
result.append(out[i])
if out[i]['end'] < out[i + 1]['start']:
result.append({
'text': '|-|',
'start': out[i]['end'],
'end': out[i + 1]['start']
})
result.append(out[-1])
return result
@eel.expose
def saveVid(strikethrough_words:list,lpath:str)->None:
"""
Saves the edited video by removing the strikethrough words
"""
app = wx.App(None)
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(None, "Save file", "", "", "All files (*.*)|*.*", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = None
clip = VideoFileClip(lpath)
video_duration = clip.duration
strikethrough_words = reduce_strikethrough_words(strikethrough_words)
for word in strikethrough_words:
start = float(word["start"])
end = float(word["end"])
print((start,end))
if(start >= 0 and end <= video_duration):
clip = clip.cutout(ta=start,tb=end)
clip.write_videofile(path)
try:
# remove the file if it exists
os.remove("./public/temp.mp4")
except FileNotFoundError:
# do nothing if the file doesn't exist
pass
eel.init('public')
try:
eel.start('./index.html' ,mode='chrome',size=(1200,600))
except EnvironmentError:
eel.start("./index.html",mode='default',size=(1200,600))