-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (44 loc) · 1.72 KB
/
main.py
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
import whisper
from whisper.utils import write_vtt
import subprocess
import os
# get system args
import argparse
parser = argparse.ArgumentParser(prog = 'AI Captions', description = 'Create captions for videos using AI.')
parser.add_argument("-v", "--video", help="The video file to add captions to", required=True, type=str)
parser.add_argument("-m", "--model", help="The whisper model to use", required=True, type=str)
parser.add_argument("-p", "--preview", help="Preview the video", action="store_true")
args = parser.parse_args()
def main():
videoFile = args.video
whisperModel = args.model
validateVideo(videoFile)
print("Extracting audio from video")
subprocess.call("ffmpeg -i " + videoFile + " output/audio.mp3 -y -hide_banner -v warning -stats")
# get audio from video usinng mp
# send audio to whisper
model = whisper.load_model(whisperModel)
print("Transcribing audio")
out = model.transcribe("output/audio.mp3", verbose=True)
with open("output/captions.vtt", "w") as f:
print("Writing transcript to file")
write_vtt(out["segments"], file=f)
addCaptions()
def addCaptions():
print("Adding captions to video")
subprocess.call("ffmpeg -i " + args.video + " -vf subtitles=output/captions.vtt output/output.mp4 -y -hide_banner -v warning -stats")
if args.preview:
print("Previewing video")
# get absolute path
path = os.path.abspath("output/output.mp4")
os.startfile(path)
def validateVideo(videoFile):
# check if video file exists
try:
with open(videoFile, 'r') as f:
pass
except FileNotFoundError:
print("Video file does not exist")
exit()
if __name__ == "__main__":
main()