Update background.py
Browse files- background.py +95 -58
background.py
CHANGED
|
@@ -7,104 +7,139 @@ def generate_background(images, output_path):
|
|
| 7 |
raise Exception("No images uploaded")
|
| 8 |
|
| 9 |
temp_videos = []
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
fps = 30
|
| 17 |
total_frames = total_slide_duration * fps
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# ==========================================
|
| 20 |
-
#
|
| 21 |
# ==========================================
|
| 22 |
for i, img in enumerate(images):
|
|
|
|
| 23 |
temp_video = f"/tmp/slide_{i}.mp4"
|
| 24 |
temp_videos.append(temp_video)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
cmd = [
|
| 27 |
"ffmpeg",
|
| 28 |
"-y",
|
| 29 |
"-loop", "1",
|
| 30 |
"-i", img,
|
| 31 |
-
"-vf",
|
| 32 |
-
(
|
| 33 |
-
# JALAN TENGAH: Upscale ke 2K (2560x1440)
|
| 34 |
-
# Jauh lebih ringan dari 4K, tapi ampuh menahan getaran di durasi 15 detik
|
| 35 |
-
"scale=2560:1440,"
|
| 36 |
-
|
| 37 |
-
# Rumus zoom dikembalikan ke versi yang terbukti berjalan (bergerak dari 1.30 ke 1.0)
|
| 38 |
-
"zoompan="
|
| 39 |
-
f"z='1.30-(0.30*on/{total_frames})':"
|
| 40 |
-
"x='iw/2-(iw/zoom/2)':"
|
| 41 |
-
"y='ih/2-(ih/zoom/2)':"
|
| 42 |
-
f"d={total_frames}:"
|
| 43 |
-
"s=2560x1440:"
|
| 44 |
-
f"fps={fps},"
|
| 45 |
-
|
| 46 |
-
# Downscale kembali ke 1080p
|
| 47 |
-
"scale=1920:1080:flags=lanczos"
|
| 48 |
-
),
|
| 49 |
"-t", str(total_slide_duration),
|
| 50 |
"-r", str(fps),
|
| 51 |
"-c:v", "libx264",
|
| 52 |
-
"-preset", "fast",
|
|
|
|
| 53 |
"-pix_fmt", "yuv420p",
|
| 54 |
temp_video
|
| 55 |
]
|
| 56 |
-
|
| 57 |
-
print(f"Generating slide {i+1}
|
| 58 |
subprocess.run(cmd, check=True)
|
| 59 |
|
| 60 |
-
#
|
|
|
|
|
|
|
| 61 |
if len(temp_videos) == 1:
|
|
|
|
| 62 |
os.replace(temp_videos[0], output_path)
|
| 63 |
-
|
|
|
|
| 64 |
return
|
| 65 |
|
| 66 |
# ==========================================
|
| 67 |
-
#
|
| 68 |
# ==========================================
|
| 69 |
-
print("Combining slides
|
| 70 |
-
|
| 71 |
cmd_concat = ["ffmpeg", "-y"]
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
filter_complex = ""
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
for i in range(1, len(temp_videos)):
|
|
|
|
| 80 |
next_input = f"[{i}:v]"
|
| 81 |
-
|
| 82 |
-
|
| 83 |
if i == len(temp_videos) - 1:
|
| 84 |
output_label = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
filter_complex += f"{last_output}{next_input}xfade=transition=fade:duration={fade_duration}:offset={current_offset}{output_label};"
|
| 88 |
-
|
| 89 |
-
last_output = f"[v_mix_{i}]"
|
| 90 |
current_offset += slide_duration
|
| 91 |
|
| 92 |
-
|
| 93 |
-
filter_complex = filter_complex[:-1]
|
| 94 |
|
| 95 |
cmd_concat.extend([
|
| 96 |
-
"-filter_complex",
|
| 97 |
-
|
| 98 |
-
"-
|
| 99 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
output_path
|
| 101 |
])
|
| 102 |
|
| 103 |
-
# Jalankan proses penggabungan
|
| 104 |
subprocess.run(cmd_concat, check=True)
|
| 105 |
|
| 106 |
# ==========================================
|
| 107 |
-
#
|
| 108 |
# ==========================================
|
| 109 |
probe = subprocess.run(
|
| 110 |
[
|
|
@@ -120,10 +155,12 @@ def generate_background(images, output_path):
|
|
| 120 |
text=True
|
| 121 |
)
|
| 122 |
|
| 123 |
-
print("\
|
| 124 |
-
print(f"
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
| 7 |
raise Exception("No images uploaded")
|
| 8 |
|
| 9 |
temp_videos = []
|
| 10 |
+
|
| 11 |
+
# ==========================================
|
| 12 |
+
# SETTINGS
|
| 13 |
+
# ==========================================
|
| 14 |
+
slide_duration = 15
|
| 15 |
+
fade_duration = 1
|
| 16 |
+
|
| 17 |
+
total_slide_duration = slide_duration + fade_duration
|
| 18 |
+
|
| 19 |
fps = 30
|
| 20 |
total_frames = total_slide_duration * fps
|
| 21 |
|
| 22 |
+
# Internal resolution
|
| 23 |
+
internal_w = 3840
|
| 24 |
+
internal_h = 2160
|
| 25 |
+
|
| 26 |
+
output_w = 1920
|
| 27 |
+
output_h = 1080
|
| 28 |
+
|
| 29 |
# ==========================================
|
| 30 |
+
# GENERATE EACH SLIDE
|
| 31 |
# ==========================================
|
| 32 |
for i, img in enumerate(images):
|
| 33 |
+
|
| 34 |
temp_video = f"/tmp/slide_{i}.mp4"
|
| 35 |
temp_videos.append(temp_video)
|
| 36 |
|
| 37 |
+
# Zoom-in 1.00 -> 1.15
|
| 38 |
+
zoom_factor = 1.15
|
| 39 |
+
|
| 40 |
+
start_crop_w = internal_w
|
| 41 |
+
start_crop_h = internal_h
|
| 42 |
+
|
| 43 |
+
end_crop_w = int(internal_w / zoom_factor)
|
| 44 |
+
end_crop_h = int(internal_h / zoom_factor)
|
| 45 |
+
|
| 46 |
+
crop_w_step = (start_crop_w - end_crop_w) / total_frames
|
| 47 |
+
crop_h_step = (start_crop_h - end_crop_h) / total_frames
|
| 48 |
+
|
| 49 |
+
vf = (
|
| 50 |
+
f"scale={internal_w}:{internal_h}:force_original_aspect_ratio=increase,"
|
| 51 |
+
f"crop="
|
| 52 |
+
f"w='max({end_crop_w},{start_crop_w}-on*{crop_w_step})':"
|
| 53 |
+
f"h='max({end_crop_h},{start_crop_h}-on*{crop_h_step})':"
|
| 54 |
+
f"x='(iw-ow)/2':"
|
| 55 |
+
f"y='(ih-oh)/2',"
|
| 56 |
+
f"scale={output_w}:{output_h}:flags=lanczos"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
cmd = [
|
| 60 |
"ffmpeg",
|
| 61 |
"-y",
|
| 62 |
"-loop", "1",
|
| 63 |
"-i", img,
|
| 64 |
+
"-vf", vf,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"-t", str(total_slide_duration),
|
| 66 |
"-r", str(fps),
|
| 67 |
"-c:v", "libx264",
|
| 68 |
+
"-preset", "fast",
|
| 69 |
+
"-crf", "20",
|
| 70 |
"-pix_fmt", "yuv420p",
|
| 71 |
temp_video
|
| 72 |
]
|
| 73 |
+
|
| 74 |
+
print(f"Generating slide {i+1}/{len(images)}")
|
| 75 |
subprocess.run(cmd, check=True)
|
| 76 |
|
| 77 |
+
# ==========================================
|
| 78 |
+
# SINGLE IMAGE
|
| 79 |
+
# ==========================================
|
| 80 |
if len(temp_videos) == 1:
|
| 81 |
+
|
| 82 |
os.replace(temp_videos[0], output_path)
|
| 83 |
+
|
| 84 |
+
print("Done! (single image)")
|
| 85 |
return
|
| 86 |
|
| 87 |
# ==========================================
|
| 88 |
+
# COMBINE WITH XFADE
|
| 89 |
# ==========================================
|
| 90 |
+
print("Combining slides...")
|
| 91 |
+
|
| 92 |
cmd_concat = ["ffmpeg", "-y"]
|
| 93 |
+
|
| 94 |
+
for video in temp_videos:
|
| 95 |
+
cmd_concat.extend(["-i", video])
|
| 96 |
|
| 97 |
filter_complex = ""
|
| 98 |
+
|
| 99 |
+
last_output = "[0:v]"
|
| 100 |
+
current_offset = slide_duration
|
| 101 |
|
| 102 |
for i in range(1, len(temp_videos)):
|
| 103 |
+
|
| 104 |
next_input = f"[{i}:v]"
|
| 105 |
+
|
|
|
|
| 106 |
if i == len(temp_videos) - 1:
|
| 107 |
output_label = ""
|
| 108 |
+
else:
|
| 109 |
+
output_label = f"[v{i}]"
|
| 110 |
+
|
| 111 |
+
filter_complex += (
|
| 112 |
+
f"{last_output}"
|
| 113 |
+
f"{next_input}"
|
| 114 |
+
f"xfade=transition=fade:"
|
| 115 |
+
f"duration={fade_duration}:"
|
| 116 |
+
f"offset={current_offset}"
|
| 117 |
+
f"{output_label};"
|
| 118 |
+
)
|
| 119 |
|
| 120 |
+
last_output = f"[v{i}]"
|
|
|
|
|
|
|
|
|
|
| 121 |
current_offset += slide_duration
|
| 122 |
|
| 123 |
+
filter_complex = filter_complex.rstrip(";")
|
|
|
|
| 124 |
|
| 125 |
cmd_concat.extend([
|
| 126 |
+
"-filter_complex",
|
| 127 |
+
filter_complex,
|
| 128 |
+
"-c:v",
|
| 129 |
+
"libx264",
|
| 130 |
+
"-preset",
|
| 131 |
+
"fast",
|
| 132 |
+
"-crf",
|
| 133 |
+
"20",
|
| 134 |
+
"-pix_fmt",
|
| 135 |
+
"yuv420p",
|
| 136 |
output_path
|
| 137 |
])
|
| 138 |
|
|
|
|
| 139 |
subprocess.run(cmd_concat, check=True)
|
| 140 |
|
| 141 |
# ==========================================
|
| 142 |
+
# CHECK OUTPUT
|
| 143 |
# ==========================================
|
| 144 |
probe = subprocess.run(
|
| 145 |
[
|
|
|
|
| 155 |
text=True
|
| 156 |
)
|
| 157 |
|
| 158 |
+
print("\nSUCCESS")
|
| 159 |
+
print(f"Duration : {probe.stdout.strip()} sec")
|
| 160 |
+
|
| 161 |
+
# ==========================================
|
| 162 |
+
# CLEANUP
|
| 163 |
+
# ==========================================
|
| 164 |
+
for video in temp_videos:
|
| 165 |
+
if os.path.exists(video):
|
| 166 |
+
os.remove(video)
|