SPACERUNNER99 commited on
Commit
c9f042d
·
verified ·
1 Parent(s): 982fd8d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from moviepy import *
2
+ import pysrt
3
+ import gradio as gr
4
+
5
+
6
+
7
+ def time_to_seconds(time_obj):
8
+ return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
9
+
10
+ def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
11
+ subtitle_clips = []
12
+ color_clips=[]
13
+ for subtitle in subtitles:
14
+ start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
15
+ end_time = time_to_seconds(subtitle.end)
16
+ duration = end_time - start_time
17
+ video_width, video_height = videosize
18
+ max_width = video_width * 0.8
19
+ max_height = video_height * 0.2
20
+ #reshaped_text = arabic_reshaper.reshape(subtitle.text)
21
+ #bidi_text = get_display(reshaped_text)
22
+ text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.8), int(video_height * 0.2)) ,text_align="right" ,color=color, method='caption').with_start(start_time).with_duration(duration)
23
+ #myclip = ColorClip(size=(int(video_width * 0.8), int(video_height * 0.2)) , color=(225, 0, 0)).with_opacity(0.2).with_start(start_time).with_duration(duration)
24
+ subtitle_x_position = 'center'
25
+ subtitle_y_position = video_height * 0.68
26
+ text_position = (subtitle_x_position, subtitle_y_position)
27
+ subtitle_clips.append(text_clip.with_position(text_position))
28
+ #color_clips.append(myclip.with_position(text_position))
29
+ return subtitle_clips
30
+
31
+ def video_edit(srt, input_video, color, font, input_audio= "audio.mp3"):
32
+ print(input_video)
33
+ input_video_name = input_video.split(".mp4")[0]
34
+ video = VideoFileClip(input_video)
35
+ audio = AudioFileClip(input_audio)
36
+ video = video.with_audio(audio)
37
+ print(video)
38
+ output_video_file = input_video_name + '_subtitled' + ".mp4"
39
+ subtitles = pysrt.open(srt, encoding="utf-8")
40
+ subtitle_clips = create_subtitle_clips(subtitles, video.size, 32, f'{font}.ttf', color, False)
41
+ final_video = CompositeVideoClip([video]+ subtitle_clips)
42
+ final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None, preset = "faster", fps=24)
43
+ print('final')
44
+ video.close()
45
+ audio.close()
46
+ return output_video_file
47
+
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("Start typing below and then click **Run** to see the progress and final output.")
50
+ with gr.Column():
51
+ srt_file = gr.File()
52
+ video_in = gr.Video()
53
+ color = gr.Text()
54
+ font = gr.Number()
55
+ btn = gr.Button("Create")
56
+ output_video = gr.Video()
57
+ btn.click(
58
+ fn=dub,
59
+ inputs=[srt_file, video_in, color, font],
60
+ outputs=output_video
61
+ )
62
+
63
+ demo.launch(debug=True)