Update background.py
Browse files- background.py +29 -64
background.py
CHANGED
|
@@ -8,10 +8,6 @@ def generate_background(images, output_path):
|
|
| 8 |
|
| 9 |
temp_videos = []
|
| 10 |
|
| 11 |
-
# ==========================================
|
| 12 |
-
# SETTINGS
|
| 13 |
-
# ==========================================
|
| 14 |
-
|
| 15 |
slide_duration = 15
|
| 16 |
fade_duration = 1
|
| 17 |
|
|
@@ -20,21 +16,18 @@ def generate_background(images, output_path):
|
|
| 20 |
fps = 30
|
| 21 |
total_frames = total_slide_duration * fps
|
| 22 |
|
| 23 |
-
# ==========================================
|
| 24 |
-
# GENERATE SLIDES
|
| 25 |
-
# ==========================================
|
| 26 |
-
|
| 27 |
for i, img in enumerate(images):
|
| 28 |
|
| 29 |
temp_video = f"/tmp/slide_{i}.mp4"
|
| 30 |
temp_videos.append(temp_video)
|
| 31 |
|
| 32 |
vf = (
|
| 33 |
-
"scale=2560:1440,"
|
|
|
|
| 34 |
"zoompan="
|
| 35 |
-
|
| 36 |
-
"x='
|
| 37 |
-
"y='
|
| 38 |
f"d={total_frames}:"
|
| 39 |
"s=2560x1440:"
|
| 40 |
f"fps={fps},"
|
|
@@ -48,33 +41,28 @@ def generate_background(images, output_path):
|
|
| 48 |
"-i", img,
|
| 49 |
"-vf", vf,
|
| 50 |
"-t", str(total_slide_duration),
|
|
|
|
|
|
|
|
|
|
| 51 |
"-c:v", "libx264",
|
| 52 |
"-preset", "fast",
|
| 53 |
"-crf", "22",
|
| 54 |
"-pix_fmt", "yuv420p",
|
|
|
|
| 55 |
temp_video
|
| 56 |
]
|
| 57 |
|
| 58 |
print(f"Generating slide {i+1}/{len(images)}")
|
| 59 |
-
|
| 60 |
subprocess.run(cmd, check=True)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
# SINGLE IMAGE
|
| 64 |
-
# ==========================================
|
| 65 |
-
|
| 66 |
if len(temp_videos) == 1:
|
| 67 |
-
|
| 68 |
os.replace(temp_videos[0], output_path)
|
| 69 |
-
|
| 70 |
-
print("Done! (single image)")
|
| 71 |
return
|
| 72 |
|
| 73 |
-
# =========================
|
| 74 |
-
#
|
| 75 |
-
# =========================
|
| 76 |
-
|
| 77 |
-
print("Combining slides...")
|
| 78 |
|
| 79 |
cmd_concat = ["ffmpeg", "-y"]
|
| 80 |
|
|
@@ -83,70 +71,47 @@ def generate_background(images, output_path):
|
|
| 83 |
|
| 84 |
filter_complex = ""
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
for i in range(1, len(temp_videos)):
|
| 90 |
|
| 91 |
-
|
| 92 |
|
| 93 |
if i == len(temp_videos) - 1:
|
| 94 |
-
|
| 95 |
else:
|
| 96 |
-
|
| 97 |
|
| 98 |
filter_complex += (
|
| 99 |
-
f"{
|
| 100 |
-
f"
|
| 101 |
-
f"xfade="
|
| 102 |
-
f"transition=fade:"
|
| 103 |
f"duration={fade_duration}:"
|
| 104 |
-
f"offset={
|
| 105 |
-
f"{
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
current_offset += slide_duration
|
| 111 |
|
| 112 |
filter_complex = filter_complex.rstrip(";")
|
| 113 |
|
| 114 |
cmd_concat.extend([
|
| 115 |
"-filter_complex", filter_complex,
|
|
|
|
|
|
|
|
|
|
| 116 |
"-c:v", "libx264",
|
| 117 |
"-preset", "fast",
|
| 118 |
"-crf", "22",
|
| 119 |
"-pix_fmt", "yuv420p",
|
|
|
|
| 120 |
output_path
|
| 121 |
])
|
| 122 |
|
| 123 |
subprocess.run(cmd_concat, check=True)
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
# CHECK OUTPUT
|
| 127 |
-
# ==========================================
|
| 128 |
-
|
| 129 |
-
probe = subprocess.run(
|
| 130 |
-
[
|
| 131 |
-
"ffprobe",
|
| 132 |
-
"-v", "error",
|
| 133 |
-
"-show_entries",
|
| 134 |
-
"format=duration",
|
| 135 |
-
"-of",
|
| 136 |
-
"default=noprint_wrappers=1:nokey=1",
|
| 137 |
-
output_path
|
| 138 |
-
],
|
| 139 |
-
capture_output=True,
|
| 140 |
-
text=True
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
print("\n--- SUCCESS ---")
|
| 144 |
-
print(f"Duration: {probe.stdout.strip()} sec")
|
| 145 |
-
|
| 146 |
-
# ==========================================
|
| 147 |
-
# CLEANUP
|
| 148 |
-
# ==========================================
|
| 149 |
-
|
| 150 |
for v in temp_videos:
|
| 151 |
if os.path.exists(v):
|
| 152 |
os.remove(v)
|
|
|
|
| 8 |
|
| 9 |
temp_videos = []
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
slide_duration = 15
|
| 12 |
fade_duration = 1
|
| 13 |
|
|
|
|
| 16 |
fps = 30
|
| 17 |
total_frames = total_slide_duration * fps
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
for i, img in enumerate(images):
|
| 20 |
|
| 21 |
temp_video = f"/tmp/slide_{i}.mp4"
|
| 22 |
temp_videos.append(temp_video)
|
| 23 |
|
| 24 |
vf = (
|
| 25 |
+
"scale=2560:1440:force_original_aspect_ratio=increase,"
|
| 26 |
+
"crop=2560:1440,"
|
| 27 |
"zoompan="
|
| 28 |
+
"z='if(lte(on,1),1.0,min(pzoom+0.00035,1.15))':"
|
| 29 |
+
"x='iw/2-(iw/zoom/2)':"
|
| 30 |
+
"y='ih/2-(ih/zoom/2)':"
|
| 31 |
f"d={total_frames}:"
|
| 32 |
"s=2560x1440:"
|
| 33 |
f"fps={fps},"
|
|
|
|
| 41 |
"-i", img,
|
| 42 |
"-vf", vf,
|
| 43 |
"-t", str(total_slide_duration),
|
| 44 |
+
|
| 45 |
+
"-vsync", "cfr",
|
| 46 |
+
|
| 47 |
"-c:v", "libx264",
|
| 48 |
"-preset", "fast",
|
| 49 |
"-crf", "22",
|
| 50 |
"-pix_fmt", "yuv420p",
|
| 51 |
+
|
| 52 |
temp_video
|
| 53 |
]
|
| 54 |
|
| 55 |
print(f"Generating slide {i+1}/{len(images)}")
|
|
|
|
| 56 |
subprocess.run(cmd, check=True)
|
| 57 |
|
| 58 |
+
# satu gambar saja
|
|
|
|
|
|
|
|
|
|
| 59 |
if len(temp_videos) == 1:
|
|
|
|
| 60 |
os.replace(temp_videos[0], output_path)
|
|
|
|
|
|
|
| 61 |
return
|
| 62 |
|
| 63 |
+
# =========================
|
| 64 |
+
# XFADE
|
| 65 |
+
# =========================
|
|
|
|
|
|
|
| 66 |
|
| 67 |
cmd_concat = ["ffmpeg", "-y"]
|
| 68 |
|
|
|
|
| 71 |
|
| 72 |
filter_complex = ""
|
| 73 |
|
| 74 |
+
last = "[0:v]"
|
| 75 |
+
offset = slide_duration
|
| 76 |
|
| 77 |
for i in range(1, len(temp_videos)):
|
| 78 |
|
| 79 |
+
nxt = f"[{i}:v]"
|
| 80 |
|
| 81 |
if i == len(temp_videos) - 1:
|
| 82 |
+
out = ""
|
| 83 |
else:
|
| 84 |
+
out = f"[v{i}]"
|
| 85 |
|
| 86 |
filter_complex += (
|
| 87 |
+
f"{last}{nxt}"
|
| 88 |
+
f"xfade=transition=fade:"
|
|
|
|
|
|
|
| 89 |
f"duration={fade_duration}:"
|
| 90 |
+
f"offset={offset}"
|
| 91 |
+
f"{out};"
|
| 92 |
)
|
| 93 |
|
| 94 |
+
last = f"[v{i}]"
|
| 95 |
+
offset += slide_duration
|
|
|
|
| 96 |
|
| 97 |
filter_complex = filter_complex.rstrip(";")
|
| 98 |
|
| 99 |
cmd_concat.extend([
|
| 100 |
"-filter_complex", filter_complex,
|
| 101 |
+
|
| 102 |
+
"-vsync", "cfr",
|
| 103 |
+
|
| 104 |
"-c:v", "libx264",
|
| 105 |
"-preset", "fast",
|
| 106 |
"-crf", "22",
|
| 107 |
"-pix_fmt", "yuv420p",
|
| 108 |
+
|
| 109 |
output_path
|
| 110 |
])
|
| 111 |
|
| 112 |
subprocess.run(cmd_concat, check=True)
|
| 113 |
|
| 114 |
+
# cleanup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
for v in temp_videos:
|
| 116 |
if os.path.exists(v):
|
| 117 |
os.remove(v)
|