Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import whisper
|
3 |
+
from pytube import YouTube
|
4 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
5 |
+
from youtube_transcript_api.formatters import JSONFormatter
|
6 |
+
from urllib.parse import urlparse, parse_qs
|
7 |
+
from moviepy.editor import *
|
8 |
+
from moviepy.video.tools.subtitles import SubtitlesClip
|
9 |
+
import glob
|
10 |
+
from translatepy import Translator
|
11 |
+
import os
|
12 |
+
import pandas as pd
|
13 |
+
from TTS.api import TTS
|
14 |
+
import wave
|
15 |
+
|
16 |
+
@st.cache_resource
|
17 |
+
def load_whisper_model():
|
18 |
+
model = whisper.load_model("base")
|
19 |
+
return model
|
20 |
+
|
21 |
+
@st.cache_resource
|
22 |
+
def load_audio_model():
|
23 |
+
device = "cpu"
|
24 |
+
tts = TTS(model_name="tts_models/fr/mai/tacotron2-DDC").to(device)
|
25 |
+
return tts
|
26 |
+
|
27 |
+
def download_video(vid_link):
|
28 |
+
# download video in mp4
|
29 |
+
yt = YouTube(vid_link)
|
30 |
+
video_stream = yt.streams.filter(
|
31 |
+
progressive=True, file_extension="mp4"
|
32 |
+
).first()
|
33 |
+
filename = "UIFiles/" + yt.title + ".mp4"
|
34 |
+
video_stream.download("UIFiles/", yt.title + ".mp4")
|
35 |
+
|
36 |
+
isEngCaptions = False
|
37 |
+
|
38 |
+
# download english captions
|
39 |
+
query_params = parse_qs(urlparse(vid_link).query)
|
40 |
+
vid_id = query_params["v"][0]
|
41 |
+
try:
|
42 |
+
captions = YouTubeTranscriptApi.get_transcript(vid_id, languages=["en"])
|
43 |
+
except:
|
44 |
+
return filename, isEngCaptions, "No English captions found for this video, choose another one!"
|
45 |
+
isEngCaptions = True
|
46 |
+
formatter = JSONFormatter()
|
47 |
+
formatted_captions = formatter.format_transcript(captions)
|
48 |
+
|
49 |
+
return filename, isEngCaptions, formatted_captions
|
50 |
+
|
51 |
+
def vid_audio_to_text(vid, model):
|
52 |
+
# separate audio from video in mp4 format
|
53 |
+
videoclip = VideoFileClip(vid)
|
54 |
+
audioclip = videoclip.audio
|
55 |
+
file_name = vid.replace(".mp4", ".mp3")
|
56 |
+
audioclip.write_audiofile(file_name)
|
57 |
+
|
58 |
+
# convert audio clip to text
|
59 |
+
result = model.transcribe(file_name)
|
60 |
+
text = result["text"]
|
61 |
+
segments = result["segments"]
|
62 |
+
segments_df = pd.DataFrame(segments, columns=['start', 'end', 'text'])
|
63 |
+
|
64 |
+
segments_df.columns = ['Timestamp', 'End_Timestamp', 'Original Text']
|
65 |
+
|
66 |
+
return file_name, text, segments_df
|
67 |
+
|
68 |
+
def translate(text, language="French"):
|
69 |
+
translator = Translator()
|
70 |
+
translated = translator.translate(text, language)
|
71 |
+
|
72 |
+
return translated.result
|
73 |
+
|
74 |
+
def convert_french_to_audio(original_text, tts_model, output_path=""):
|
75 |
+
tts_model.tts_to_file(original_text, file_path=output_path)
|
76 |
+
# return tts_model.tts(original_text)
|
77 |
+
|
78 |
+
def delete_temp_file(fpath):
|
79 |
+
if os.path.exists(fpath):
|
80 |
+
os.remove(fpath)
|
81 |
+
|
82 |
+
def change_audio_speed(audio_fname, scale_factor):
|
83 |
+
oa = wave.open(audio_fname, 'rb')
|
84 |
+
rate = oa.getframerate()
|
85 |
+
signal = oa.readframes(-1)
|
86 |
+
|
87 |
+
new_fname = audio_fname.replace('.wav', '_speed.wav')
|
88 |
+
new_aud = wave.open(new_fname, 'wb')
|
89 |
+
new_aud.setnchannels(1)
|
90 |
+
new_aud.setsampwidth(2)
|
91 |
+
new_aud.setframerate(rate * scale_factor)
|
92 |
+
new_aud.writeframes(signal)
|
93 |
+
new_aud.close()
|
94 |
+
|
95 |
+
return new_fname
|
96 |
+
|
97 |
+
def add_subtitles_and_translation_to_movie(subs_df, translated, vid_fname):
|
98 |
+
segs = subs_df.values.tolist()
|
99 |
+
subs = []
|
100 |
+
for start, end, text in segs:
|
101 |
+
subs.append(((start, end), text))
|
102 |
+
|
103 |
+
video = VideoFileClip(vid_fname)
|
104 |
+
generator = lambda txt: TextClip(txt, font='Arial', fontsize=16, color='white', method='caption', align='south', size=video.size)
|
105 |
+
|
106 |
+
subtitles = SubtitlesClip(subs, generator)
|
107 |
+
audio_fr_name = vid_fname.replace('.mp4', '_subs.wav')
|
108 |
+
convert_french_to_audio(translated, tts, audio_fr_name)
|
109 |
+
|
110 |
+
audio_fr = AudioFileClip(audio_fr_name)
|
111 |
+
new_audio_fr = change_audio_speed(audio_fr_name, audio_fr.duration/video.duration)
|
112 |
+
audio_fr = AudioFileClip(new_audio_fr)
|
113 |
+
|
114 |
+
video_fr = video.set_audio(audio_fr)
|
115 |
+
|
116 |
+
result = CompositeVideoClip([video_fr, subtitles.set_pos(('center','bottom'))])
|
117 |
+
sub_vid_fname = vid_fname.replace(".mp4", "_subs.mp4")
|
118 |
+
result.write_videofile(sub_vid_fname, fps=video_fr.fps)
|
119 |
+
return sub_vid_fname, audio_fr_name, new_audio_fr
|
120 |
+
|
121 |
+
st.title("Translate YouTube subtitles from English to French")
|
122 |
+
|
123 |
+
form = st.form(key='my_form')
|
124 |
+
vid_link = form.text_input("Select a YouTube Video with English Captions", placeholder="Enter full URL")
|
125 |
+
submitted = submit_button = form.form_submit_button(label='Submit')
|
126 |
+
|
127 |
+
if submitted:
|
128 |
+
vid_fname, isEngCaptions, captions = download_video(vid_link)
|
129 |
+
if not isEngCaptions:
|
130 |
+
st.write(captions)
|
131 |
+
else:
|
132 |
+
model = load_whisper_model()
|
133 |
+
tts = load_audio_model()
|
134 |
+
|
135 |
+
audio_fname, audio_text, segments_df = vid_audio_to_text(vid_fname, model)
|
136 |
+
|
137 |
+
# subtitles with timestamp
|
138 |
+
segments_df['Translated Text'] = segments_df['Original Text'].apply((lambda x: translate(x)))
|
139 |
+
translated_audio_text = translate(audio_text)
|
140 |
+
|
141 |
+
sub_vid_fname, audio_fr_name, new_audio_fr = add_subtitles_and_translation_to_movie(segments_df[['Timestamp', 'End_Timestamp', 'Translated Text']], translated_audio_text, vid_fname)
|
142 |
+
|
143 |
+
st.video(sub_vid_fname)
|
144 |
+
|
145 |
+
segments_df['Timestamp'] = segments_df['Timestamp'].map(lambda x: '{:02}:{:02}:{:02.2f}'.format(int(x//3600), int(x%3600//60), x%60))
|
146 |
+
del segments_df['End_Timestamp']
|
147 |
+
|
148 |
+
st.table(segments_df.set_index(segments_df.columns[0]))
|
149 |
+
|
150 |
+
delete_temp_file(vid_fname)
|
151 |
+
delete_temp_file(audio_fname)
|
152 |
+
delete_temp_file(sub_vid_fname)
|
153 |
+
delete_temp_file(audio_fr_name)
|
154 |
+
delete_temp_file(new_audio_fr)
|
155 |
+
|