Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,31 +36,42 @@ def create_loop(
|
|
| 36 |
|
| 37 |
uid = str(uuid.uuid4())[:8]
|
| 38 |
|
| 39 |
-
input_path = os.path.join(
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
shutil.copy(input_video, input_path)
|
| 43 |
|
| 44 |
duration = get_video_duration(input_path)
|
| 45 |
|
| 46 |
-
if duration < fade_duration * 2:
|
| 47 |
raise gr.Error(
|
| 48 |
f"Video too short.\n"
|
| 49 |
-
f"Minimum duration
|
| 50 |
)
|
| 51 |
|
| 52 |
start_last = duration - fade_duration
|
| 53 |
offset = duration - (fade_duration * 2)
|
| 54 |
|
| 55 |
filter_complex = f"""
|
| 56 |
-
[0:v]split[a][b];
|
|
|
|
| 57 |
[a]setpts=PTS-STARTPTS[first];
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
[first][last]xfade=
|
| 60 |
transition={transition}:
|
| 61 |
duration={fade_duration}:
|
| 62 |
offset={offset},
|
| 63 |
-
|
| 64 |
format=yuv420p
|
| 65 |
"""
|
| 66 |
|
|
@@ -79,20 +90,27 @@ def create_loop(
|
|
| 79 |
subprocess.run(cmd, check=True)
|
| 80 |
|
| 81 |
preview_html = f"""
|
| 82 |
-
<div style="
|
|
|
|
|
|
|
|
|
|
| 83 |
<video
|
| 84 |
autoplay
|
| 85 |
loop
|
| 86 |
muted
|
| 87 |
controls
|
| 88 |
-
width="100%"
|
| 89 |
playsinline
|
|
|
|
| 90 |
>
|
| 91 |
<source src="/file={output_path}" type="video/mp4">
|
| 92 |
</video>
|
| 93 |
</div>
|
| 94 |
|
| 95 |
-
<p style="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
Preview is looping automatically
|
| 97 |
</p>
|
| 98 |
"""
|
|
@@ -100,7 +118,7 @@ def create_loop(
|
|
| 100 |
return output_path, preview_html
|
| 101 |
|
| 102 |
|
| 103 |
-
with gr.Blocks(
|
| 104 |
|
| 105 |
gr.Markdown(
|
| 106 |
"""
|
|
@@ -116,7 +134,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 116 |
|
| 117 |
input_video = gr.Video(
|
| 118 |
label="Upload Video",
|
| 119 |
-
sources=["upload"]
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
fade_duration = gr.Slider(
|
|
@@ -183,7 +202,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 183 |
)
|
| 184 |
|
| 185 |
demo.launch(
|
| 186 |
-
|
| 187 |
-
server_port=7860,
|
| 188 |
-
ssr_mode=False
|
| 189 |
)
|
|
|
|
| 36 |
|
| 37 |
uid = str(uuid.uuid4())[:8]
|
| 38 |
|
| 39 |
+
input_path = os.path.join(
|
| 40 |
+
TEMP_DIR,
|
| 41 |
+
f"{uid}_input.mp4"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
output_path = os.path.join(
|
| 45 |
+
OUTPUT_DIR,
|
| 46 |
+
f"{uid}_loop.mp4"
|
| 47 |
+
)
|
| 48 |
|
| 49 |
shutil.copy(input_video, input_path)
|
| 50 |
|
| 51 |
duration = get_video_duration(input_path)
|
| 52 |
|
| 53 |
+
if duration <= fade_duration * 2:
|
| 54 |
raise gr.Error(
|
| 55 |
f"Video too short.\n"
|
| 56 |
+
f"Minimum duration needed: {fade_duration * 2:.1f} seconds"
|
| 57 |
)
|
| 58 |
|
| 59 |
start_last = duration - fade_duration
|
| 60 |
offset = duration - (fade_duration * 2)
|
| 61 |
|
| 62 |
filter_complex = f"""
|
| 63 |
+
[0:v]fps={fps},split[a][b];
|
| 64 |
+
|
| 65 |
[a]setpts=PTS-STARTPTS[first];
|
| 66 |
+
|
| 67 |
+
[b]trim=start={start_last},
|
| 68 |
+
setpts=PTS-STARTPTS[last];
|
| 69 |
+
|
| 70 |
[first][last]xfade=
|
| 71 |
transition={transition}:
|
| 72 |
duration={fade_duration}:
|
| 73 |
offset={offset},
|
| 74 |
+
|
| 75 |
format=yuv420p
|
| 76 |
"""
|
| 77 |
|
|
|
|
| 90 |
subprocess.run(cmd, check=True)
|
| 91 |
|
| 92 |
preview_html = f"""
|
| 93 |
+
<div style="
|
| 94 |
+
border-radius:16px;
|
| 95 |
+
overflow:hidden;
|
| 96 |
+
">
|
| 97 |
<video
|
| 98 |
autoplay
|
| 99 |
loop
|
| 100 |
muted
|
| 101 |
controls
|
|
|
|
| 102 |
playsinline
|
| 103 |
+
width="100%"
|
| 104 |
>
|
| 105 |
<source src="/file={output_path}" type="video/mp4">
|
| 106 |
</video>
|
| 107 |
</div>
|
| 108 |
|
| 109 |
+
<p style="
|
| 110 |
+
margin-top:10px;
|
| 111 |
+
opacity:0.7;
|
| 112 |
+
font-size:14px;
|
| 113 |
+
">
|
| 114 |
Preview is looping automatically
|
| 115 |
</p>
|
| 116 |
"""
|
|
|
|
| 118 |
return output_path, preview_html
|
| 119 |
|
| 120 |
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
|
| 123 |
gr.Markdown(
|
| 124 |
"""
|
|
|
|
| 134 |
|
| 135 |
input_video = gr.Video(
|
| 136 |
label="Upload Video",
|
| 137 |
+
sources=["upload"],
|
| 138 |
+
type="filepath"
|
| 139 |
)
|
| 140 |
|
| 141 |
fade_duration = gr.Slider(
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
demo.launch(
|
| 205 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
| 206 |
)
|