Lenylvt commited on
Commit
eb5718c
1 Parent(s): effdcb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -32
app.py CHANGED
@@ -87,48 +87,60 @@ def transcribe(audio_file_path, model_size="base"):
87
  return "\n".join(transcription_with_timestamps)
88
 
89
  # Fonction pour ajouter des sous-titres à une vidéo
90
- def add_subtitle_to_video(input_video, subtitle_file, subtitle_language, soft_subtitle):
91
  video_input_stream = ffmpeg.input(input_video)
92
  subtitle_input_stream = ffmpeg.input(subtitle_file)
93
  input_video_name = os.path.splitext(os.path.basename(input_video))[0]
94
- output_video = f"/tmp/output-{input_video_name}.mp4"
95
- subtitle_track_title = os.path.splitext(os.path.basename(subtitle_file))[0]
96
-
97
  if soft_subtitle:
98
- stream = ffmpeg.output(
99
- video_input_stream, subtitle_input_stream, output_video,
100
- **{"c": "copy", "c:s": "mov_text"},
101
- **{"metadata:s:s:0": f"language={subtitle_language}", "metadata:s:s:0": f"title={subtitle_track_title}"}
102
- )
103
  else:
104
- stream = ffmpeg.output(
105
- video_input_stream, output_video,
106
- vf=f"subtitles={subtitle_file}"
107
- )
108
-
109
  ffmpeg.run(stream, overwrite_output=True)
110
  return output_video
111
 
112
- # Définition des interfaces Gradio
113
- def transcribe_and_translate_video(video_file, source_language_code, target_language_code):
 
 
 
 
 
 
 
 
114
  transcription = transcribe(video_file.name, "tiny")
115
  srt_path = text_to_srt(transcription)
116
  translated_srt_path = translate_srt(srt_path, source_language_code, target_language_code)
117
- output_video = add_subtitle_to_video(video_file.name, translated_srt_path, target_language_code, False)
118
- return output_video, translated_srt_path
119
-
120
- # Inputs et outputs Gradio
121
- video_input = gr.inputs.Video(label="Video File")
122
- source_language_dropdown = gr.inputs.Dropdown(choices=language_options, label="Source Language")
123
- target_language_dropdown = gr.inputs.Dropdown(choices=language_options, label="Target Language")
124
-
125
- transcribe_translate_interface = gr.Interface(
126
- fn=transcribe_and_translate_video,
127
- inputs=[video_input, source_language_dropdown, target_language_dropdown],
128
- outputs=[gr.outputs.Video(label="Video with Translated Subtitles"), gr.outputs.File(label="Translated Subtitles (.srt)")],
129
- title="Video Transcribe & Translate",
130
- description="Transcribe and translate the subtitles of your video into another language."
 
131
  )
132
 
133
- # Lancement de l'application Gradio
134
- transcribe_translate_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  return "\n".join(transcription_with_timestamps)
88
 
89
  # Fonction pour ajouter des sous-titres à une vidéo
90
+ def add_subtitle_to_video(input_video, subtitle_file, subtitle_language, soft_subtitle=False):
91
  video_input_stream = ffmpeg.input(input_video)
92
  subtitle_input_stream = ffmpeg.input(subtitle_file)
93
  input_video_name = os.path.splitext(os.path.basename(input_video))[0]
94
+ output_video = f"/tmp/{input_video_name}_subtitled.mp4"
 
 
95
  if soft_subtitle:
96
+ stream = ffmpeg.output(video_input_stream, subtitle_input_stream, output_video, **{"c": "copy", "c:s": "mov_text"})
 
 
 
 
97
  else:
98
+ stream = ffmpeg.output(video_input_stream, output_video, vf=f"subtitles={subtitle_file}")
 
 
 
 
99
  ffmpeg.run(stream, overwrite_output=True)
100
  return output_video
101
 
102
+ # Fonction de transcription pour Gradio avec ajout de sous-titres à la vidéo
103
+ def transcribe_video(video_file):
104
+ transcription = transcribe(video_file.name, "tiny")
105
+ srt_path = text_to_srt(transcription)
106
+ # Ajouter les sous-titres à la vidéo
107
+ output_video_path = add_subtitle_to_video(video_file.name, srt_path, subtitle_language="eng", soft_subtitle=False)
108
+ return output_video_path, srt_path
109
+
110
+ # Fonction de traduction pour Gradio qui utilise maintenant add_subtitle_to_video
111
+ def translate_video(video_file, source_language_code, target_language_code):
112
  transcription = transcribe(video_file.name, "tiny")
113
  srt_path = text_to_srt(transcription)
114
  translated_srt_path = translate_srt(srt_path, source_language_code, target_language_code)
115
+ # Ajoutez les sous-titres traduits à la vidéo
116
+ output_video_path = add_subtitle_to_video(video_file.name, translated_srt_path, target_language_code, soft_subtitle=False)
117
+ return output_video_path, translated_srt_path
118
+
119
+ # Interface Gradio
120
+ file_input = gr.Video(label="Video File")
121
+ source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
122
+ target_language_dropdown = gr.Dropdown(choices=language_options, label="Target Language")
123
+
124
+ transcription_interface = gr.Interface(
125
+ fn=transcribe_video,
126
+ inputs=file_input,
127
+ outputs=[gr.Video(label="Video with Subtitles"), gr.File(label="Transcription File (.srt)")],
128
+ title="Video Transcription",
129
+ description="Automatically transcribe the audio from your video into subtitles and add them to the video."
130
  )
131
 
132
+ translation_interface = gr.Interface(
133
+ fn=translate_video,
134
+ inputs=[file_input, source_language_dropdown, target_language_dropdown],
135
+ outputs=[gr.Video(label="Video with Translated Subtitles"), gr.File(label="Translated Subtitles (.srt)")],
136
+ title="Video Translation",
137
+ description="Translate the subtitles of your video into another language and add them to the video."
138
+ )
139
+
140
+ tabs = gr.Tabs([
141
+ gr.Tab(transcription_interface, label="Transcribe"),
142
+ gr.Tab(translation_interface, label="Translate")
143
+ ])
144
+
145
+ # Lancement de l'application
146
+ tabs.launch()