BatuhanYilmaz commited on
Commit
deb7eb8
β€’
1 Parent(s): 43445ac

Upload 03_πŸ“_Upload_Video_File_and_Transcript.py

Browse files
pages/03_πŸ“_Upload_Video_File_and_Transcript.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+