Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ def meta(v):
|
|
| 13 |
return int(d["width"]),int(d["height"]),float(a[0])/float(a[1]),float(d["duration"])
|
| 14 |
|
| 15 |
def audio_meta(a):
|
|
|
|
| 16 |
try:
|
| 17 |
r = subprocess.run(["ffprobe","-v","error","-show_entries",
|
| 18 |
"format=duration","-of","default=noprint_wrappers=1:nokey=1",a],
|
|
@@ -25,13 +26,23 @@ def download_video(url):
|
|
| 25 |
if not url: return None, "⚠️ Please enter a valid URL."
|
| 26 |
try:
|
| 27 |
d = tempfile.mkdtemp()
|
| 28 |
-
|
|
|
|
| 29 |
ydl_opts = {
|
| 30 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 31 |
'outtmpl': os.path.join(d, 'downloaded_video.%(ext)s'),
|
| 32 |
'merge_output_format': 'mp4',
|
| 33 |
'noplaylist': True,
|
| 34 |
-
'quiet': True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 37 |
ydl.extract_info(url, download=True)
|
|
@@ -94,6 +105,7 @@ def make_video(s, video_in, audio_in):
|
|
| 94 |
total_vid_dur = dur + (len(fs) / fps)
|
| 95 |
intro = os.path.join(d_s, "intro.mp4")
|
| 96 |
|
|
|
|
| 97 |
r = subprocess.run(["ffmpeg","-y","-framerate",str(fps),
|
| 98 |
"-i",os.path.join(d_s,"f_%06d.png"),
|
| 99 |
"-c:v","libx264","-crf","18","-preset","medium",
|
|
@@ -105,11 +117,13 @@ def make_video(s, video_in, audio_in):
|
|
| 105 |
v = v_path
|
| 106 |
fs = []
|
| 107 |
|
|
|
|
| 108 |
if audio_path:
|
| 109 |
a_dur = audio_meta(audio_path)
|
| 110 |
loop_args = ["-stream_loop", "-1"] if (total_vid_dur - a_dur) > 1.0 else []
|
| 111 |
|
| 112 |
if has_frames:
|
|
|
|
| 113 |
cmd = ["ffmpeg", "-y", "-i", intro, "-i", v] + loop_args + ["-i", audio_path,
|
| 114 |
"-filter_complex", "[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 115 |
"-map", "[v]", "-map", "2:a",
|
|
@@ -117,6 +131,7 @@ def make_video(s, video_in, audio_in):
|
|
| 117 |
"-c:v", "libx264", "-crf", "18", "-preset", "medium",
|
| 118 |
"-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", out]
|
| 119 |
else:
|
|
|
|
| 120 |
cmd = ["ffmpeg", "-y", "-i", v] + loop_args + ["-i", audio_path,
|
| 121 |
"-map", "0:v", "-map", "1:a",
|
| 122 |
"-t", str(total_vid_dur),
|
|
@@ -133,6 +148,7 @@ def make_video(s, video_in, audio_in):
|
|
| 133 |
return out, msg
|
| 134 |
|
| 135 |
else:
|
|
|
|
| 136 |
cmd = ["ffmpeg","-y","-i",intro,"-i",v,
|
| 137 |
"-filter_complex","[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 138 |
"-map","[v]","-map","1:a?",
|
|
@@ -149,7 +165,6 @@ def make_video(s, video_in, audio_in):
|
|
| 149 |
with gr.Blocks(title="Video Frame Editor") as demo:
|
| 150 |
gr.Markdown("# 🎬 Video Frame Editor")
|
| 151 |
|
| 152 |
-
# NEW: Collapsible YouTube Download Section
|
| 153 |
with gr.Accordion("🌐 Download Video from Web (Optional)", open=False):
|
| 154 |
with gr.Row():
|
| 155 |
url_input = gr.Textbox(label="YouTube / Web Video URL", placeholder="https://www.youtube.com/watch?v=...", scale=3)
|
|
@@ -176,7 +191,7 @@ with gr.Blocks(title="Video Frame Editor") as demo:
|
|
| 176 |
output=gr.Video(label="Final Video")
|
| 177 |
output_status=gr.Textbox(label="Output Status")
|
| 178 |
|
| 179 |
-
# Link
|
| 180 |
dl_btn.click(
|
| 181 |
fn=download_video,
|
| 182 |
inputs=[url_input],
|
|
|
|
| 13 |
return int(d["width"]),int(d["height"]),float(a[0])/float(a[1]),float(d["duration"])
|
| 14 |
|
| 15 |
def audio_meta(a):
|
| 16 |
+
"""Fetches the duration of the audio file in seconds."""
|
| 17 |
try:
|
| 18 |
r = subprocess.run(["ffprobe","-v","error","-show_entries",
|
| 19 |
"format=duration","-of","default=noprint_wrappers=1:nokey=1",a],
|
|
|
|
| 26 |
if not url: return None, "⚠️ Please enter a valid URL."
|
| 27 |
try:
|
| 28 |
d = tempfile.mkdtemp()
|
| 29 |
+
|
| 30 |
+
# Upgraded options to bypass YouTube SSL/Throttling drops
|
| 31 |
ydl_opts = {
|
| 32 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 33 |
'outtmpl': os.path.join(d, 'downloaded_video.%(ext)s'),
|
| 34 |
'merge_output_format': 'mp4',
|
| 35 |
'noplaylist': True,
|
| 36 |
+
'quiet': True,
|
| 37 |
+
# --- ANTI-SSL-DROP SETTINGS ---
|
| 38 |
+
'source_address': '0.0.0.0', # Forces IPv4 (fixes 90% of YouTube EOF errors)
|
| 39 |
+
'nocheckcertificate': True, # Ignores local network SSL tampering
|
| 40 |
+
'extractor_retries': 5, # Try harder on disconnects
|
| 41 |
+
'retries': 10, # Retry file drops
|
| 42 |
+
'fragment_retries': 10,
|
| 43 |
+
'http_headers': { # Pretend to be a normal Chrome browser
|
| 44 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 45 |
+
}
|
| 46 |
}
|
| 47 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 48 |
ydl.extract_info(url, download=True)
|
|
|
|
| 105 |
total_vid_dur = dur + (len(fs) / fps)
|
| 106 |
intro = os.path.join(d_s, "intro.mp4")
|
| 107 |
|
| 108 |
+
# 1. Create intro block
|
| 109 |
r = subprocess.run(["ffmpeg","-y","-framerate",str(fps),
|
| 110 |
"-i",os.path.join(d_s,"f_%06d.png"),
|
| 111 |
"-c:v","libx264","-crf","18","-preset","medium",
|
|
|
|
| 117 |
v = v_path
|
| 118 |
fs = []
|
| 119 |
|
| 120 |
+
# 2. Combine Logic
|
| 121 |
if audio_path:
|
| 122 |
a_dur = audio_meta(audio_path)
|
| 123 |
loop_args = ["-stream_loop", "-1"] if (total_vid_dur - a_dur) > 1.0 else []
|
| 124 |
|
| 125 |
if has_frames:
|
| 126 |
+
# Scenario A: Frames + Audio Replacement
|
| 127 |
cmd = ["ffmpeg", "-y", "-i", intro, "-i", v] + loop_args + ["-i", audio_path,
|
| 128 |
"-filter_complex", "[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 129 |
"-map", "[v]", "-map", "2:a",
|
|
|
|
| 131 |
"-c:v", "libx264", "-crf", "18", "-preset", "medium",
|
| 132 |
"-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", out]
|
| 133 |
else:
|
| 134 |
+
# Scenario B: NO Frames + Audio Replacement ONLY
|
| 135 |
cmd = ["ffmpeg", "-y", "-i", v] + loop_args + ["-i", audio_path,
|
| 136 |
"-map", "0:v", "-map", "1:a",
|
| 137 |
"-t", str(total_vid_dur),
|
|
|
|
| 148 |
return out, msg
|
| 149 |
|
| 150 |
else:
|
| 151 |
+
# Scenario C: Frames added + Keep Original Audio
|
| 152 |
cmd = ["ffmpeg","-y","-i",intro,"-i",v,
|
| 153 |
"-filter_complex","[0:v][1:v]concat=n=2:v=1:a=0[v]",
|
| 154 |
"-map","[v]","-map","1:a?",
|
|
|
|
| 165 |
with gr.Blocks(title="Video Frame Editor") as demo:
|
| 166 |
gr.Markdown("# 🎬 Video Frame Editor")
|
| 167 |
|
|
|
|
| 168 |
with gr.Accordion("🌐 Download Video from Web (Optional)", open=False):
|
| 169 |
with gr.Row():
|
| 170 |
url_input = gr.Textbox(label="YouTube / Web Video URL", placeholder="https://www.youtube.com/watch?v=...", scale=3)
|
|
|
|
| 191 |
output=gr.Video(label="Final Video")
|
| 192 |
output_status=gr.Textbox(label="Output Status")
|
| 193 |
|
| 194 |
+
# Link download button
|
| 195 |
dl_btn.click(
|
| 196 |
fn=download_video,
|
| 197 |
inputs=[url_input],
|