101 lines
4.7 KiB
Python

#!/usr/bin/env python3
#bash video_add_watermark.sh "$VID" "$PNG" 30 30
import requests
import json
import time
import sys
# Base URL for the API
BASE_URL = "https://animator-gg-api.ca2324.servep2p.com:8443/api/script"
# Arguments to pass to the script
VID="https://ca2324.ddns.net:8443/jerry/_seed847064922.mp4"
PNG="https://ca2324.ddns.net:8443/jerry/0a97d84a70fec3991cf05ee99ceb6469.png"
SAMPLE_OUPUT = """
{
"status": "completed",
"result": {
"success": true,
"stdout": "{\n \"input_video_url\": \"https://ca2324.ddns.net:8443/jerry/_seed847064922.mp4\",\n \"watermark_png_url\": \"https://ca2324.ddns.net:8443/jerry/0a97d84a70fec3991cf05ee99ceb6469.png\", \n \"pos_x\": 30,\n \"pos_y\": 30,\n \"size_w\": 0,\n \"size_h\": 0,\n \"output_file_name\": \"/var/www/html/watermarked_videos//bcc55e77-f1b4-472c-8a1b-0f3c852bd73a.mp4\",\n \"output_file_url\": \"https://ca2324.ddns.net:8443/watermarked_videos/bcc55e77-f1b4-472c-8a1b-0f3c852bd73a.mp4\"\n}",
"stderr": "x265 [info]: HEVC encoder version 4.1+1-1d117be\nx265 [info]: build info [Linux][GCC 11.4.0][64 bit] 8bit+10bit+12bit\nx265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2\nx265 [info]: Main profile, Level-4 (Main tier)\nx265 [info]: Thread pool created using 56 threads\nx265 [info]: Slices : 1\nx265 [info]: frame threads / pool features : 5 / wpp(25 rows)\nx265 [info]: Coding QT: max CU size, min CU size : 64 / 8\nx265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra\nx265 [info]: ME / range / subpel / merge : hex / 57 / 1 / 2\nx265 [info]: Keyframe min / max / scenecut / bias : 24 / 250 / 40 / 5.00 \nx265 [info]: Lookahead / bframes / badapt : 15 / 4 / 0\nx265 [info]: b-pyramid / weightp / weightb : 1 / 1 / 0\nx265 [info]: References / ref-limit cu / depth : 2 / on / on\nx265 [info]: AQ: mode / str / qg-size / cu-tree : 2 / 1.0 / 32 / 1\nx265 [info]: Rate Control / qCompress : CRF-24.0 / 0.60\nx265 [info]: tools: rd=2 psy-rd=2.00 early-skip rskip mode=1 signhide tmvp\nx265 [info]: tools: fast-intra strong-intra-smoothing lslices=8 deblock sao\nx265 [info]: frame I: 2, Avg QP:24.88 kb/s: 14848.22\nx265 [info]: frame P: 95, Avg QP:24.31 kb/s: 8688.46 \nx265 [info]: frame B: 384, Avg QP:29.93 kb/s: 2459.11 \nx265 [info]: Weighted P-Frames: Y:8.4% UV:5.3%\n\nencoded 481 frames in 8.94s (53.79 fps), 3740.95 kb/s, Avg QP:28.80"
},
"timestamp": 1743991194237
}
"""
def main():
print("Starting script execution...")
# Start script execution and get token
try:
response = requests.post(
BASE_URL,
json={
"scriptPath": "/tmp/api_scripts/video_add_watermark.sh",
"args": [VID, PNG, 30, 30]
},
headers={"Content-Type": "application/json"}
)
response.raise_for_status() # Raise exception for non-200 status codes
token_data = response.json()
token = token_data.get("token")
if not token:
print(f"Failed to get token. Response: {response.text}")
sys.exit(1)
print(f"Received token: {token}")
print("Polling for results...")
except requests.exceptions.RequestException as e:
print(f"Error starting script execution: {e}")
sys.exit(1)
# Poll for results with timeout
MAX_ATTEMPTS = 30
SLEEP_SECONDS = 1
status = "pending"
for attempt in range(1, MAX_ATTEMPTS + 1):
if status not in ["pending", "running"]:
break
print(f"Polling attempt {attempt} of {MAX_ATTEMPTS}...")
try:
result_response = requests.get(f"{BASE_URL}?token={token}")
result_response.raise_for_status()
result_data = result_response.json()
status = result_data.get("status")
if status in ["pending", "running"]:
print(f"Script is still {status}. Waiting {SLEEP_SECONDS}s...")
time.sleep(SLEEP_SECONDS)
else:
break
except requests.exceptions.RequestException as e:
print(f"Error polling for results: {e}")
sys.exit(1)
if status in ["pending", "running"]:
print("Timeout waiting for script to complete")
sys.exit(1)
# Print results
print(f"Script execution completed with status: {status}")
print(f"Result: {json.dumps(result_data, indent=2)}")
# Pretty print stdout if available
if result_data.get("result", {}).get("stdout"):
stdout = result_data["result"]["stdout"]
print("\nScript output:")
print(stdout)
if __name__ == "__main__":
main()