Deddy commited on
Commit
67e6ed2
1 Parent(s): 6240bf6

Update process_energy.py

Browse files
Files changed (1) hide show
  1. process_energy.py +47 -8
process_energy.py CHANGED
@@ -72,7 +72,7 @@ import time
72
  import numpy as np
73
  from tqdm import tqdm
74
  from scipy.io.wavfile import write
75
- from moviepy.editor import CompositeVideoClip, VideoFileClip, AudioFileClip
76
  from moviepy.video.fx.all import fadein, fadeout, loop
77
  from PIL import Image, ImageDraw, ImageFont
78
 
@@ -167,20 +167,59 @@ def activate_transfer():
167
  new_mp = recipient_mp + transferred_mp
168
  return new_mp
169
 
170
- # Fungsi untuk membuat video afirmasi dengan audio tanpa teks
171
- def create_affirmation_video_with_audio(audio_filename, output_filename="affirmation_video.mp4"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  # Load background GIF and loop it
173
  background_clip = VideoFileClip("spin_energy.gif").fx(loop, duration=180)
174
 
175
  # Load the generated audio file
176
  audio_clip = AudioFileClip(audio_filename)
177
 
178
- # Set background duration and add audio
179
- video = background_clip.set_duration(180).set_audio(audio_clip)
180
 
181
- # Write the result to a file with a lower resolution to reduce size
182
  video.write_videofile(output_filename, fps=24, codec='libx264', bitrate='500k') # Bitrate rendah untuk ukuran file lebih kecil
183
 
 
 
 
184
  # Fungsi untuk memproses transfer energi
185
  def process_energy_transfer(tujuan, nama_anda):
186
  status = "Mengumpulkan Energi Semesta Digital ..."
@@ -206,7 +245,7 @@ def process_energy_transfer(tujuan, nama_anda):
206
 
207
  new_mp = sum(energy_data)
208
  video_filename = "affirmation_video.mp4"
209
- create_affirmation_video_with_audio(filename, video_filename)
210
  video_path = os.path.abspath(video_filename)
211
  return (
212
  f"Transfer Selesai! MP baru Anda: {new_mp:.2f}\n\n"
@@ -217,4 +256,4 @@ def process_energy_transfer(tujuan, nama_anda):
217
  filename,
218
  status,
219
  video_path
220
- )
 
72
  import numpy as np
73
  from tqdm import tqdm
74
  from scipy.io.wavfile import write
75
+ from moviepy.editor import ImageClip, CompositeVideoClip, VideoFileClip, AudioFileClip
76
  from moviepy.video.fx.all import fadein, fadeout, loop
77
  from PIL import Image, ImageDraw, ImageFont
78
 
 
167
  new_mp = recipient_mp + transferred_mp
168
  return new_mp
169
 
170
+ from PIL import Image, ImageDraw, ImageFont
171
+ from moviepy.editor import ImageClip, CompositeVideoClip, VideoFileClip, AudioFileClip
172
+ from moviepy.video.fx.loop import loop
173
+ import os
174
+
175
+ # Fungsi untuk membuat frame gambar afirmasi
176
+ def create_affirmation_frame(text, width=640, height=480, duration=5):
177
+ frame = Image.new("RGBA", (width, height), color=(0, 0, 0, 0)) # Transparan
178
+ draw = ImageDraw.Draw(frame)
179
+
180
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Path font default
181
+ font_size = 48
182
+
183
+ # Menggunakan font default
184
+ try:
185
+ font = ImageFont.truetype(font_path, font_size)
186
+ except IOError:
187
+ font = ImageFont.load_default()
188
+
189
+ bbox = draw.textbbox((0, 0), text, font=font)
190
+ width_text = bbox[2] - bbox[0]
191
+ height_text = bbox[3] - bbox[1]
192
+ draw.text(((width - width_text) / 2, (height - height_text) / 2), text, font=font, fill="white")
193
+
194
+ return frame
195
+
196
+ # Fungsi untuk membuat video afirmasi dengan audio
197
+ def create_affirmation_video_with_audio(tujuan, audio_filename, output_filename="affirmation_video.mp4"):
198
+ # Create frame for the chosen goal
199
+ frame = create_affirmation_frame(tujuan.upper(), width=640, height=480)
200
+
201
+ # Save frame as image
202
+ image_filename = "frame_tujuan.png"
203
+ frame.save(image_filename)
204
+
205
+ # Load image as video clip
206
+ clip = ImageClip(image_filename).set_duration(180) # Durasi teks 3 menit
207
+
208
  # Load background GIF and loop it
209
  background_clip = VideoFileClip("spin_energy.gif").fx(loop, duration=180)
210
 
211
  # Load the generated audio file
212
  audio_clip = AudioFileClip(audio_filename)
213
 
214
+ # Concatenate text clip with background
215
+ video = CompositeVideoClip([background_clip, clip]).set_duration(180).set_audio(audio_clip)
216
 
217
+ # Write the result to a file
218
  video.write_videofile(output_filename, fps=24, codec='libx264', bitrate='500k') # Bitrate rendah untuk ukuran file lebih kecil
219
 
220
+ # Remove temporary image file
221
+ os.remove(image_filename)
222
+
223
  # Fungsi untuk memproses transfer energi
224
  def process_energy_transfer(tujuan, nama_anda):
225
  status = "Mengumpulkan Energi Semesta Digital ..."
 
245
 
246
  new_mp = sum(energy_data)
247
  video_filename = "affirmation_video.mp4"
248
+ create_affirmation_video_with_audio(tujuan, filename, video_filename)
249
  video_path = os.path.abspath(video_filename)
250
  return (
251
  f"Transfer Selesai! MP baru Anda: {new_mp:.2f}\n\n"
 
256
  filename,
257
  status,
258
  video_path
259
+ )