BatuhanYilmaz commited on
Commit
9aaeafd
โ€ข
1 Parent(s): f33d1e7
pages/02_๐Ÿ“ผ_Upload_Video_File.py CHANGED
@@ -9,8 +9,6 @@ from io import StringIO
9
  import numpy as np
10
  import pathlib
11
  import os
12
- import components.authenticate as authenticate
13
- import torch
14
 
15
  st.set_page_config(page_title="Auto Subtitled Video Generator", page_icon=":movie_camera:", layout="wide")
16
 
@@ -51,10 +49,8 @@ with col2:
51
 
52
  @st.cache(allow_output_mutation=True)
53
  def change_model(current_size, size):
54
- torch.cuda.is_available()
55
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
56
  if current_size != size:
57
- loaded_model = whisper.load_model(size, device=DEVICE)
58
  return loaded_model
59
  else:
60
  raise Exception("Model size is the same as the current size.")
@@ -102,7 +98,7 @@ def getSubs(segments: Iterator[dict], format: str, maxLineWidth: int) -> str:
102
  def generate_subtitled_video(video, audio, transcript):
103
  video_file = ffmpeg.input(video)
104
  audio_file = ffmpeg.input(audio)
105
- ffmpeg.concat(video_file.filter("subtitles", transcript), audio_file, v=1, a=1).output("final.mp4").global_args('-report').run(quiet=True, overwrite_output=True)
106
  video_with_subs = open("final.mp4", "rb")
107
  return video_with_subs
108
 
@@ -112,7 +108,7 @@ def main():
112
  loaded_model = change_model(current_size, size)
113
  st.write(f"Model is {'multilingual' if loaded_model.is_multilingual else 'English-only'} "
114
  f"and has {sum(np.prod(p.shape) for p in loaded_model.parameters()):,} parameters.")
115
- input_file = st.file_uploader("Upload Video File", type=["mp4", "avi", "mov", "mkv"])
116
  # get the name of the input_file
117
  if input_file is not None:
118
  filename = input_file.name[:-4]
@@ -230,10 +226,5 @@ def main():
230
 
231
 
232
  if __name__ == "__main__":
233
- authenticate.set_st_state_vars()
234
- if st.session_state["authenticated"]:
235
- main()
236
- authenticate.button_logout()
237
- else:
238
- st.info("Please log in or sign up to use the app.")
239
- authenticate.button_login()
 
9
  import numpy as np
10
  import pathlib
11
  import os
 
 
12
 
13
  st.set_page_config(page_title="Auto Subtitled Video Generator", page_icon=":movie_camera:", layout="wide")
14
 
 
49
 
50
  @st.cache(allow_output_mutation=True)
51
  def change_model(current_size, size):
 
 
52
  if current_size != size:
53
+ loaded_model = whisper.load_model(size)
54
  return loaded_model
55
  else:
56
  raise Exception("Model size is the same as the current size.")
 
98
  def generate_subtitled_video(video, audio, transcript):
99
  video_file = ffmpeg.input(video)
100
  audio_file = ffmpeg.input(audio)
101
+ ffmpeg.concat(video_file.filter("subtitles", transcript), audio_file, v=1, a=1).output("final.mp4").run(quiet=True, overwrite_output=True)
102
  video_with_subs = open("final.mp4", "rb")
103
  return video_with_subs
104
 
 
108
  loaded_model = change_model(current_size, size)
109
  st.write(f"Model is {'multilingual' if loaded_model.is_multilingual else 'English-only'} "
110
  f"and has {sum(np.prod(p.shape) for p in loaded_model.parameters()):,} parameters.")
111
+ input_file = st.file_uploader("File", type=["mp4", "avi", "mov", "mkv"])
112
  # get the name of the input_file
113
  if input_file is not None:
114
  filename = input_file.name[:-4]
 
226
 
227
 
228
  if __name__ == "__main__":
229
+ main()
230
+ st.markdown("###### Made with :heart: by [@BatuhanYฤฑlmaz](https://twitter.com/batuhan3326) [![this is an image link](https://i.imgur.com/thJhzOO.png)](https://www.buymeacoffee.com/batuhanylmz)")
 
 
 
 
 
