Spaces:
Running
Running
Create utility/video /background_video_generator.py
Browse files
utility/video /background_video_generator.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from utility.utils import log_response,LOG_TYPE_PEXEL
|
4 |
+
|
5 |
+
PEXELS_API_KEY = os.environ.get('PEXELS_KEY')
|
6 |
+
|
7 |
+
def search_videos(query_string, orientation_landscape=True):
|
8 |
+
|
9 |
+
url = "https://api.pexels.com/videos/search"
|
10 |
+
headers = {
|
11 |
+
"Authorization": PEXELS_API_KEY,
|
12 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
13 |
+
}
|
14 |
+
params = {
|
15 |
+
"query": query_string,
|
16 |
+
"orientation": "landscape" if orientation_landscape else "portrait",
|
17 |
+
"per_page": 15
|
18 |
+
}
|
19 |
+
|
20 |
+
response = requests.get(url, headers=headers, params=params)
|
21 |
+
json_data = response.json()
|
22 |
+
log_response(LOG_TYPE_PEXEL,query_string,response.json())
|
23 |
+
|
24 |
+
return json_data
|
25 |
+
|
26 |
+
|
27 |
+
def getBestVideo(query_string, orientation_landscape=True, used_vids=[]):
|
28 |
+
vids = search_videos(query_string, orientation_landscape)
|
29 |
+
videos = vids['videos'] # Extract the videos list from JSON
|
30 |
+
|
31 |
+
# Filter and extract videos with width and height as 1920x1080 for landscape or 1080x1920 for portrait
|
32 |
+
if orientation_landscape:
|
33 |
+
filtered_videos = [video for video in videos if video['width'] >= 1920 and video['height'] >= 1080 and video['width']/video['height'] == 16/9]
|
34 |
+
else:
|
35 |
+
filtered_videos = [video for video in videos if video['width'] >= 1080 and video['height'] >= 1920 and video['height']/video['width'] == 16/9]
|
36 |
+
|
37 |
+
# Sort the filtered videos by duration in ascending order
|
38 |
+
sorted_videos = sorted(filtered_videos, key=lambda x: abs(15-int(x['duration'])))
|
39 |
+
|
40 |
+
# Extract the top 3 videos' URLs
|
41 |
+
for video in sorted_videos:
|
42 |
+
for video_file in video['video_files']:
|
43 |
+
if orientation_landscape:
|
44 |
+
if video_file['width'] == 1920 and video_file['height'] == 1080:
|
45 |
+
if not (video_file['link'].split('.hd')[0] in used_vids):
|
46 |
+
return video_file['link']
|
47 |
+
else:
|
48 |
+
if video_file['width'] == 1080 and video_file['height'] == 1920:
|
49 |
+
if not (video_file['link'].split('.hd')[0] in used_vids):
|
50 |
+
return video_file['link']
|
51 |
+
print("NO LINKS found for this round of search with query :", query_string)
|
52 |
+
return None
|
53 |
+
|
54 |
+
|
55 |
+
def generate_video_url(timed_video_searches,video_server):
|
56 |
+
timed_video_urls = []
|
57 |
+
if video_server == "pexel":
|
58 |
+
used_links = []
|
59 |
+
for (t1, t2), search_terms in timed_video_searches:
|
60 |
+
url = ""
|
61 |
+
for query in search_terms:
|
62 |
+
|
63 |
+
url = getBestVideo(query, orientation_landscape=True, used_vids=used_links)
|
64 |
+
if url:
|
65 |
+
used_links.append(url.split('.hd')[0])
|
66 |
+
break
|
67 |
+
timed_video_urls.append([[t1, t2], url])
|
68 |
+
elif video_server == "stable_diffusion":
|
69 |
+
timed_video_urls = get_images_for_video(timed_video_searches)
|
70 |
+
|
71 |
+
return timed_video_urls
|