Update app.py
Browse files
app.py
CHANGED
|
@@ -20,14 +20,16 @@ def get_video_dimensions(url):
|
|
| 20 |
except:
|
| 21 |
return 0, 0
|
| 22 |
|
| 23 |
-
def run_scraper(
|
| 24 |
-
"""
|
| 25 |
try:
|
| 26 |
-
|
|
|
|
| 27 |
result = subprocess.run(cmd, capture_output=True, text=True, timeout=240)
|
| 28 |
-
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
print(f"Error
|
| 31 |
return []
|
| 32 |
|
| 33 |
def process_video(query, orientation, start_time, end_time, mute_audio, progress=gr.Progress()):
|
|
@@ -38,73 +40,65 @@ def process_video(query, orientation, start_time, end_time, mute_audio, progress
|
|
| 38 |
|
| 39 |
# --- PHASE 1: PINTEREST SEARCH ---
|
| 40 |
progress(0.1, desc="π Searching Pinterest...")
|
| 41 |
-
|
| 42 |
-
pinterest_urls = run_scraper("pinterest", search_query)
|
| 43 |
-
selected_url = None
|
| 44 |
|
| 45 |
# Filter Pinterest Results
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
w, h = get_video_dimensions(url)
|
| 54 |
-
if orientation == "Portrait" and h > w:
|
| 55 |
-
selected_url = url; break
|
| 56 |
-
if orientation == "Landscape" and w > h:
|
| 57 |
-
selected_url = url; break
|
| 58 |
|
| 59 |
-
# --- PHASE 2:
|
| 60 |
if not selected_url:
|
| 61 |
-
progress(0.
|
| 62 |
|
| 63 |
-
# We
|
| 64 |
pexels_urls = run_scraper("pexels", query, orientation)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# --- PHASE 3: PROCESSING ---
|
| 74 |
-
progress(0.8, desc="βοΈ
|
| 75 |
output_filename = f"final_{uuid.uuid4().hex[:5]}.mp4"
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
]
|
| 89 |
-
|
| 90 |
-
if mute_audio:
|
| 91 |
-
ffmpeg_cmd.insert(-1, "-an")
|
| 92 |
-
else:
|
| 93 |
-
ffmpeg_cmd.insert(-1, "-c:a")
|
| 94 |
-
ffmpeg_cmd.insert(-1, "aac")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# --- UI ---
|
| 105 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 106 |
-
gr.Markdown("# π Pinterest
|
| 107 |
-
gr.Markdown("Searches Pinterest first. If no matching orientation is found, it automatically searches Pexels.")
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column():
|
|
|
|
| 20 |
except:
|
| 21 |
return 0, 0
|
| 22 |
|
| 23 |
+
def run_scraper(mode, query, orientation="Any"):
|
| 24 |
+
"""Helper to run the Node.js scraper in specific mode"""
|
| 25 |
try:
|
| 26 |
+
# Calls: node scraper.js [mode] [query] [orientation]
|
| 27 |
+
cmd = ["node", "scraper.js", mode, query, orientation]
|
| 28 |
result = subprocess.run(cmd, capture_output=True, text=True, timeout=240)
|
| 29 |
+
urls = [line.strip() for line in result.stdout.split('\n') if line.strip().startswith("http")]
|
| 30 |
+
return urls
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Scraper Error ({mode}): {e}")
|
| 33 |
return []
|
| 34 |
|
| 35 |
def process_video(query, orientation, start_time, end_time, mute_audio, progress=gr.Progress()):
|
|
|
|
| 40 |
|
| 41 |
# --- PHASE 1: PINTEREST SEARCH ---
|
| 42 |
progress(0.1, desc="π Searching Pinterest...")
|
| 43 |
+
all_urls = run_scraper("pinterest", search_query)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Filter Pinterest Results
|
| 46 |
+
selected_url = None
|
| 47 |
+
for i, url in enumerate(all_urls):
|
| 48 |
+
progress(0.3 + (i/len(all_urls)*0.1), desc=f"Checking Pinterest {i+1}...")
|
| 49 |
+
if orientation == "Any": selected_url = url; break
|
| 50 |
+
w, h = get_video_dimensions(url)
|
| 51 |
+
if orientation == "Portrait" and h > w: selected_url = url; break
|
| 52 |
+
if orientation == "Landscape" and w > h: selected_url = url; break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# --- PHASE 2: PEXELS FALLBACK ---
|
| 55 |
if not selected_url:
|
| 56 |
+
progress(0.5, desc=f"β οΈ No {orientation} videos on Pinterest. Switching to Pexels Fallback...")
|
| 57 |
|
| 58 |
+
# We pass the orientation to Pexels so it filters efficiently
|
| 59 |
pexels_urls = run_scraper("pexels", query, orientation)
|
| 60 |
|
| 61 |
+
for i, url in enumerate(pexels_urls):
|
| 62 |
+
progress(0.6 + (i/len(pexels_urls)*0.1), desc=f"Checking Pexels {i+1}...")
|
| 63 |
+
# Double check dimensions just to be safe
|
| 64 |
+
if orientation == "Any": selected_url = url; break
|
| 65 |
+
w, h = get_video_dimensions(url)
|
| 66 |
+
if orientation == "Portrait" and h > w: selected_url = url; break
|
| 67 |
+
if orientation == "Landscape" and w > h: selected_url = url; break
|
| 68 |
+
|
| 69 |
+
if not selected_url:
|
| 70 |
+
return None, f"β Failed. Searched Pinterest & Pexels, but found no {orientation} videos."
|
| 71 |
|
| 72 |
# --- PHASE 3: PROCESSING ---
|
| 73 |
+
progress(0.8, desc="βοΈ Processing Video...")
|
| 74 |
output_filename = f"final_{uuid.uuid4().hex[:5]}.mp4"
|
| 75 |
|
| 76 |
+
ffmpeg_cmd = [
|
| 77 |
+
"ffmpeg",
|
| 78 |
+
"-ss", str(start_time),
|
| 79 |
+
"-to", str(end_time) if end_time > 0 else "999",
|
| 80 |
+
"-i", selected_url,
|
| 81 |
+
"-vf", "unsharp=3:3:1.2:3:3:0.0,format=yuv420p",
|
| 82 |
+
"-c:v", "libx264",
|
| 83 |
+
"-crf", "18",
|
| 84 |
+
"-preset", "veryfast",
|
| 85 |
+
"-y", output_filename
|
| 86 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
if mute_audio:
|
| 89 |
+
ffmpeg_cmd.insert(-1, "-an")
|
| 90 |
+
else:
|
| 91 |
+
ffmpeg_cmd.insert(-1, "-c:a")
|
| 92 |
+
ffmpeg_cmd.insert(-1, "aac")
|
| 93 |
|
| 94 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
| 95 |
+
|
| 96 |
+
source_name = "Pinterest" if "pinimg" in selected_url else "Pexels"
|
| 97 |
+
return output_filename, f"β
Success! Source: {source_name} ({orientation})"
|
| 98 |
|
| 99 |
# --- UI ---
|
| 100 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 101 |
+
gr.Markdown("# π Pinterest + Pexels (Auto-Fallback)")
|
|
|
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column():
|