pages/03_๐Ÿ“_Upload_Video_File_and_Transcript.py DELETED
@@ -1,137 +0,0 @@
1
- import streamlit as st
2
- from streamlit_lottie import st_lottie
3
- from utils import write_vtt, write_srt
4
- import ffmpeg
5
- import requests
6
- from typing import Iterator
7
- from io import StringIO
8
- import numpy as np
9
- import pathlib
10
- import os
11
- import components.authenticate as authenticate
12
-
13
-
14
- st.set_page_config(page_title="Auto Subtitled Video Generator", page_icon=":movie_camera:", layout="wide")
15
-
16
- # Define a function that we can use to load lottie files from a link.
17
- @st.cache(allow_output_mutation=True)
18
- def load_lottieurl(url: str):
19
- r = requests.get(url)
20
- if r.status_code != 200:
21
- return None
22
- return r.json()
23
-
24
-
25
- APP_DIR = pathlib.Path(__file__).parent.absolute()
26
-
27
- LOCAL_DIR = APP_DIR / "local_transcript"
28
- LOCAL_DIR.mkdir(exist_ok=True)
29
- save_dir = LOCAL_DIR / "output"
30
- save_dir.mkdir(exist_ok=True)
31
-
32
-
33
- col1, col2 = st.columns([1, 3])
34
- with col1:
35
- lottie = load_lottieurl("https://assets6.lottiefiles.com/packages/lf20_cjnxwrkt.json")
36
- st_lottie(lottie)
37
-
38
- with col2:
39
- st.write("""
40
- ## Auto Subtitled Video Generator
41
- ##### โž  Upload a video file and a transcript as .srt or .vtt file and get a video with subtitles.
42
- ##### โž  Processing time will increase as the video length increases. """)
43
-
44
-
45
- def getSubs(segments: Iterator[dict], format: str, maxLineWidth: int) -> str:
46
- segmentStream = StringIO()
47
-
48
- if format == 'vtt':
49
- write_vtt(segments, file=segmentStream, maxLineWidth=maxLineWidth)
50
- elif format == 'srt':
51
- write_srt(segments, file=segmentStream, maxLineWidth=maxLineWidth)
52
- else:
53
- raise Exception("Unknown format " + format)
54
-
55
- segmentStream.seek(0)
56
- return segmentStream.read()
57
-
58
-
59
- def split_video_audio(uploaded_file):
60
- with open(f"{save_dir}/input.mp4", "wb") as f:
61
- f.write(uploaded_file.read())
62
- audio = ffmpeg.input(f"{save_dir}/input.mp4")
63
- audio = ffmpeg.output(audio, f"{save_dir}/output.wav", acodec="pcm_s16le", ac=1, ar="16k")
64
- ffmpeg.run(audio, overwrite_output=True)
65
-
66
-
67
- def main():
68
- uploaded_video = st.file_uploader("Upload Video File", type=["mp4", "avi", "mov", "mkv"])
69
- # get the name of the input_file
70
- if uploaded_video is not None:
71
- filename = uploaded_video.name[:-4]
72
- else:
73
- filename = None
74
- transcript_file = st.file_uploader("Upload Transcript File", type=["srt", "vtt"])
75
- if transcript_file is not None:
76
- transcript_name = transcript_file.name
77
- else:
78
- transcript_name = None
79
- if uploaded_video is not None and transcript_file is not None:
80
- if transcript_name[-3:] == "vtt":
81
- with open("uploaded_transcript.vtt", "wb") as f:
82
- f.writelines(transcript_file)
83
- f.close()
84
- with open(os.path.join(os.getcwd(), "uploaded_transcript.vtt"), "rb") as f:
85
- vtt_file = f.read()
86
- if st.button("Generate Video with Subtitles"):
87
- with st.spinner("Generating Subtitled Video"):
88
- split_video_audio(uploaded_video)
89
- video_file = ffmpeg.input(f"{save_dir}/input.mp4")
90
- audio_file = ffmpeg.input(f"{save_dir}/output.wav")
91
- ffmpeg.concat(video_file.filter("subtitles", "uploaded_transcript.vtt"), audio_file, v=1, a=1).output("final.mp4").global_args('-report').run(quiet=True, overwrite_output=True)
92
- video_with_subs = open("final.mp4", "rb")
93
- col3, col4 = st.columns(2)
94
- with col3:
95
- st.video(uploaded_video)
96
- with col4:
97
- st.video(video_with_subs)
98
- st.download_button(label="Download Video with Subtitles",
99
- data=video_with_subs,
100
- file_name=f"{filename}_with_subs.mp4")
101
-
102
- elif transcript_name[-3:] == "srt":
103
- with open("uploaded_transcript.srt", "wb") as f:
104
- f.writelines(transcript_file)
105
- f.close()
106
- with open(os.path.join(os.getcwd(), "uploaded_transcript.srt"), "rb") as f:
107
- srt_file = f.read()
108
- if st.button("Generate Video with Subtitles"):
109
- with st.spinner("Generating Subtitled Video"):
110
- split_video_audio(uploaded_video)
111
- video_file = ffmpeg.input(f"{save_dir}/input.mp4")
112
- audio_file = ffmpeg.input(f"{save_dir}/output.wav")
113
- ffmpeg.concat(video_file.filter("subtitles", "uploaded_transcript.srt"), audio_file, v=1, a=1).output("final.mp4").run(quiet=True, overwrite_output=True)
114
- video_with_subs = open("final.mp4", "rb")
115
- col3, col4 = st.columns(2)
116
- with col3:
117
- st.video(uploaded_video)
118
- with col4:
119
- st.video(video_with_subs)
120
- st.download_button(label="Download Video with Subtitles",
121
- data=video_with_subs,
122
- file_name=f"{filename}_with_subs.mp4")
123
- else:
124
- st.error("Please upload a .srt or .vtt file")
125
- else:
126
- st.info("Please upload a video file and a transcript file")
127
-
128
-
129
- if __name__ == "__main__":
130
- authenticate.set_st_state_vars()
131
- if st.session_state["authenticated"]:
132
- main()
133
- authenticate.button_logout()
134
- else:
135
- st.info("Please log in or sign up to use the app.")
136
- authenticate.button_login()
137
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pages/{04_๐Ÿ”Š_Upload_Audio_File.py โ†’ 03_๐Ÿ”Š_Upload_Audio_File.py} RENAMED
@@ -9,8 +9,6 @@ from io import StringIO
9
  import numpy as np
