codewithdark commited on
Commit
0680b74
·
verified ·
1 Parent(s): 550ad1f

Create utility/render /render_engine.py

Browse files
Files changed (1) hide show
  1. utility/render /render_engine.py +78 -0
utility/render /render_engine.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import os
3
+ import tempfile
4
+ import zipfile
5
+ import platform
6
+ import subprocess
7
+ from moviepy.editor import (AudioFileClip, CompositeVideoClip, CompositeAudioClip, ImageClip,
8
+ TextClip, VideoFileClip)
9
+ from moviepy.audio.fx.audio_loop import audio_loop
10
+ from moviepy.audio.fx.audio_normalize import audio_normalize
11
+ import requests
12
+
13
+ def download_file(url, filename):
14
+ with open(filename, 'wb') as f:
15
+ headers = {
16
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
17
+ }
18
+ response = requests.get(url, headers=headers)
19
+ f.write(response.content)
20
+
21
+ def search_program(program_name):
22
+ try:
23
+ search_cmd = "where" if platform.system() == "Windows" else "which"
24
+ return subprocess.check_output([search_cmd, program_name]).decode().strip()
25
+ except subprocess.CalledProcessError:
26
+ return None
27
+
28
+ def get_program_path(program_name):
29
+ program_path = search_program(program_name)
30
+ return program_path
31
+
32
+ def get_output_media(audio_file_path, timed_captions, background_video_data, video_server):
33
+ OUTPUT_FILE_NAME = "rendered_video.mp4"
34
+ magick_path = get_program_path("magick")
35
+ print(magick_path)
36
+ if magick_path:
37
+ os.environ['IMAGEMAGICK_BINARY'] = magick_path
38
+ else:
39
+ os.environ['IMAGEMAGICK_BINARY'] = '/usr/bin/convert'
40
+
41
+ visual_clips = []
42
+ for (t1, t2), video_url in background_video_data:
43
+ # Download the video file
44
+ video_filename = tempfile.NamedTemporaryFile(delete=False).name
45
+ download_file(video_url, video_filename)
46
+
47
+ # Create VideoFileClip from the downloaded file
48
+ video_clip = VideoFileClip(video_filename)
49
+ video_clip = video_clip.set_start(t1)
50
+ video_clip = video_clip.set_end(t2)
51
+ visual_clips.append(video_clip)
52
+
53
+ audio_clips = []
54
+ audio_file_clip = AudioFileClip(audio_file_path)
55
+ audio_clips.append(audio_file_clip)
56
+
57
+ for (t1, t2), text in timed_captions:
58
+ text_clip = TextClip(txt=text, fontsize=100, color="white", stroke_width=3, stroke_color="black", method="label")
59
+ text_clip = text_clip.set_start(t1)
60
+ text_clip = text_clip.set_end(t2)
61
+ text_clip = text_clip.set_position(["center", 800])
62
+ visual_clips.append(text_clip)
63
+
64
+ video = CompositeVideoClip(visual_clips)
65
+
66
+ if audio_clips:
67
+ audio = CompositeAudioClip(audio_clips)
68
+ video.duration = audio.duration
69
+ video.audio = audio
70
+
71
+ video.write_videofile(OUTPUT_FILE_NAME, codec='libx264', audio_codec='aac', fps=25, preset='veryfast')
72
+
73
+ # Clean up downloaded files
74
+ for (t1, t2), video_url in background_video_data:
75
+ video_filename = tempfile.NamedTemporaryFile(delete=False).name
76
+ os.remove(video_filename)
77
+
78
+ return OUTPUT_FILE_NAME