Update app.py
Browse files
app.py
CHANGED
|
@@ -7,60 +7,48 @@ import json
|
|
| 7 |
if not os.path.exists("node_modules"):
|
| 8 |
subprocess.run(["npm", "install"], check=True)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
try:
|
| 12 |
-
cmd = [
|
| 13 |
-
"ffprobe", "-v", "error", "-select_streams", "v:0",
|
| 14 |
-
"-show_entries", "stream=width,height", "-of", "json", url
|
| 15 |
-
]
|
| 16 |
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
|
| 17 |
data = json.loads(result.stdout)
|
| 18 |
w, h = data['streams'][0]['width'], data['streams'][0]['height']
|
| 19 |
-
if h > w
|
| 20 |
-
if w > h: return "Landscape"
|
| 21 |
-
return "Square"
|
| 22 |
except:
|
| 23 |
return "Unknown"
|
| 24 |
|
| 25 |
def process_video(query, target_orientation, start, end, mute, progress=gr.Progress()):
|
| 26 |
if not query: return None, "β οΈ Enter a query"
|
| 27 |
|
| 28 |
-
progress(0, desc="π Searching
|
| 29 |
try:
|
| 30 |
-
|
|
|
|
| 31 |
raw_lines = result.stdout.split('\n')
|
| 32 |
|
| 33 |
-
# Separate results by source
|
| 34 |
pinterest_links = [l.split('|')[1] for l in raw_lines if l.startswith("SOURCE_PINTEREST")]
|
| 35 |
pexels_links = [l.split('|')[1] for l in raw_lines if l.startswith("SOURCE_PEXELS")]
|
| 36 |
|
| 37 |
final_url = None
|
| 38 |
-
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
progress(0.2, desc=f"Checking Pinterest for {target_orientation}...")
|
| 42 |
for url in pinterest_links:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
final_url = url
|
| 46 |
-
current_source = "Pinterest"
|
| 47 |
break
|
| 48 |
|
| 49 |
-
#
|
| 50 |
if not final_url:
|
| 51 |
-
progress(0.5, desc=
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if target_orientation == "Any" or ori == target_orientation:
|
| 55 |
-
final_url = url
|
| 56 |
-
current_source = "Pexels (Fallback)"
|
| 57 |
-
break
|
| 58 |
|
| 59 |
if not final_url:
|
| 60 |
-
return None, f"β
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
progress(0.9, desc=f"βοΈ Found on {
|
| 64 |
out_file = f"final_{uuid.uuid4().hex[:5]}.mp4"
|
| 65 |
ffmpeg_cmd = [
|
| 66 |
"ffmpeg", "-ss", str(start), "-to", str(end) if end > 0 else "999",
|
|
@@ -71,31 +59,27 @@ def process_video(query, target_orientation, start, end, mute, progress=gr.Progr
|
|
| 71 |
else: ffmpeg_cmd.insert(-1, "-c:a"); ffmpeg_cmd.insert(-1, "aac")
|
| 72 |
|
| 73 |
subprocess.run(ffmpeg_cmd, check=True)
|
| 74 |
-
return out_file, f"β
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
return None, f"β Error: {str(e)}"
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
# --- UI ---
|
| 80 |
-
with gr.Blocks(
|
| 81 |
-
gr.Markdown("# π AI Video Scraper
|
| 82 |
-
gr.Markdown("**Smart Fallback:** If Pinterest doesn't have your Portrait/Landscape choice, it auto-switches to Pexels.")
|
| 83 |
-
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column():
|
| 86 |
-
q = gr.Textbox(label="
|
| 87 |
-
o = gr.Dropdown(choices=["
|
| 88 |
with gr.Row():
|
| 89 |
-
s = gr.Number(label="Start
|
| 90 |
-
e = gr.Number(label="End Sec", value=5)
|
| 91 |
mute = gr.Checkbox(label="Mute Audio")
|
| 92 |
btn = gr.Button("Find & Download", variant="primary")
|
| 93 |
-
|
| 94 |
with gr.Column():
|
| 95 |
-
out_v = gr.Video(label="
|
| 96 |
-
out_s = gr.Textbox(label="Status / Source Info")
|
| 97 |
-
|
| 98 |
btn.click(process_video, [q, o, s, e, mute], [out_v, out_s])
|
| 99 |
|
| 100 |
-
|
| 101 |
-
demo.launch()
|
|
|
|
| 7 |
if not os.path.exists("node_modules"):
|
| 8 |
subprocess.run(["npm", "install"], check=True)
|
| 9 |
|
| 10 |
+
def get_video_info(url):
|
| 11 |
try:
|
| 12 |
+
cmd = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "json", url]
|
|
|
|
|
|
|
|
|
|
| 13 |
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
|
| 14 |
data = json.loads(result.stdout)
|
| 15 |
w, h = data['streams'][0]['width'], data['streams'][0]['height']
|
| 16 |
+
return "Portrait" if h > w else "Landscape"
|
|
|
|
|
|
|
| 17 |
except:
|
| 18 |
return "Unknown"
|
| 19 |
|
| 20 |
def process_video(query, target_orientation, start, end, mute, progress=gr.Progress()):
|
| 21 |
if not query: return None, "β οΈ Enter a query"
|
| 22 |
|
| 23 |
+
progress(0, desc=f"π Searching for {target_orientation} videos...")
|
| 24 |
try:
|
| 25 |
+
# Pass both query and orientation to Node
|
| 26 |
+
result = subprocess.run(["node", "scraper.js", query, target_orientation], capture_output=True, text=True, timeout=300)
|
| 27 |
raw_lines = result.stdout.split('\n')
|
| 28 |
|
|
|
|
| 29 |
pinterest_links = [l.split('|')[1] for l in raw_lines if l.startswith("SOURCE_PINTEREST")]
|
| 30 |
pexels_links = [l.split('|')[1] for l in raw_lines if l.startswith("SOURCE_PEXELS")]
|
| 31 |
|
| 32 |
final_url = None
|
| 33 |
+
source_name = ""
|
| 34 |
|
| 35 |
+
# 1. Try Pinterest Results (Manual check)
|
|
|
|
| 36 |
for url in pinterest_links:
|
| 37 |
+
if target_orientation == "Any" or check_orientation(url) == target_orientation:
|
| 38 |
+
final_url, source_name = url, "Pinterest"
|
|
|
|
|
|
|
| 39 |
break
|
| 40 |
|
| 41 |
+
# 2. Fallback to Pexels (Guaranteed orientation via URL filter)
|
| 42 |
if not final_url:
|
| 43 |
+
progress(0.5, desc="Pinterest failed. Checking Pexels...")
|
| 44 |
+
if pexels_links:
|
| 45 |
+
final_url, source_name = pexels_links[0], "Pexels (Fallback)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
if not final_url:
|
| 48 |
+
return None, f"β No {target_orientation} videos found on Pinterest or Pexels for '{query}'."
|
| 49 |
|
| 50 |
+
# 3. Process
|
| 51 |
+
progress(0.9, desc=f"βοΈ Found on {source_name}! Processing...")
|
| 52 |
out_file = f"final_{uuid.uuid4().hex[:5]}.mp4"
|
| 53 |
ffmpeg_cmd = [
|
| 54 |
"ffmpeg", "-ss", str(start), "-to", str(end) if end > 0 else "999",
|
|
|
|
| 59 |
else: ffmpeg_cmd.insert(-1, "-c:a"); ffmpeg_cmd.insert(-1, "aac")
|
| 60 |
|
| 61 |
subprocess.run(ffmpeg_cmd, check=True)
|
| 62 |
+
return out_file, f"β
Found on {source_name}!"
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
return None, f"β Error: {str(e)}"
|
| 66 |
|
| 67 |
+
def check_orientation(url):
|
| 68 |
+
return get_video_info(url)
|
| 69 |
+
|
| 70 |
# --- UI ---
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("# π Ultimate AI Video Scraper")
|
|
|
|
|
|
|
| 73 |
with gr.Row():
|
| 74 |
with gr.Column():
|
| 75 |
+
q = gr.Textbox(label="Query", placeholder="crowded city aerial view")
|
| 76 |
+
o = gr.Dropdown(choices=["Portrait", "Landscape"], value="Portrait", label="Orientation")
|
| 77 |
with gr.Row():
|
| 78 |
+
s, e = gr.Number(label="Start", value=0), gr.Number(label="End", value=5)
|
|
|
|
| 79 |
mute = gr.Checkbox(label="Mute Audio")
|
| 80 |
btn = gr.Button("Find & Download", variant="primary")
|
|
|
|
| 81 |
with gr.Column():
|
| 82 |
+
out_v, out_s = gr.Video(), gr.Textbox(label="Status")
|
|
|
|
|
|
|
| 83 |
btn.click(process_video, [q, o, s, e, mute], [out_v, out_s])
|
| 84 |
|
| 85 |
+
demo.launch()
|
|
|