digidau commited on
Commit
45b6e45
·
verified ·
1 Parent(s): df25281

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import shutil
4
+ import subprocess
5
+ import gradio as gr
6
+
7
+ TEMP_DIR = "temp"
8
+ OUTPUT_DIR = "outputs"
9
+
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",
17
+ "-v", "error",
18
+ "-show_entries", "format=duration",
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
+ fade_duration,
30
+ transition,
31
+ fps,
32
+ crf
33
+ ):
34
+ if input_video is None:
35
+ raise gr.Error("Please upload a video.")
36
+
37
+ uid = str(uuid.uuid4())[:8]
38
+
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
+ 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 for {fade_duration}s fade is {fade_duration * 2}s."
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
+ [b]trim=start={start_last},setpts=PTS-STARTPTS[last];
59
+ [first][last]xfade=
60
+ transition={transition}:
61
+ duration={fade_duration}:
62
+ offset={offset},
63
+ fps={fps},
64
+ format=yuv420p
65
+ """
66
+
67
+ cmd = [
68
+ "ffmpeg",
69
+ "-y",
70
+ "-i", input_path,
71
+ "-filter_complex", filter_complex,
72
+ "-an",
73
+ "-c:v", "libx264",
74
+ "-preset", "ultrafast",
75
+ "-crf", str(crf),
76
+ output_path
77
+ ]
78
+
79
+ subprocess.run(cmd, check=True)
80
+
81
+ preview_html = f"""
82
+ <div style="border-radius:16px; overflow:hidden;">
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="margin-top:10px; opacity:0.7;">
96
+ Preview is looping automatically
97
+ </p>
98
+ """
99
+
100
+ return output_path, preview_html
101
+
102
+
103
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
104
+
105
+ gr.Markdown(
106
+ """
107
+ # Seamless Loop Video Maker
108
+
109
+ Upload a video and create a smooth cinematic seamless loop using FFmpeg.
110
+ """
111
+ )
112
+
113
+ with gr.Row():
114
+
115
+ with gr.Column():
116
+
117
+ input_video = gr.Video(
118
+ label="Upload Video",
119
+ sources=["upload"]
120
+ )
121
+
122
+ fade_duration = gr.Slider(
123
+ minimum=1,
124
+ maximum=5,
125
+ value=2,
126
+ step=0.5,
127
+ label="Fade Duration (seconds)"
128
+ )
129
+
130
+ transition = gr.Dropdown(
131
+ choices=[
132
+ "dissolve",
133
+ "fade",
134
+ "smoothleft",
135
+ "fadeblack"
136
+ ],
137
+ value="dissolve",
138
+ label="Transition"
139
+ )
140
+
141
+ fps = gr.Dropdown(
142
+ choices=[24, 30],
143
+ value=24,
144
+ label="FPS"
145
+ )
146
+
147
+ crf = gr.Slider(
148
+ minimum=18,
149
+ maximum=30,
150
+ value=24,
151
+ step=1,
152
+ label="Quality (CRF)"
153
+ )
154
+
155
+ generate_btn = gr.Button(
156
+ "Generate Seamless Loop",
157
+ variant="primary"
158
+ )
159
+
160
+ with gr.Column():
161
+
162
+ preview = gr.HTML(
163
+ label="Loop Preview"
164
+ )
165
+
166
+ output_video = gr.Video(
167
+ label="Rendered Video"
168
+ )
169
+
170
+ generate_btn.click(
171
+ fn=create_loop,
172
+ inputs=[
173
+ input_video,
174
+ fade_duration,
175
+ transition,
176
+ fps,
177
+ crf
178
+ ],
179
+ outputs=[
180
+ output_video,
181
+ preview
182
+ ]
183
+ )
184
+
185
+ demo.launch(
186
+ server_name="0.0.0.0",
187
+ server_port=7860,
188
+ ssr_mode=False
189
+ )