nazib61 commited on
Commit
b331259
Β·
verified Β·
1 Parent(s): dc87db2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -44
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 check_orientation(url):
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: return "Portrait"
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 across platforms...")
29
  try:
30
- result = subprocess.run(["node", "scraper.js", query], capture_output=True, text=True, timeout=300)
 
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
- current_source = ""
39
 
40
- # --- STEP 1: Try Pinterest ---
41
- progress(0.2, desc=f"Checking Pinterest for {target_orientation}...")
42
  for url in pinterest_links:
43
- ori = check_orientation(url)
44
- if target_orientation == "Any" or ori == target_orientation:
45
- final_url = url
46
- current_source = "Pinterest"
47
  break
48
 
49
- # --- STEP 2: Automatic Fallback to Pexels ---
50
  if not final_url:
51
- progress(0.5, desc=f"❌ No {target_orientation} on Pinterest. Falling back to Pexels...")
52
- for url in pexels_links:
53
- ori = check_orientation(url)
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"❌ Checked both Pinterest and Pexels. No {target_orientation} videos found for '{query}'."
61
 
62
- # --- STEP 3: Process the winner ---
63
- progress(0.9, desc=f"βš™οΈ Found on {current_source}! Processing...")
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"βœ… Success! | Source: {current_source} | Mode: {target_orientation}"
75
 
76
  except Exception as e:
77
  return None, f"❌ Error: {str(e)}"
78
 
 
 
 
79
  # --- UI ---
80
- with gr.Blocks(theme=gr.themes.Default()) as demo:
81
- gr.Markdown("# πŸš€ AI Video Scraper & Editor")
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="Search Query", placeholder="e.g. Cyberpunk City")
87
- o = gr.Dropdown(choices=["Any", "Portrait", "Landscape"], value="Portrait", label="Orientation Preference")
88
  with gr.Row():
89
- s = gr.Number(label="Start Sec", value=0)
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="Result Video")
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
- if __name__ == "__main__":
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()