File size: 7,660 Bytes
b4a9f77 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | """
Viral Clip Extractor - Python Client Example
Usage: python python-client.py
"""
import requests
import json
# Configuration
API_BASE_URL = "https://your-api.vercel.app" # Replace with your API URL
def analyze_video(url: str, num_clips: int = 5, clip_length: int = 40, quality: str = "720"):
"""
Analyze a YouTube video for viral clips
Args:
url: YouTube video URL
num_clips: Number of clips to return
clip_length: Target clip length in seconds
quality: Video quality (360, 720, 1080)
"""
endpoint = f"{API_BASE_URL}/analyze"
payload = {
"url": url,
"num_clips": num_clips,
"clip_length": clip_length,
"quality": quality
}
print(f"π Analyzing: {url}")
print(f" Clips: {num_clips} | Length: {clip_length}s | Quality: {quality}p")
print("-" * 60)
try:
response = requests.post(endpoint, json=payload, timeout=60)
response.raise_for_status()
data = response.json()
if not data.get("success"):
print(f"β Error: {data.get('error')}")
return None
print(f"β
Found {len(data['clips'])} viral clips!")
print(f"πΉ Video: {data['video_title']}")
print()
for i, clip in enumerate(data['clips'], 1):
print(f"CLIP #{i}")
print(f" Score: {clip['viral_score']}% viral")
print(f" Time: {clip['start_formatted']} - {clip['end_formatted']}")
print(f" Reasons: {', '.join(clip['reasons'])}")
print(f" Preview: {clip['transcript_preview'][:80]}...")
print(f" URL: {clip['youtube_url']}")
print()
return data
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
return None
def extract_video_info(url: str):
"""Extract basic video information"""
endpoint = f"{API_BASE_URL}/extract"
print(f"πΉ Extracting info: {url}")
try:
response = requests.post(endpoint, json={"url": url}, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("success"):
info = data["data"]
print(f"β
Title: {info['title']}")
print(f" Channel: {info['uploader']}")
print(f" Duration: {info['duration']}s")
print(f" Views: {info.get('view_count', 'N/A')}")
return info
else:
print(f"β Error: {data.get('error')}")
return None
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
return None
def get_transcript(url: str, start: int = 0, end: int = 60):
"""Get transcript for a specific time range"""
endpoint = f"{API_BASE_URL}/transcript"
payload = {
"url": url,
"start": start,
"end": end
}
print(f"π Getting transcript: {start}s - {end}s")
try:
response = requests.post(endpoint, json=payload, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("success"):
print(f"β
Transcript ({data['word_count']} words):")
print(f" {data['transcript'][:200]}...")
return data
else:
print(f"β Error: {data.get('error')}")
return None
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
return None
def score_clip(url: str, start: str, end: str):
"""Score a specific time range for viral potential"""
endpoint = f"{API_BASE_URL}/score"
payload = {
"url": url,
"start": start,
"end": end
}
print(f"π― Scoring clip: {start} - {end}")
try:
response = requests.post(endpoint, json=payload, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("success"):
clip = data["clip"]
print(f"β
Viral Score: {clip['viral_score']}%")
print(f" Reasons: {', '.join(clip['reasons'])}")
print(f" Words: {clip['word_count']}")
return clip
else:
print(f"β Error: {data.get('error')}")
return None
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
return None
def get_download_url(url: str, start: str, end: str, quality: str = "720"):
"""Get download URL for a clip"""
endpoint = f"{API_BASE_URL}/download"
payload = {
"url": url,
"start": start,
"end": end,
"quality": quality
}
print(f"β¬οΈ Getting download URL: {start} - {end}")
try:
response = requests.post(endpoint, json=payload, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("success"):
info = data["download_info"]
print(f"β
Download ready!")
print(f" URL: {info['download_url'][:80]}...")
print(f" Formats available: {len(info['formats'])}")
return info
else:
print(f"β Error: {data.get('error')}")
return None
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
return None
def batch_analyze(urls: list, num_clips: int = 3):
"""Analyze multiple videos in batch"""
results = []
print(f"π Batch analyzing {len(urls)} videos...")
print("=" * 60)
for i, url in enumerate(urls, 1):
print(f"\n[{i}/{len(urls)}]")
result = analyze_video(url, num_clips=num_clips)
if result:
results.append(result)
print("\n" + "=" * 60)
print(f"β
Batch complete! Analyzed {len(results)} videos")
return results
# Example usage
if __name__ == "__main__":
# Replace with your API URL
API_BASE_URL = input("Enter your API base URL: ").strip()
# Example YouTube URL (replace with actual URL)
test_url = input("Enter YouTube URL: ").strip()
print("\n" + "=" * 60)
print("VIRAL CLIP EXTRACTOR - Python Client")
print("=" * 60 + "\n")
# Menu
print("Choose an option:")
print("1. Full Analysis (find viral clips)")
print("2. Extract Video Info")
print("3. Get Transcript")
print("4. Score Specific Range")
print("5. Get Download URL")
choice = input("\nEnter choice (1-5): ").strip()
if choice == "1":
num = int(input("Number of clips (default 5): ") or "5")
length = int(input("Clip length in seconds (default 40): ") or "40")
analyze_video(test_url, num_clips=num, clip_length=length)
elif choice == "2":
extract_video_info(test_url)
elif choice == "3":
start = int(input("Start time in seconds: "))
end = int(input("End time in seconds: "))
get_transcript(test_url, start, end)
elif choice == "4":
start = input("Start time (MM:SS or seconds): ")
end = input("End time (MM:SS or seconds): ")
score_clip(test_url, start, end)
elif choice == "5":
start = input("Start time (MM:SS or seconds): ")
end = input("End time (MM:SS or seconds): ")
quality = input("Quality (360/720/1080, default 720): ") or "720"
get_download_url(test_url, start, end, quality)
else:
print("Invalid choice!")
|