Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import shutil
|
|
| 4 |
import subprocess
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# --- PERBAIKAN 1: Gunakan Absolute Path ---
|
| 8 |
TEMP_DIR = os.path.abspath("temp")
|
| 9 |
OUTPUT_DIR = os.path.abspath("outputs")
|
| 10 |
|
|
@@ -27,6 +26,7 @@ def get_video_duration(video_path):
|
|
| 27 |
|
| 28 |
def create_loop(
|
| 29 |
input_video,
|
|
|
|
| 30 |
fade_duration,
|
| 31 |
transition,
|
| 32 |
fps,
|
|
@@ -36,16 +36,8 @@ def create_loop(
|
|
| 36 |
raise gr.Error("Please upload a video.")
|
| 37 |
|
| 38 |
uid = str(uuid.uuid4())[:8]
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
TEMP_DIR,
|
| 42 |
-
f"{uid}_input.mp4"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
output_path = os.path.join(
|
| 46 |
-
OUTPUT_DIR,
|
| 47 |
-
f"{uid}_loop.mp4"
|
| 48 |
-
)
|
| 49 |
|
| 50 |
# Gradio compatibility
|
| 51 |
if isinstance(input_video, dict):
|
|
@@ -54,32 +46,50 @@ def create_loop(
|
|
| 54 |
video_file = input_video
|
| 55 |
|
| 56 |
shutil.copy(video_file, input_path)
|
| 57 |
-
|
| 58 |
duration = get_video_duration(input_path)
|
| 59 |
|
| 60 |
-
if
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
cmd = [
|
| 85 |
"ffmpeg",
|
|
@@ -87,7 +97,7 @@ def create_loop(
|
|
| 87 |
"-i", input_path,
|
| 88 |
"-filter_complex", filter_complex,
|
| 89 |
"-map", "[v]",
|
| 90 |
-
"-t",
|
| 91 |
"-an",
|
| 92 |
"-c:v", "libx264",
|
| 93 |
"-preset", "ultrafast",
|
|
@@ -135,8 +145,7 @@ with gr.Blocks() as demo:
|
|
| 135 |
gr.Markdown(
|
| 136 |
"""
|
| 137 |
# Seamless Loop Video Maker
|
| 138 |
-
|
| 139 |
-
Upload a video and create a cinematic seamless loop using FFmpeg.
|
| 140 |
"""
|
| 141 |
)
|
| 142 |
|
|
@@ -148,69 +157,49 @@ with gr.Blocks() as demo:
|
|
| 148 |
label="Upload Video",
|
| 149 |
sources=["upload"]
|
| 150 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
|
|
|
| 152 |
fade_duration = gr.Slider(
|
| 153 |
-
minimum=1,
|
| 154 |
-
maximum=5,
|
| 155 |
-
value=
|
| 156 |
-
step=0.
|
| 157 |
label="Fade Duration (seconds)"
|
| 158 |
)
|
|
|
|
| 159 |
|
| 160 |
transition = gr.Dropdown(
|
| 161 |
-
choices=[
|
| 162 |
-
|
| 163 |
-
"fade",
|
| 164 |
-
"smoothleft",
|
| 165 |
-
"fadeblack"
|
| 166 |
-
],
|
| 167 |
-
value="dissolve",
|
| 168 |
label="Transition"
|
| 169 |
)
|
| 170 |
|
| 171 |
-
fps = gr.Dropdown(
|
| 172 |
-
choices=[24, 30],
|
| 173 |
-
value=24,
|
| 174 |
-
label="FPS"
|
| 175 |
-
)
|
| 176 |
|
| 177 |
crf = gr.Slider(
|
| 178 |
-
minimum=18,
|
| 179 |
-
maximum=30,
|
| 180 |
-
value=24,
|
| 181 |
-
step=1,
|
| 182 |
-
label="Quality (CRF)"
|
| 183 |
)
|
| 184 |
|
| 185 |
-
generate_btn = gr.Button(
|
| 186 |
-
"Generate Seamless Loop",
|
| 187 |
-
variant="primary"
|
| 188 |
-
)
|
| 189 |
|
| 190 |
with gr.Column():
|
| 191 |
-
|
| 192 |
preview = gr.HTML()
|
| 193 |
-
|
| 194 |
-
output_video = gr.Video(
|
| 195 |
-
label="Rendered Video"
|
| 196 |
-
)
|
| 197 |
|
| 198 |
generate_btn.click(
|
| 199 |
fn=create_loop,
|
| 200 |
-
inputs=[
|
| 201 |
-
|
| 202 |
-
fade_duration,
|
| 203 |
-
transition,
|
| 204 |
-
fps,
|
| 205 |
-
crf
|
| 206 |
-
],
|
| 207 |
-
outputs=[
|
| 208 |
-
output_video,
|
| 209 |
-
preview
|
| 210 |
-
]
|
| 211 |
)
|
| 212 |
|
| 213 |
-
# --- PERBAIKAN 2: Tambahkan allowed_paths ---
|
| 214 |
demo.launch(
|
| 215 |
server_name="0.0.0.0",
|
| 216 |
server_port=7860,
|
|
|
|
| 4 |
import subprocess
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 7 |
TEMP_DIR = os.path.abspath("temp")
|
| 8 |
OUTPUT_DIR = os.path.abspath("outputs")
|
| 9 |
|
|
|
|
| 26 |
|
| 27 |
def create_loop(
|
| 28 |
input_video,
|
| 29 |
+
loop_method,
|
| 30 |
fade_duration,
|
| 31 |
transition,
|
| 32 |
fps,
|
|
|
|
| 36 |
raise gr.Error("Please upload a video.")
|
| 37 |
|
| 38 |
uid = str(uuid.uuid4())[:8]
|
| 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 |
# Gradio compatibility
|
| 43 |
if isinstance(input_video, dict):
|
|
|
|
| 46 |
video_file = input_video
|
| 47 |
|
| 48 |
shutil.copy(video_file, input_path)
|
|
|
|
| 49 |
duration = get_video_duration(input_path)
|
| 50 |
|
| 51 |
+
if loop_method == "Crossfade (Standard)":
|
| 52 |
+
if duration <= fade_duration * 2:
|
| 53 |
+
raise gr.Error(
|
| 54 |
+
f"Video too short for crossfade.\n"
|
| 55 |
+
f"Minimum duration needed: {fade_duration * 2:.1f} seconds"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
offset = duration - fade_duration
|
| 59 |
+
filter_complex = f"""
|
| 60 |
+
[0:v]
|
| 61 |
+
setpts=PTS-STARTPTS,
|
| 62 |
+
fps={fps},
|
| 63 |
+
format=yuv420p,
|
| 64 |
+
split[v1][v2];
|
| 65 |
+
|
| 66 |
+
[v1][v2]
|
| 67 |
+
xfade=
|
| 68 |
+
transition={transition}:
|
| 69 |
+
duration={fade_duration}:
|
| 70 |
+
offset={offset},
|
| 71 |
+
|
| 72 |
+
format=yuv420p[v]
|
| 73 |
+
"""
|
| 74 |
+
target_duration = str(duration)
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
# PING-PONG / BOOMERANG METHOD (No Ghosting)
|
| 78 |
+
filter_complex = f"""
|
| 79 |
+
[0:v]
|
| 80 |
+
setpts=PTS-STARTPTS,
|
| 81 |
+
fps={fps},
|
| 82 |
+
format=yuv420p,
|
| 83 |
+
split[fwd][rev_src];
|
| 84 |
+
|
| 85 |
+
[rev_src]
|
| 86 |
+
reverse[rev];
|
| 87 |
+
|
| 88 |
+
[fwd][rev]
|
| 89 |
+
concat=n=2:v=1:a=0,
|
| 90 |
+
format=yuv420p[v]
|
| 91 |
+
"""
|
| 92 |
+
target_duration = str(duration * 2)
|
| 93 |
|
| 94 |
cmd = [
|
| 95 |
"ffmpeg",
|
|
|
|
| 97 |
"-i", input_path,
|
| 98 |
"-filter_complex", filter_complex,
|
| 99 |
"-map", "[v]",
|
| 100 |
+
"-t", target_duration,
|
| 101 |
"-an",
|
| 102 |
"-c:v", "libx264",
|
| 103 |
"-preset", "ultrafast",
|
|
|
|
| 145 |
gr.Markdown(
|
| 146 |
"""
|
| 147 |
# Seamless Loop Video Maker
|
| 148 |
+
Create cinematic seamless loops. **Tip:** Use `Ping-Pong` mode for nature/scenery videos to avoid ghosting effects!
|
|
|
|
| 149 |
"""
|
| 150 |
)
|
| 151 |
|
|
|
|
| 157 |
label="Upload Video",
|
| 158 |
sources=["upload"]
|
| 159 |
)
|
| 160 |
+
|
| 161 |
+
loop_method = gr.Radio(
|
| 162 |
+
choices=["Crossfade (Standard)", "Ping-Pong (No Ghosting)"],
|
| 163 |
+
value="Ping-Pong (No Ghosting)",
|
| 164 |
+
label="Loop Method"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
gr.Markdown("*Note: Fade settings below only apply to Crossfade mode.*")
|
| 168 |
|
| 169 |
+
# --- BAGIAN YANG DIUBAH ---
|
| 170 |
fade_duration = gr.Slider(
|
| 171 |
+
minimum=0.1, # Minimum diubah ke 0.1
|
| 172 |
+
maximum=5.0,
|
| 173 |
+
value=1.0,
|
| 174 |
+
step=0.1, # Step diubah ke 0.1 agar slider bisa digeser per 0.1
|
| 175 |
label="Fade Duration (seconds)"
|
| 176 |
)
|
| 177 |
+
# --------------------------
|
| 178 |
|
| 179 |
transition = gr.Dropdown(
|
| 180 |
+
choices=["dissolve", "fade", "smoothleft", "fadeblack"],
|
| 181 |
+
value="fadeblack",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
label="Transition"
|
| 183 |
)
|
| 184 |
|
| 185 |
+
fps = gr.Dropdown(choices=[24, 30, 60], value=24, label="FPS")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
crf = gr.Slider(
|
| 188 |
+
minimum=18, maximum=30, value=24, step=1, label="Quality (CRF)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
)
|
| 190 |
|
| 191 |
+
generate_btn = gr.Button("Generate Seamless Loop", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
with gr.Column():
|
|
|
|
| 194 |
preview = gr.HTML()
|
| 195 |
+
output_video = gr.Video(label="Rendered Video")
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
generate_btn.click(
|
| 198 |
fn=create_loop,
|
| 199 |
+
inputs=[input_video, loop_method, fade_duration, transition, fps, crf],
|
| 200 |
+
outputs=[output_video, preview]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
)
|
| 202 |
|
|
|
|
| 203 |
demo.launch(
|
| 204 |
server_name="0.0.0.0",
|
| 205 |
server_port=7860,
|