Digz / app.py
alexx-ai's picture
Update app.py
af36228 verified
Raw
History Blame Contribute Delete
3.14 kB
import requests
import time
import gradio as gr
# Headers for requests
HEADERS = {
'accept': 'application/json',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,pt;q=0.7,hi;q=0.6',
'content-type': 'application/json',
'origin': 'https://www.savethevideo.com',
'priority': 'u=1, i',
'referer': 'https://www.savethevideo.com/',
'sec-ch-ua': '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36'
}
def get_video_data(url):
response = requests.post(
'https://api.v02.savethevideo.com/tasks',
headers=HEADERS,
json={"type": "info", "url": url}
)
data = response.json()
while data.get('state') != 'completed':
if data.get('state') in ('pending', 'active'):
task_id = data['id']
time.sleep(2)
response = requests.get(
f'https://api.v02.savethevideo.com/tasks/{task_id}',
headers=HEADERS
)
data = response.json()
else:
raise Exception(f"Unexpected task state: {data}")
return data['result']
def extract_important_info(video_json):
video = video_json[0]
# hd_formats = [
# f for f in video.get('formats', [])
# if f.get('format_id') == 'hd'
# and f.get('vcodec') != 'none'
# and 'DASH' not in f.get('format_note', '')
# and not f.get('url', '').endswith('.m3u8')
# ]
hd_formats = [
f for f in video.get('formats', [])
if f.get('vcodec') != 'none' # has video
and 'DASH' not in f.get('format_note', '') # skip DASH streams
and not f.get('url', '').endswith('.m3u8') # skip HLS
and 'dash' not in f.get('format_id', '').lower() # skip any format_id containing 'dash'
]
best_format = hd_formats[0] if hd_formats else {}
return {
'title': video.get('title'),
'filesize_approx': best_format.get('filesize_approx'),
'hd_video_url': best_format.get('url'),
'view_count': video.get('like_count'),
'duration': video.get('duration'),
'duration_string': video.get('duration_string'),
'thumbnail': video.get('thumbnail'),
'upload_date': video.get('upload_date'),
'timestamp': video.get('timestamp')
}
def gradio_handler(video_url):
try:
raw_data = get_video_data(video_url)
return extract_important_info(raw_data)
except Exception as e:
return {"error": str(e)}
# Gradio UI
app = gr.Interface(
fn=gradio_handler,
inputs=gr.Textbox(
label="Video URL",
placeholder="Paste video URL here..."
),
outputs=gr.JSON(label="Extracted Video Info"),
title="Video Info Extractor",
description="Fetch HD video metadata using API"
)
if __name__ == "__main__":
app.launch()