Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ OUTPUT_DIR = os.path.abspath("outputs")
|
|
| 10 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 12 |
|
| 13 |
-
|
| 14 |
def get_video_duration(video_path):
|
| 15 |
cmd = [
|
| 16 |
"ffprobe",
|
|
@@ -19,11 +18,9 @@ def get_video_duration(video_path):
|
|
| 19 |
"-of", "default=noprint_wrappers=1:nokey=1",
|
| 20 |
video_path
|
| 21 |
]
|
| 22 |
-
|
| 23 |
result = subprocess.check_output(cmd).decode().strip()
|
| 24 |
return float(result)
|
| 25 |
|
| 26 |
-
|
| 27 |
def create_loop(
|
| 28 |
input_video,
|
| 29 |
loop_method,
|
|
@@ -39,6 +36,7 @@ def create_loop(
|
|
| 39 |
input_path = os.path.join(TEMP_DIR, f"{uid}_input.mp4")
|
| 40 |
output_path = os.path.join(OUTPUT_DIR, f"{uid}_loop.mp4")
|
| 41 |
|
|
|
|
| 42 |
if isinstance(input_video, dict):
|
| 43 |
video_file = input_video["path"]
|
| 44 |
else:
|
|
@@ -47,58 +45,37 @@ def create_loop(
|
|
| 47 |
shutil.copy(video_file, input_path)
|
| 48 |
duration = get_video_duration(input_path)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
#
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
if loop_method == "Crossfade (Standard)":
|
| 54 |
if duration <= fade_duration * 2:
|
| 55 |
-
raise gr.Error(
|
| 56 |
-
|
| 57 |
-
f"Minimum duration needed: {fade_duration * 2:.1f} seconds"
|
| 58 |
-
)
|
| 59 |
offset = duration - fade_duration
|
| 60 |
filter_complex = f"""
|
| 61 |
-
[0:v]
|
| 62 |
-
|
| 63 |
-
fps={fps},
|
| 64 |
-
format=rgb24,
|
| 65 |
-
split[v1][v2];
|
| 66 |
-
|
| 67 |
-
[v1][v2]
|
| 68 |
-
xfade=
|
| 69 |
-
transition={transition}:
|
| 70 |
-
duration={fade_duration}:
|
| 71 |
-
offset={offset},
|
| 72 |
-
|
| 73 |
format=yuv420p[v]
|
| 74 |
"""
|
| 75 |
-
target_duration = str(duration)
|
| 76 |
|
| 77 |
elif loop_method == "Split & Swap (Premiere Style)":
|
| 78 |
midpoint = duration / 2.0
|
| 79 |
offset = midpoint - fade_duration
|
| 80 |
|
| 81 |
if offset <= 0:
|
| 82 |
-
raise gr.Error(
|
| 83 |
-
|
| 84 |
-
"Reduce fade duration or use a longer video."
|
| 85 |
-
)
|
| 86 |
filter_complex = f"""
|
| 87 |
-
[0:v]
|
| 88 |
-
setpts=PTS-STARTPTS
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
split[v_a][v_b];
|
| 92 |
-
|
| 93 |
-
[v_a]trim=start=0:end={midpoint},setpts=PTS-STARTPTS,fps={fps}[partA];
|
| 94 |
-
[v_b]trim=start={midpoint}:end={duration},setpts=PTS-STARTPTS,fps={fps}[partB];
|
| 95 |
-
|
| 96 |
-
[partB][partA]
|
| 97 |
-
xfade=
|
| 98 |
-
transition={transition}:
|
| 99 |
-
duration={fade_duration}:
|
| 100 |
-
offset={offset},
|
| 101 |
-
|
| 102 |
format=yuv420p[v]
|
| 103 |
"""
|
| 104 |
target_duration = str(duration - fade_duration)
|
|
@@ -106,90 +83,61 @@ def create_loop(
|
|
| 106 |
else:
|
| 107 |
# PING-PONG / BOOMERANG METHOD
|
| 108 |
filter_complex = f"""
|
| 109 |
-
[0:v]
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
format=rgb24,
|
| 113 |
-
split[fwd][rev_src];
|
| 114 |
-
|
| 115 |
-
[rev_src]
|
| 116 |
-
reverse[rev];
|
| 117 |
-
|
| 118 |
-
[fwd][rev]
|
| 119 |
-
concat=n=2:v=1:a=0,
|
| 120 |
format=yuv420p[v]
|
| 121 |
"""
|
| 122 |
target_duration = str(duration * 2)
|
| 123 |
|
| 124 |
-
# PENGATURAN ENCODER
|
| 125 |
cmd = [
|
| 126 |
-
"ffmpeg",
|
| 127 |
-
"-y",
|
| 128 |
"-i", input_path,
|
| 129 |
"-filter_complex", filter_complex,
|
| 130 |
"-map", "[v]",
|
| 131 |
"-t", target_duration,
|
| 132 |
-
"-an",
|
| 133 |
"-c:v", "libx264",
|
| 134 |
-
"-preset", "
|
| 135 |
-
"-profile:v", "high",
|
| 136 |
-
"-tune", "film",
|
| 137 |
"-crf", str(crf),
|
| 138 |
-
"-pix_fmt", "yuv420p",
|
|
|
|
| 139 |
output_path
|
| 140 |
]
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
preview_html = f"""
|
| 145 |
-
<div style="
|
| 146 |
-
|
| 147 |
-
overflow:hidden;
|
| 148 |
-
background-color: #000;
|
| 149 |
-
">
|
| 150 |
-
<video
|
| 151 |
-
autoplay
|
| 152 |
-
loop
|
| 153 |
-
muted
|
| 154 |
-
controls
|
| 155 |
-
playsinline
|
| 156 |
-
width="100%"
|
| 157 |
-
style="display: block;"
|
| 158 |
-
>
|
| 159 |
<source src="/file={output_path}" type="video/mp4">
|
| 160 |
</video>
|
| 161 |
</div>
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
margin-top:10px;
|
| 165 |
-
opacity:0.7;
|
| 166 |
-
font-size:14px;
|
| 167 |
-
text-align:center;
|
| 168 |
-
">
|
| 169 |
-
Preview is looping automatically
|
| 170 |
</p>
|
| 171 |
"""
|
| 172 |
|
| 173 |
return output_path, preview_html
|
| 174 |
|
| 175 |
-
|
| 176 |
-
with gr.Blocks() as demo:
|
| 177 |
-
|
| 178 |
gr.Markdown(
|
| 179 |
"""
|
| 180 |
-
# Seamless Loop Video Maker
|
| 181 |
-
|
| 182 |
"""
|
| 183 |
)
|
| 184 |
|
| 185 |
with gr.Row():
|
| 186 |
-
|
| 187 |
with gr.Column():
|
| 188 |
-
|
| 189 |
-
input_video = gr.Video(
|
| 190 |
-
label="Upload Video",
|
| 191 |
-
sources=["upload"]
|
| 192 |
-
)
|
| 193 |
|
| 194 |
loop_method = gr.Radio(
|
| 195 |
choices=[
|
|
@@ -201,34 +149,29 @@ with gr.Blocks() as demo:
|
|
| 201 |
label="Loop Method"
|
| 202 |
)
|
| 203 |
|
| 204 |
-
gr.
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
minimum=14, maximum=30, value=18, step=1,
|
| 224 |
-
label="Quality (CRF - Lower is Better)"
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
generate_btn = gr.Button("Generate Seamless Loop", variant="primary")
|
| 228 |
|
| 229 |
with gr.Column():
|
| 230 |
-
preview = gr.HTML()
|
| 231 |
-
output_video = gr.Video(label="Rendered
|
| 232 |
|
| 233 |
generate_btn.click(
|
| 234 |
fn=create_loop,
|
|
@@ -236,9 +179,9 @@ with gr.Blocks() as demo:
|
|
| 236 |
outputs=[output_video, preview]
|
| 237 |
)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
)
|
|
|
|
| 10 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 12 |
|
|
|
|
| 13 |
def get_video_duration(video_path):
|
| 14 |
cmd = [
|
| 15 |
"ffprobe",
|
|
|
|
| 18 |
"-of", "default=noprint_wrappers=1:nokey=1",
|
| 19 |
video_path
|
| 20 |
]
|
|
|
|
| 21 |
result = subprocess.check_output(cmd).decode().strip()
|
| 22 |
return float(result)
|
| 23 |
|
|
|
|
| 24 |
def create_loop(
|
| 25 |
input_video,
|
| 26 |
loop_method,
|
|
|
|
| 36 |
input_path = os.path.join(TEMP_DIR, f"{uid}_input.mp4")
|
| 37 |
output_path = os.path.join(OUTPUT_DIR, f"{uid}_loop.mp4")
|
| 38 |
|
| 39 |
+
# Handling Gradio video input
|
| 40 |
if isinstance(input_video, dict):
|
| 41 |
video_file = input_video["path"]
|
| 42 |
else:
|
|
|
|
| 45 |
shutil.copy(video_file, input_path)
|
| 46 |
duration = get_video_duration(input_path)
|
| 47 |
|
| 48 |
+
# Flags untuk High-Quality Scaling & Anti-Aliasing
|
| 49 |
+
# Lanczos mencegah gambar "bergerigi" (aliasing)
|
| 50 |
+
sws_flags = "flags=lanczos+accurate_rnd+full_chroma_int"
|
| 51 |
|
| 52 |
+
# Filter dasar: Perbaiki resolusi (harus genap), set FPS, dan format pixel
|
| 53 |
+
base_filter = f"format=yuv420p,fps={fps}:round=near,scale=trunc(iw/2)*2:trunc(ih/2)*2:{sws_flags}"
|
| 54 |
+
|
| 55 |
if loop_method == "Crossfade (Standard)":
|
| 56 |
if duration <= fade_duration * 2:
|
| 57 |
+
raise gr.Error(f"Video too short. Min: {fade_duration * 2:.1f}s")
|
| 58 |
+
|
|
|
|
|
|
|
| 59 |
offset = duration - fade_duration
|
| 60 |
filter_complex = f"""
|
| 61 |
+
[0:v]{base_filter},split[v1][v2];
|
| 62 |
+
[v1][v2]xfade=transition={transition}:duration={fade_duration}:offset={offset},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
format=yuv420p[v]
|
| 64 |
"""
|
| 65 |
+
target_duration = str(duration - fade_duration)
|
| 66 |
|
| 67 |
elif loop_method == "Split & Swap (Premiere Style)":
|
| 68 |
midpoint = duration / 2.0
|
| 69 |
offset = midpoint - fade_duration
|
| 70 |
|
| 71 |
if offset <= 0:
|
| 72 |
+
raise gr.Error("Fade duration is too long for this video.")
|
| 73 |
+
|
|
|
|
|
|
|
| 74 |
filter_complex = f"""
|
| 75 |
+
[0:v]{base_filter},split[v_src1][v_src2];
|
| 76 |
+
[v_src1]trim=start=0:end={midpoint},setpts=PTS-STARTPTS[partA];
|
| 77 |
+
[v_src2]trim=start={midpoint}:end={duration},setpts=PTS-STARTPTS[partB];
|
| 78 |
+
[partB][partA]xfade=transition={transition}:duration={fade_duration}:offset={offset},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
format=yuv420p[v]
|
| 80 |
"""
|
| 81 |
target_duration = str(duration - fade_duration)
|
|
|
|
| 83 |
else:
|
| 84 |
# PING-PONG / BOOMERANG METHOD
|
| 85 |
filter_complex = f"""
|
| 86 |
+
[0:v]{base_filter},split[fwd][rev_src];
|
| 87 |
+
[rev_src]reverse[rev];
|
| 88 |
+
[fwd][rev]concat=n=2:v=1:a=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
format=yuv420p[v]
|
| 90 |
"""
|
| 91 |
target_duration = str(duration * 2)
|
| 92 |
|
| 93 |
+
# PENGATURAN ENCODER (Optimasi Kualitas Tinggi)
|
| 94 |
cmd = [
|
| 95 |
+
"ffmpeg", "-y",
|
|
|
|
| 96 |
"-i", input_path,
|
| 97 |
"-filter_complex", filter_complex,
|
| 98 |
"-map", "[v]",
|
| 99 |
"-t", target_duration,
|
| 100 |
+
"-an", # Hapus audio agar loop lebih fokus ke visual
|
| 101 |
"-c:v", "libx264",
|
| 102 |
+
"-preset", "slower", # Lebih lambat = kualitas kompresi lebih halus
|
| 103 |
+
"-profile:v", "high",
|
| 104 |
+
"-tune", "film", # Menjaga tekstur halus (grass/flowers)
|
| 105 |
"-crf", str(crf),
|
| 106 |
+
"-pix_fmt", "yuv420p",
|
| 107 |
+
"-movflags", "+faststart", # Optimal untuk playback browser
|
| 108 |
output_path
|
| 109 |
]
|
| 110 |
|
| 111 |
+
try:
|
| 112 |
+
subprocess.run(cmd, check=True)
|
| 113 |
+
except subprocess.CalledProcessError as e:
|
| 114 |
+
raise gr.Error(f"FFmpeg Error: {e}")
|
| 115 |
|
| 116 |
preview_html = f"""
|
| 117 |
+
<div style="border-radius:12px; overflow:hidden; background-color: #000; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.3);">
|
| 118 |
+
<video autoplay loop muted controls playsinline width="100%" style="display: block;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
<source src="/file={output_path}" type="video/mp4">
|
| 120 |
</video>
|
| 121 |
</div>
|
| 122 |
+
<p style="margin-top:10px; opacity:0.7; font-size:14px; text-align:center;">
|
| 123 |
+
✨ Seamless loop preview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
</p>
|
| 125 |
"""
|
| 126 |
|
| 127 |
return output_path, preview_html
|
| 128 |
|
| 129 |
+
# UI GRADIO
|
| 130 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 131 |
gr.Markdown(
|
| 132 |
"""
|
| 133 |
+
# 🎬 Seamless Loop Video Maker Pro
|
| 134 |
+
Membuat loop video tanpa patah dengan kualitas sinematik (Anti-Aliasing).
|
| 135 |
"""
|
| 136 |
)
|
| 137 |
|
| 138 |
with gr.Row():
|
|
|
|
| 139 |
with gr.Column():
|
| 140 |
+
input_video = gr.Video(label="Upload Video", sources=["upload"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
loop_method = gr.Radio(
|
| 143 |
choices=[
|
|
|
|
| 149 |
label="Loop Method"
|
| 150 |
)
|
| 151 |
|
| 152 |
+
with gr.Row():
|
| 153 |
+
fade_duration = gr.Slider(
|
| 154 |
+
minimum=0.1, maximum=5.0, value=1.0, step=0.1,
|
| 155 |
+
label="Fade Duration (sec)"
|
| 156 |
+
)
|
| 157 |
+
fps = gr.Dropdown(choices=[24, 30, 60], value=24, label="FPS")
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
transition = gr.Dropdown(
|
| 161 |
+
choices=["dissolve", "fade", "smoothleft", "fadeblack", "pixelize"],
|
| 162 |
+
value="dissolve",
|
| 163 |
+
label="Transition Type"
|
| 164 |
+
)
|
| 165 |
+
crf = gr.Slider(
|
| 166 |
+
minimum=12, maximum=28, value=18, step=1,
|
| 167 |
+
label="Quality (Lower is Better)"
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
generate_btn = gr.Button("🚀 Generate Seamless Loop", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
with gr.Column():
|
| 173 |
+
preview = gr.HTML(label="Loop Preview")
|
| 174 |
+
output_video = gr.Video(label="Download Rendered MP4")
|
| 175 |
|
| 176 |
generate_btn.click(
|
| 177 |
fn=create_loop,
|
|
|
|
| 179 |
outputs=[output_video, preview]
|
| 180 |
)
|
| 181 |
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
demo.launch(
|
| 184 |
+
server_name="0.0.0.0",
|
| 185 |
+
server_port=7860,
|
| 186 |
+
allowed_paths=[OUTPUT_DIR]
|
| 187 |
+
)
|