SPACERUNNER99 commited on
Commit
31a1bef
·
verified ·
1 Parent(s): fa31cb4

Update edite_video.py

Browse files
Files changed (1) hide show
  1. edite_video.py +43 -43
edite_video.py CHANGED
@@ -1,43 +1,43 @@
1
- from moviepy import *
2
- import pysrt
3
-
4
-
5
-
6
- def time_to_seconds(time_obj):
7
- return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
8
-
9
- def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
10
- subtitle_clips = []
11
- color_clips=[]
12
- for subtitle in subtitles:
13
- start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
14
- end_time = time_to_seconds(subtitle.end)
15
- duration = end_time - start_time
16
- video_width, video_height = videosize
17
- max_width = video_width * 0.8
18
- max_height = video_height * 0.2
19
- #reshaped_text = arabic_reshaper.reshape(subtitle.text)
20
- #bidi_text = get_display(reshaped_text)
21
- 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)
22
- 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)
23
- subtitle_x_position = 'center'
24
- subtitle_y_position = video_height * 0.68
25
- text_position = (subtitle_x_position, subtitle_y_position)
26
- subtitle_clips.append(text_clip.with_position(text_position))
27
- color_clips.append(myclip.with_position(text_position))
28
- return subtitle_clips, color_clips
29
-
30
- def video_edit(srt, input_video, input_audio= "audio.mp3"):
31
- print(input_video)
32
- input_video_name = input_video.split(".mp4")[0]
33
- video = VideoFileClip(input_video)
34
- audio = AudioFileClip(input_audio)
35
- video = video.with_audio(audio)
36
- print(video)
37
- output_video_file = input_video_name + '_subtitled' + ".mp4"
38
- subtitles = pysrt.open(srt, encoding="utf-8")
39
- subtitle_clips, color_clips = create_subtitle_clips(subtitles, video.size, 24, 'arial-unicode-ms.ttf', 'white', False)
40
- final_video = CompositeVideoClip([video]+color_clips + subtitle_clips)
41
- final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None)
42
- print('final')
43
- return output_video_file
 
1
+ from moviepy import *
2
+ import pysrt
3
+
4
+
5
+
6
+ def time_to_seconds(time_obj):
7
+ return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
8
+
9
+ def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
10
+ subtitle_clips = []
11
+ color_clips=[]
12
+ for subtitle in subtitles:
13
+ start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
14
+ end_time = time_to_seconds(subtitle.end)
15
+ duration = end_time - start_time
16
+ video_width, video_height = videosize
17
+ max_width = video_width * 0.8
18
+ max_height = video_height * 0.2
19
+ #reshaped_text = arabic_reshaper.reshape(subtitle.text)
20
+ #bidi_text = get_display(reshaped_text)
21
+ 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)
22
+ 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)
23
+ subtitle_x_position = 'center'
24
+ subtitle_y_position = video_height * 0.68
25
+ text_position = (subtitle_x_position, subtitle_y_position)
26
+ subtitle_clips.append(text_clip.with_position(text_position))
27
+ color_clips.append(myclip.with_position(text_position))
28
+ return subtitle_clips, color_clips
29
+
30
+ def video_edit(srt, input_video, input_audio= "audio.mp3"):
31
+ print(input_video)
32
+ input_video_name = input_video.split(".mp4")[0]
33
+ video = VideoFileClip(input_video)
34
+ audio = AudioFileClip(input_audio)
35
+ video = video.with_audio(audio)
36
+ print(video)
37
+ output_video_file = input_video_name + '_subtitled' + ".mp4"
38
+ subtitles = pysrt.open(srt, encoding="utf-8")
39
+ subtitle_clips, color_clips = create_subtitle_clips(subtitles, video.size, 24, 'arial.ttf', 'white', False)
40
+ final_video = CompositeVideoClip([video]+color_clips + subtitle_clips)
41
+ final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None)
42
+ print('final')
43
+ return output_video_file