Lenylvt commited on
Commit
be35490
Β·
verified Β·
1 Parent(s): d81fec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -14
app.py CHANGED
@@ -3,17 +3,16 @@ import tempfile
3
  import os
4
  import ffmpeg # Make sure to install ffmpeg-python
5
 
6
- def save_uploaded_file(uploaded_file, progress_bar, progress_increment):
7
  try:
8
  with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
9
  tmp_file.write(uploaded_file.read())
10
- current_progress = progress_bar.progress(progress_bar.progress_value + progress_increment)
11
  return tmp_file.name
12
  except Exception as e:
13
  st.error(f"Error saving uploaded file: {e}")
14
  return None
15
 
16
- def add_subtitle_to_video(input_video_path, subtitle_file_path, subtitle_language, soft_subtitle, progress_bar, progress_increment):
17
  output_video_path = os.path.join(tempfile.gettempdir(), f"output-{os.path.basename(input_video_path)}")
18
  try:
19
  if soft_subtitle:
@@ -31,30 +30,24 @@ def add_subtitle_to_video(input_video_path, subtitle_file_path, subtitle_languag
31
  **{'vf': f'subtitles={subtitle_file_path}'}
32
  )
33
  ffmpeg.run(stream, overwrite_output=True)
34
- progress_bar.progress(progress_bar.progress_value + progress_increment)
35
  except ffmpeg.Error as e:
36
  st.error(f"FFmpeg error: {e.stderr}")
37
  return None
38
  return output_video_path
39
 
40
  def process_video(video_file, subtitle_file, subtitle_type, subtitle_language):
41
- progress_bar_text = "Operation in progress. Please Wait."
42
- progress_bar = st.progress(0)
43
- progress_bar.progress_value = 0 # Initialize progress value
44
-
45
  if video_file is not None and subtitle_file is not None:
46
- video_path = save_uploaded_file(video_file, progress_bar, 30) # Assuming 30% progress for video upload
47
- subtitle_path = save_uploaded_file(subtitle_file, progress_bar, 30) # Another 30% for subtitle upload
48
  if video_path and subtitle_path:
49
  soft_subtitle = subtitle_type == "Soft"
50
- processed_video_path = add_subtitle_to_video(video_path, subtitle_path, subtitle_language, soft_subtitle, progress_bar, 40) # Remaining 40% for processing
51
- progress_bar.progress(100) # Complete the progress bar
52
  return processed_video_path
53
  return None
54
 
55
  # Streamlit UI
56
  st.title("Video Subtitler")
57
- st.markdown("For API use, please visit [this space](https://huggingface.co/spaces/Lenylvt/SRT_to_Video-API).")
58
 
59
  video_file = st.file_uploader("πŸ“Ή Upload a video", type=["mp4", "avi", "mov"])
60
  subtitle_file = st.file_uploader("πŸ“œ Upload subtitle file", type=["srt", "vtt"])
@@ -66,4 +59,4 @@ if st.button("πŸ“ Process Video"):
66
  if processed_video_path:
67
  st.video(processed_video_path)
68
  else:
69
- st.error("πŸ”΄ Please upload both a video and a subtitle file.")
 
3
  import os
4
  import ffmpeg # Make sure to install ffmpeg-python
5
 
6
+ def save_uploaded_file(uploaded_file):
7
  try:
8
  with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
9
  tmp_file.write(uploaded_file.read())
 
10
  return tmp_file.name
11
  except Exception as e:
12
  st.error(f"Error saving uploaded file: {e}")
13
  return None
14
 
15
+ def add_subtitle_to_video(input_video_path, subtitle_file_path, subtitle_language, soft_subtitle):
16
  output_video_path = os.path.join(tempfile.gettempdir(), f"output-{os.path.basename(input_video_path)}")
17
  try:
18
  if soft_subtitle:
 
30
  **{'vf': f'subtitles={subtitle_file_path}'}
31
  )
32
  ffmpeg.run(stream, overwrite_output=True)
 
33
  except ffmpeg.Error as e:
34
  st.error(f"FFmpeg error: {e.stderr}")
35
  return None
36
  return output_video_path
37
 
38
  def process_video(video_file, subtitle_file, subtitle_type, subtitle_language):
 
 
 
 
39
  if video_file is not None and subtitle_file is not None:
40
+ video_path = save_uploaded_file(video_file)
41
+ subtitle_path = save_uploaded_file(subtitle_file)
42
  if video_path and subtitle_path:
43
  soft_subtitle = subtitle_type == "Soft"
44
+ processed_video_path = add_subtitle_to_video(video_path, subtitle_path, subtitle_language, soft_subtitle)
 
45
  return processed_video_path
46
  return None
47
 
48
  # Streamlit UI
49
  st.title("Video Subtitler")
50
+ st.markdown("The process can be long. For API use, please visit [this space](https://huggingface.co/spaces/Lenylvt/SRT_to_Video-API).")
51
 
52
  video_file = st.file_uploader("πŸ“Ή Upload a video", type=["mp4", "avi", "mov"])
53
  subtitle_file = st.file_uploader("πŸ“œ Upload subtitle file", type=["srt", "vtt"])
 
59
  if processed_video_path:
60
  st.video(processed_video_path)
61
  else:
62
+ st.error("πŸ”΄ Please upload both a video and a subtitle file.")