nazib61 commited on
Commit
d56f850
Β·
verified Β·
1 Parent(s): b208db7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -56
app.py CHANGED
@@ -20,14 +20,16 @@ def get_video_dimensions(url):
20
  except:
21
  return 0, 0
22
 
23
- def run_scraper(platform, query, orientation="Any"):
24
- """Runs the Node.js scraper for a specific platform"""
25
  try:
26
- cmd = ["node", "scraper.js", platform, query, orientation]
 
27
  result = subprocess.run(cmd, capture_output=True, text=True, timeout=240)
28
- return [line.strip() for line in result.stdout.split('\n') if line.strip().startswith("http")]
 
29
  except Exception as e:
30
- print(f"Error running {platform}: {e}")
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
- if pinterest_urls:
47
- for i, url in enumerate(pinterest_urls):
48
- progress(0.3 + (i/len(pinterest_urls)*0.2), desc=f"Checking Pinterest Result {i+1}...")
49
-
50
- if orientation == "Any":
51
- selected_url = url; break
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: FALLBACK TO PEXELS ---
60
  if not selected_url:
61
- progress(0.6, desc=f"⚠️ No {orientation} video on Pinterest. Switching to Pexels Fallback...")
62
 
63
- # We search Pexels specifically for the orientation we need
64
  pexels_urls = run_scraper("pexels", query, orientation)
65
 
66
- if pexels_urls:
67
- # Pexels URLs are already filtered by the search query parameters, so we take the first valid one
68
- selected_url = pexels_urls[0]
69
- progress(0.7, desc="βœ… Found match on Pexels!")
70
- else:
71
- return None, f"❌ Failed on Pinterest AND Pexels. Try a different keyword."
 
 
 
 
72
 
73
  # --- PHASE 3: PROCESSING ---
74
- progress(0.8, desc="βš™οΈ Trimming & Processing...")
75
  output_filename = f"final_{uuid.uuid4().hex[:5]}.mp4"
76
 
77
- try:
78
- ffmpeg_cmd = [
79
- "ffmpeg",
80
- "-ss", str(start_time),
81
- "-to", str(end_time) if end_time > 0 else "999",
82
- "-i", selected_url,
83
- "-vf", "unsharp=3:3:1.2:3:3:0.0,format=yuv420p",
84
- "-c:v", "libx264",
85
- "-crf", "18",
86
- "-preset", "veryfast",
87
- "-y", output_filename
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
- subprocess.run(ffmpeg_cmd, check=True)
97
-
98
- source_name = "Pexels" if "pexels.com" in selected_url or "vimeo" in selected_url else "Pinterest"
99
- return output_filename, f"βœ… Success! Source: {source_name} ({orientation})"
 
100
 
101
- except Exception as e:
102
- return None, f"❌ Processing Error: {str(e)}"
 
 
103
 
104
  # --- UI ---
105
  with gr.Blocks(theme=gr.themes.Default()) as demo:
106
- gr.Markdown("# πŸ“Œ Pinterest Video Master (with Auto-Fallback)")
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():