ChiBenevisamPas commited on
Commit
c10abaa
·
verified ·
1 Parent(s): 1a2251f

YouTube Link and Other Languages

Browse files
Files changed (1) hide show
  1. app.py +41 -12
app.py CHANGED
@@ -7,6 +7,7 @@ from fpdf import FPDF
7
  from pptx import Presentation
8
  import subprocess
9
  import shlex
 
10
 
11
  # Load the Whisper model (smaller model for faster transcription)
12
  model = whisper.load_model("tiny")
@@ -17,6 +18,15 @@ def load_translation_model(target_language):
17
  "fa": "fa", # Persian (Farsi)
18
  "es": "es", # Spanish
19
  "fr": "fr", # French
 
 
 
 
 
 
 
 
 
20
  }
21
  target_lang_code = lang_codes.get(target_language)
22
  if not target_lang_code:
@@ -121,10 +131,25 @@ def write_ppt(transcription, output_file, tokenizer=None, translation_model=None
121
  title.text = f"{i + 1}. {text.strip()}"
122
  ppt.save(output_file)
123
 
 
 
 
 
 
 
 
 
 
 
124
  # Transcribing video and generating output
125
- def transcribe_video(video_file, language, target_language, output_format):
126
- result = model.transcribe(video_file.name, language=language)
127
- video_name = os.path.splitext(video_file.name)[0]
 
 
 
 
 
128
  if target_language != "en":
129
  try:
130
  tokenizer, translation_model = load_translation_model(target_language)
@@ -141,13 +166,13 @@ def transcribe_video(video_file, language, target_language, output_format):
141
  elif output_format == "Video with Hardsub":
142
  output_video = f"{video_name}_with_subtitles.mp4"
143
  try:
144
- embed_hardsub_in_video(video_file.name, srt_file, output_video)
145
  return output_video
146
  except Exception as e:
147
  raise RuntimeError(f"Error embedding subtitles in video: {e}")
148
  elif output_format == "Word":
149
  word_file = f"{video_name}.docx"
150
- write_word(result, word_file, tokenizer, translation_model)
151
  return word_file
152
  elif output_format == "PDF":
153
  pdf_file = f"{video_name}.pdf"
@@ -158,19 +183,24 @@ def transcribe_video(video_file, language, target_language, output_format):
158
  write_ppt(result, ppt_file, tokenizer, translation_model)
159
  return ppt_file
160
 
161
- # Gradio interface with better UI
162
  iface = gr.Interface(
163
  fn=transcribe_video,
164
  inputs=[
165
- gr.File(label="Upload Video File"),
 
166
  gr.Dropdown(label="Select Original Video Language", choices=["en", "es", "fr", "de", "it", "pt"], value="en"),
167
- gr.Dropdown(label="Select Subtitle Translation Language", choices=["en", "fa", "es", "fr"], value="fa"),
 
 
 
 
168
  gr.Radio(label="Choose Output Format", choices=["SRT", "Video with Hardsub", "Word", "PDF", "PowerPoint"], value="Video with Hardsub")
169
  ],
170
  outputs=gr.File(label="Download File"),
171
- title="Video Subtitle Generator with Translation & Multi-Format Output",
172
  description=(
173
- "This tool allows you to generate subtitles from a video file using Whisper, "
174
  "translate the subtitles into multiple languages using M2M100, and export them "
175
  "in various formats including SRT, hardcoded subtitles in video, Word, PDF, or PowerPoint."
176
  ),
@@ -178,5 +208,4 @@ iface = gr.Interface(
178
  live=False # No live interaction needed
179
  )
180
 
181
- if __name__ == "__main__":
182
- iface.launch()
 
7
  from pptx import Presentation
8
  import subprocess
9
  import shlex
10
+ import yt_dlp
11
 
12
  # Load the Whisper model (smaller model for faster transcription)
13
  model = whisper.load_model("tiny")
 
18
  "fa": "fa", # Persian (Farsi)
19
  "es": "es", # Spanish
20
  "fr": "fr", # French
21
+ "de": "de", # German
22
+ "it": "it", # Italian
23
+ "pt": "pt", # Portuguese
24
+ "ar": "ar", # Arabic
25
+ "zh": "zh", # Chinese
26
+ "hi": "hi", # Hindi
27
+ "ja": "ja", # Japanese
28
+ "ko": "ko", # Korean
29
+ "ru": "ru", # Russian
30
  }
31
  target_lang_code = lang_codes.get(target_language)
32
  if not target_lang_code:
 
131
  title.text = f"{i + 1}. {text.strip()}"
132
  ppt.save(output_file)
133
 
134
+ # Function to download YouTube video
135
+ def download_youtube_video(url):
136
+ ydl_opts = {
137
+ 'format': 'mp4',
138
+ 'outtmpl': 'downloaded_video.mp4',
139
+ }
140
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
141
+ ydl.download([url])
142
+ return 'downloaded_video.mp4'
143
+
144
  # Transcribing video and generating output
145
+ def transcribe_video(video_file, video_url, language, target_language, output_format):
146
+ if video_url:
147
+ video_file_path = download_youtube_video(video_url)
148
+ else:
149
+ video_file_path = video_file.name
150
+
151
+ result = model.transcribe(video_file_path, language=language)
152
+ video_name = os.path.splitext(video_file_path)[0]
153
  if target_language != "en":
154
  try:
155
  tokenizer, translation_model = load_translation_model(target_language)
 
166
  elif output_format == "Video with Hardsub":
167
  output_video = f"{video_name}_with_subtitles.mp4"
168
  try:
169
+ embed_hardsub_in_video(video_file_path, srt_file, output_video)
170
  return output_video
171
  except Exception as e:
172
  raise RuntimeError(f"Error embedding subtitles in video: {e}")
173
  elif output_format == "Word":
174
  word_file = f"{video_name}.docx"
175
+ write_word(result, word_file, tokenizer, translation_model, target_language)
176
  return word_file
177
  elif output_format == "PDF":
178
  pdf_file = f"{video_name}.pdf"
 
183
  write_ppt(result, ppt_file, tokenizer, translation_model)
184
  return ppt_file
185
 
186
+ # Gradio interface with YouTube URL
187
  iface = gr.Interface(
188
  fn=transcribe_video,
189
  inputs=[
190
+ gr.File(label="Upload Video File (or leave empty for YouTube link)", optional=True),
191
+ gr.Textbox(label="YouTube Video URL (optional)", placeholder="https://www.youtube.com/watch?v=..."),
192
  gr.Dropdown(label="Select Original Video Language", choices=["en", "es", "fr", "de", "it", "pt"], value="en"),
193
+ gr.Dropdown(
194
+ label="Select Subtitle Translation Language",
195
+ choices=["en", "fa", "es", "fr", "de", "it", "pt", "ar", "zh", "hi", "ja", "ko", "ru"],
196
+ value="fa"
197
+ ),
198
  gr.Radio(label="Choose Output Format", choices=["SRT", "Video with Hardsub", "Word", "PDF", "PowerPoint"], value="Video with Hardsub")
199
  ],
200
  outputs=gr.File(label="Download File"),
201
+ title="Video Subtitle Generator with Translation & Multi-Format Output (Supports YouTube)",
202
  description=(
203
+ "This tool allows you to generate subtitles from a video file or YouTube link using Whisper, "
204
  "translate the subtitles into multiple languages using M2M100, and export them "
205
  "in various formats including SRT, hardcoded subtitles in video, Word, PDF, or PowerPoint."
206
  ),
 
208
  live=False # No live interaction needed
209
  )
210
 
211
+ iface.launch()