Lenylvt commited on
Commit
dab027f
·
verified ·
1 Parent(s): 43a87ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -19
app.py CHANGED
@@ -1,35 +1,25 @@
1
  import gradio as gr
2
  import os
3
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
4
- import ffmpeg # Make sure to install ffmpeg-python
5
 
6
  def read_subtitle_file(subtitle_path):
7
  with open(subtitle_path, 'r', encoding='utf-8') as file:
8
  subtitle_content = file.read()
9
  return os.path.basename(subtitle_path), subtitle_content
10
 
11
- def add_subtitle_to_video(input_video, subtitle_file, subtitle_language, soft_subtitle):
12
- subtitle_file_path = subtitle_file.replace(" ", "\\ ")
13
  video_input_stream = ffmpeg.input(input_video)
14
- subtitle_input_stream = ffmpeg.input(subtitle_file)
15
- input_video_name = os.path.splitext(os.path.basename(input_video))[0]
16
- output_video = f"/tmp/output-{input_video_name}.mp4"
17
- subtitle_track_title = os.path.splitext(os.path.basename(subtitle_file))[0]
18
-
19
- stream = ffmpeg.output(
20
- video_input_stream, subtitle_input_stream, output_video,
21
- **{"c": "copy", "c:s": "mov_text"},
22
- **{"metadata:s:s:0": f"language={subtitle_language}",
23
- "metadata:s:s:0": f"title={subtitle_track_title}"}
24
- )
25
 
 
 
26
  ffmpeg.run(stream, overwrite_output=True)
27
  return output_video
28
 
29
- def video_demo(video, subtitle, subtitle_type, subtitle_language):
30
  if subtitle is not None:
31
- soft_subtitle = "Hard"
32
- processed_video_path = add_subtitle_to_video(video, subtitle, subtitle_language, soft_subtitle)
33
  return processed_video_path
34
  else:
35
  return video
@@ -71,6 +61,5 @@ with gr.Blocks() as demo:
71
  unsafe_allow_html=True
72
  )
73
 
74
-
75
  if __name__ == "__main__":
76
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
4
+ import ffmpeg # Ensure ffmpeg-python is installed
5
 
6
  def read_subtitle_file(subtitle_path):
7
  with open(subtitle_path, 'r', encoding='utf-8') as file:
8
  subtitle_content = file.read()
9
  return os.path.basename(subtitle_path), subtitle_content
10
 
11
+ def add_hard_subtitle_to_video(input_video, subtitle_file, subtitle_language):
 
12
  video_input_stream = ffmpeg.input(input_video)
13
+ output_video = f"/tmp/output-{os.path.splitext(os.path.basename(input_video))[0]}.mp4"
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Hard subtitle process
16
+ stream = ffmpeg.output(video_input_stream, output_video, vf=f"subtitles={subtitle_file}")
17
  ffmpeg.run(stream, overwrite_output=True)
18
  return output_video
19
 
20
+ def video_demo(video, subtitle, subtitle_language):
21
  if subtitle is not None:
22
+ processed_video_path = add_hard_subtitle_to_video(video, subtitle, subtitle_language)
 
23
  return processed_video_path
24
  else:
25
  return video
 
61
  unsafe_allow_html=True
62
  )
63
 
 
64
  if __name__ == "__main__":
65
+ app.launch()