Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,16 @@ def meta(v):
|
|
| 11 |
a=d["r_frame_rate"].split("/")
|
| 12 |
return int(d["width"]),int(d["height"]),float(a[0])/float(a[1]),float(d["duration"])
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def save_frames(v,t,n):
|
| 15 |
v=p(v)
|
| 16 |
if not v:return [],None,"Upload a video."
|
|
@@ -27,8 +37,9 @@ def save_frames(v,t,n):
|
|
| 27 |
fs=sorted(glob.glob(os.path.join(d,"f_*.png")))
|
| 28 |
if not fs:return [],None,"No frames found."
|
| 29 |
|
|
|
|
| 30 |
return [(f,f"Frame {i+1}") for i,f in enumerate(fs)],\
|
| 31 |
-
{"v":v,"d":d,"fs":fs,"fps":fps,"w":w,"h":h},\
|
| 32 |
f"β
Extracted {len(fs)} frame(s) from {t:.3f}s"
|
| 33 |
except Exception as e:return [],None,str(e)
|
| 34 |
|
|
@@ -39,41 +50,72 @@ def extract_last(v,n):
|
|
| 39 |
n=int(n); w,h,fps,dur=meta(v)
|
| 40 |
if n<1:return [],None,"Number of frames must be at least 1."
|
| 41 |
|
| 42 |
-
# Start exactly far enough back to capture the requested
|
| 43 |
-
# number of consecutive frames ending at the last frame.
|
| 44 |
t=max(0,dur-(n/fps))
|
| 45 |
-
|
| 46 |
return save_frames(v,t,n)
|
| 47 |
except Exception as e:return [],None,str(e)
|
| 48 |
|
| 49 |
-
def make_video(s):
|
| 50 |
if not s:return None,"Extract frames first."
|
| 51 |
try:
|
| 52 |
-
|
|
|
|
| 53 |
intro=os.path.join(d,"intro.mp4")
|
| 54 |
out=os.path.join(d,f"final_{int(time.time())}.mp4")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
r=subprocess.run(["ffmpeg","-y","-framerate",str(fps),
|
| 57 |
"-i",os.path.join(d,"f_%06d.png"),
|
| 58 |
"-c:v","libx264","-crf","18","-preset","medium",
|
| 59 |
"-pix_fmt","yuv420p",intro],capture_output=True,text=True)
|
| 60 |
if r.returncode:return None,r.stderr
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except Exception as e:return None,str(e)
|
| 72 |
|
| 73 |
with gr.Blocks(title="Video Frame Editor") as demo:
|
| 74 |
gr.Markdown("# π¬ Video Frame Editor")
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
start=gr.Number(value=0,label="Start Time (seconds)")
|
|
@@ -87,7 +129,7 @@ with gr.Blocks(title="Video Frame Editor") as demo:
|
|
| 87 |
state=gr.State()
|
| 88 |
status=gr.Textbox(label="Status")
|
| 89 |
|
| 90 |
-
copy_btn=gr.Button("π
|
| 91 |
output=gr.Video(label="Final Video")
|
| 92 |
output_status=gr.Textbox(label="Output Status")
|
| 93 |
|
|
@@ -105,7 +147,7 @@ with gr.Blocks(title="Video Frame Editor") as demo:
|
|
| 105 |
|
| 106 |
copy_btn.click(
|
| 107 |
fn=make_video,
|
| 108 |
-
inputs=state,
|
| 109 |
outputs=[output,output_status]
|
| 110 |
)
|
| 111 |
|
|
|
|
| 11 |
a=d["r_frame_rate"].split("/")
|
| 12 |
return int(d["width"]),int(d["height"]),float(a[0])/float(a[1]),float(d["duration"])
|
| 13 |
|
| 14 |
+
def audio_meta(a):
|
| 15 |
+
"""Fetches the duration of the audio file in seconds."""
|
| 16 |
+
try:
|
| 17 |
+
r = subprocess.run(["ffprobe","-v","error","-show_entries",
|
| 18 |
+
"format=duration","-of","default=noprint_wrappers=1:nokey=1",a],
|
| 19 |
+
capture_output=True,text=True,check=True)
|
| 20 |
+
return float(r.stdout.strip())
|
| 21 |
+
except Exception:
|
| 22 |
+
return 0.0
|
| 23 |
+
|
| 24 |
def save_frames(v,t,n):
|
| 25 |
v=p(v)
|
| 26 |
if not v:return [],None,"Upload a video."
|
|
|
|
| 37 |
fs=sorted(glob.glob(os.path.join(d,"f_*.png")))
|
| 38 |
if not fs:return [],None,"No frames found."
|
| 39 |
|
| 40 |
+
# Added "dur":dur to the state output
|
| 41 |
return [(f,f"Frame {i+1}") for i,f in enumerate(fs)],\
|
| 42 |
+
{"v":v,"d":d,"fs":fs,"fps":fps,"w":w,"h":h, "dur":dur},\
|
| 43 |
f"β
Extracted {len(fs)} frame(s) from {t:.3f}s"
|
| 44 |
except Exception as e:return [],None,str(e)
|
| 45 |
|
|
|
|
| 50 |
n=int(n); w,h,fps,dur=meta(v)
|
| 51 |
if n<1:return [],None,"Number of frames must be at least 1."
|
| 52 |
|
|
|
|
|
|
|
| 53 |
t=max(0,dur-(n/fps))
|
|
|
|
| 54 |
return save_frames(v,t,n)
|
| 55 |
except Exception as e:return [],None,str(e)
|
| 56 |
|
| 57 |
+
def make_video(s, audio_in):
|
| 58 |
if not s:return None,"Extract frames first."
|
| 59 |
try:
|
| 60 |
+
# Added extraction of original video dur
|
| 61 |
+
d,v,fs,fps,w,h,dur=s["d"],s["v"],s["fs"],s["fps"],s["w"],s["h"],s["dur"]
|
| 62 |
intro=os.path.join(d,"intro.mp4")
|
| 63 |
out=os.path.join(d,f"final_{int(time.time())}.mp4")
|
| 64 |
|
| 65 |
+
# Total combined video duration (Intro duration + Original Video duration)
|
| 66 |
+
total_vid_dur = dur + (len(fs) / fps)
|
| 67 |
+
|
| 68 |
+
# 1. Create intro block
|
| 69 |
r=subprocess.run(["ffmpeg","-y","-framerate",str(fps),
|
| 70 |
"-i",os.path.join(d,"f_%06d.png"),
|
| 71 |
"-c:v","libx264","-crf","18","-preset","medium",
|
| 72 |
"-pix_fmt","yuv420p",intro],capture_output=True,text=True)
|
| 73 |
if r.returncode:return None,r.stderr
|
| 74 |
|
| 75 |
+
audio_path = p(audio_in)
|
| 76 |
+
|
| 77 |
+
# 2. Combine and inject logic
|
| 78 |
+
if audio_path:
|
| 79 |
+
a_dur = audio_meta(audio_path)
|
| 80 |
+
|
| 81 |
+
# Loop audio if the gap is larger than 1s, otherwise don't
|
| 82 |
+
loop_args = ["-stream_loop", "-1"] if (total_vid_dur - a_dur) > 1.0 else []
|
| 83 |
+
|
| 84 |
+
cmd = ["ffmpeg", "-y", "-i", intro, "-i", v] + loop_args + ["-i", audio_path,
|
| 85 |
+
"-filter_complex", "[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 86 |
+
"-map", "[v]", "-map", "2:a",
|
| 87 |
+
"-t", str(total_vid_dur),
|
| 88 |
+
"-c:v", "libx264", "-crf", "18", "-preset", "medium",
|
| 89 |
+
"-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", out]
|
| 90 |
+
|
| 91 |
+
r = subprocess.run(cmd, capture_output=True, text=True)
|
| 92 |
+
if r.returncode: return None, r.stderr
|
| 93 |
+
|
| 94 |
+
msg = f"β
Combined final video. "
|
| 95 |
+
if loop_args: msg += f"Audio looped (was {total_vid_dur - a_dur:.1f}s short)."
|
| 96 |
+
else: msg += "Audio matched/trimmed to video length without looping."
|
| 97 |
+
return out, msg
|
| 98 |
+
|
| 99 |
+
else:
|
| 100 |
+
# Fallback when no audio is uploaded (keeps original video's audio)
|
| 101 |
+
r=subprocess.run(["ffmpeg","-y","-i",intro,"-i",v,
|
| 102 |
+
"-filter_complex","[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 103 |
+
"-map","[v]","-map","1:a?",
|
| 104 |
+
"-c:v","libx264","-crf","18","-preset","medium",
|
| 105 |
+
"-c:a","copy","-movflags","+faststart",out],
|
| 106 |
+
capture_output=True,text=True)
|
| 107 |
+
|
| 108 |
+
if r.returncode:return None,r.stderr
|
| 109 |
+
return out, f"β
Added {len(fs)} frame(s) to beginning."
|
| 110 |
+
|
| 111 |
except Exception as e:return None,str(e)
|
| 112 |
|
| 113 |
with gr.Blocks(title="Video Frame Editor") as demo:
|
| 114 |
gr.Markdown("# π¬ Video Frame Editor")
|
| 115 |
|
| 116 |
+
with gr.Row():
|
| 117 |
+
video=gr.Video(label="Input Video")
|
| 118 |
+
audio_in=gr.Audio(type="filepath", label="Upload New Final Audio (Optional)")
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
start=gr.Number(value=0,label="Start Time (seconds)")
|
|
|
|
| 129 |
state=gr.State()
|
| 130 |
status=gr.Textbox(label="Status")
|
| 131 |
|
| 132 |
+
copy_btn=gr.Button("π Compile Final Video",variant="primary")
|
| 133 |
output=gr.Video(label="Final Video")
|
| 134 |
output_status=gr.Textbox(label="Output Status")
|
| 135 |
|
|
|
|
| 147 |
|
| 148 |
copy_btn.click(
|
| 149 |
fn=make_video,
|
| 150 |
+
inputs=[state, audio_in],
|
| 151 |
outputs=[output,output_status]
|
| 152 |
)
|
| 153 |
|