10
  import pathlib
11
  import os
12
- import components.authenticate as authenticate
13
- import torch
14
 
15
  st.set_page_config(page_title="Auto Transcriber", page_icon="๐Ÿ”Š", layout="wide")
16
 
@@ -50,10 +48,8 @@ current_size = "None"
50
 
51
  @st.cache(allow_output_mutation=True)
52
  def change_model(current_size, size):
53
- torch.cuda.is_available()
54
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
55
  if current_size != size:
56
- loaded_model = whisper.load_model(size, device=DEVICE)
57
  return loaded_model
58
  else:
59
  raise Exception("Model size is the same as the current size.")
@@ -102,7 +98,7 @@ def main():
102
  loaded_model = change_model(current_size, size)
103
  st.write(f"Model is {'multilingual' if loaded_model.is_multilingual else 'English-only'} "
104
  f"and has {sum(np.prod(p.shape) for p in loaded_model.parameters()):,} parameters.")
105
- input_file = st.file_uploader("Upload Audio File", type=["mp3", "wav", "m4a"])
106
  if input_file is not None:
107
  filename = input_file.name[:-4]
108
  else:
@@ -205,10 +201,5 @@ def main():
205
 
206
 
207
  if __name__ == "__main__":
208
- authenticate.set_st_state_vars()
209
- if st.session_state["authenticated"]:
210
- main()
211
- authenticate.button_logout()
212
- else:
213
- st.info("Please log in or sign up to use the app.")
214
- authenticate.button_login()
 
9
  import numpy as np
10
  import pathlib
11
  import os
 
 
12
 
13
  st.set_page_config(page_title="Auto Transcriber", page_icon="๐Ÿ”Š", layout="wide")
14
 
 
48
 
49
  @st.cache(allow_output_mutation=True)
50
  def change_model(current_size, size):
 
 
51
  if current_size != size:
52
+ loaded_model = whisper.load_model(size)
53
  return loaded_model
54
  else:
55
  raise Exception("Model size is the same as the current size.")
 
98
  loaded_model = change_model(current_size, size)
99
  st.write(f"Model is {'multilingual' if loaded_model.is_multilingual else 'English-only'} "
100
  f"and has {sum(np.prod(p.shape) for p in loaded_model.parameters()):,} parameters.")
101
+ input_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "m4a"])
102
  if input_file is not None:
103
  filename = input_file.name[:-4]
104
  else:
 
201
 
202
 
203
  if __name__ == "__main__":
204
+ main()
205
+ st.markdown("###### Made with :heart: by [@BatuhanYฤฑlmaz](https://twitter.com/batuhan3326) [![this is an image link](https://i.imgur.com/thJhzOO.png)](https://www.buymeacoffee.com/batuhanylmz)")