ChiBenevisamPas commited on
Commit
555a60a
·
verified ·
1 Parent(s): ef3342d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -58
app.py CHANGED
@@ -6,69 +6,37 @@ from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
6
  # Load the Whisper model
7
  model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
8
 
9
- def write_srt(transcription, output_file):
10
- with open(output_file, "w") as f:
11
- for i, segment in enumerate(transcription['segments']):
12
- start = segment['start']
13
- end = segment['end']
14
- text = segment['text']
15
- # Format timestamps for SRT
16
- start_time = whisper.utils.format_timestamp(start)
17
- end_time = whisper.utils.format_timestamp(end)
18
- f.write(f"{i + 1}\n")
19
- f.write(f"{start_time} --> {end_time}\n")
20
- f.write(f"{text.strip()}\n\n")
21
-
22
- def create_subtitled_video(video_file, srt_file):
23
- # Load video
24
- video_clip = VideoFileClip(video_file)
25
-
26
- # Read SRT file and create text clips
27
- subs = []
28
- with open(srt_file, 'r') as f:
29
- lines = f.readlines()
30
- for i in range(0, len(lines), 4):
31
- if i + 1 < len(lines): # Ensure there is a next line for timestamps
32
- timestamp_line = lines[i + 1].strip()
33
- if " --> " in timestamp_line: # Ensure it contains a timestamp
34
- try:
35
- start_time = convert_to_seconds(timestamp_line.split(" --> ")[0])
36
- end_time = convert_to_seconds(timestamp_line.split(" --> ")[1])
37
- text = lines[i + 2].strip() if (i + 2 < len(lines)) else ""
38
- text_clip = TextClip(text, fontsize=24, color='white', bg_color='black', size=video_clip.size, print_cmd=True)
39
- text_clip = text_clip.set_start(start_time).set_end(end_time).set_position('bottom')
40
- subs.append(text_clip)
41
- except Exception as e:
42
- print(f"Error processing subtitle {i // 4 + 1}: {e}") # Debug print
43
- continue # Skip to the next subtitle on error
44
-
45
- # Combine video and subtitles
46
- final_video = CompositeVideoClip([video_clip] + subs)
47
-
48
- # Save the final video with subtitles
49
- subtitled_video_file = "subtitled_video.mp4"
50
- final_video.write_videofile(subtitled_video_file, codec="libx264", audio_codec="aac")
51
-
52
- return subtitled_video_file
53
-
54
- def convert_to_seconds(timestamp):
55
- """Convert timestamp in SRT format (HH:MM:SS,mmm) to seconds."""
56
- h, m, s = timestamp.split(':')
57
- seconds = int(h) * 3600 + int(m) * 60 + float(s.replace(',', '.'))
58
- return seconds
59
-
60
  def transcribe_video(video_file):
61
  # Transcribe the video to generate subtitles
62
  result = model.transcribe(video_file)
63
 
64
- # Save the transcription to an .srt file
65
- srt_file = "generated_subtitles.srt"
 
 
 
66
 
67
- # Write the transcription as subtitles
68
- write_srt(result, srt_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Create the subtitled video
71
- subtitled_video_file = create_subtitled_video(video_file, srt_file)
 
72
 
73
  return subtitled_video_file
74
 
@@ -78,7 +46,7 @@ iface = gr.Interface(
78
  inputs=gr.File(label="Upload Video"),
79
  outputs=gr.File(label="Download Subtitled Video"),
80
  title="Video Subtitle Generator",
81
- description="Upload a video file to generate subtitles and download a subtitled video."
82
  )
83
 
84
  if __name__ == "__main__":
 
6
  # Load the Whisper model
7
  model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def transcribe_video(video_file):
10
  # Transcribe the video to generate subtitles
11
  result = model.transcribe(video_file)
12
 
13
+ # Create a list of (start_time, end_time, text) tuples for subtitles
14
+ subtitles = [(segment['start'], segment['end'], segment['text'].strip()) for segment in result['segments']]
15
+
16
+ # Create a subtitled video
17
+ subtitled_video_file = create_subtitled_video(video_file, subtitles)
18
 
19
+ return subtitled_video_file
20
+
21
+ def create_subtitled_video(video_file, subtitles):
22
+ # Load the original video
23
+ video = VideoFileClip(video_file)
24
+
25
+ # Create a list of TextClips for each subtitle
26
+ text_clips = []
27
+ for start, end, text in subtitles:
28
+ text_clip = (TextClip(text, fontsize=24, color='white', bg_color='black', size=video.size)
29
+ .set_start(start)
30
+ .set_duration(end - start)
31
+ .set_position(('center', 'bottom'))) # Position the subtitle at the bottom center
32
+ text_clips.append(text_clip)
33
+
34
+ # Overlay the subtitles on the video
35
+ final_video = CompositeVideoClip([video] + text_clips)
36
 
37
+ # Save the final video
38
+ subtitled_video_file = "subtitled_video.mp4"
39
+ final_video.write_videofile(subtitled_video_file, codec='libx264', audio_codec='aac')
40
 
41
  return subtitled_video_file
42
 
 
46
  inputs=gr.File(label="Upload Video"),
47
  outputs=gr.File(label="Download Subtitled Video"),
48
  title="Video Subtitle Generator",
49
+ description="Upload a video file to generate a subtitled video using Whisper."
50
  )
51
 
52
  if __name__ == "__main__":