habulaj commited on
Commit
4d8c594
·
verified ·
1 Parent(s): 1c58981

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -5
main.py CHANGED
@@ -455,10 +455,18 @@ EXEMPLOS
455
  # Usar o vídeo cortado se disponível, senão o original
456
  video_for_export = cropped_video_path if cropped_video_path and os.path.exists(cropped_video_path) else temp_file.name
457
 
458
- # 1. Screenshot do vídeo cortado (frame no segundo 1)
459
  screenshot_path = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg').name
 
 
 
 
 
 
 
 
460
  ffmpeg_ss = subprocess.run(
461
- ["ffmpeg", "-y", "-i", video_for_export, "-ss", "1", "-frames:v", "1", screenshot_path],
462
  capture_output=True, text=True
463
  )
464
  if ffmpeg_ss.returncode != 0:
@@ -494,7 +502,20 @@ EXEMPLOS
494
  # URL do vídeo para o export: cortado se disponível, senão original
495
  export_video_url = cropped_video_url if cropped_video_url else video_url
496
 
497
- # 4. Montar title_url
 
 
 
 
 
 
 
 
 
 
 
 
 
498
  import urllib.parse
499
  title_params = urllib.parse.urlencode({
500
  "text": title_text,
@@ -503,11 +524,19 @@ EXEMPLOS
503
  })
504
  title_url = f"https://habulaj-recurve-api-img.hf.space/cover/title?{title_params}"
505
 
506
- # 5. Chamar API de video export
507
  print(f"🎬 Chamando video export API...")
508
  export_payload = {
509
  "video_url": export_video_url,
510
- "title_url": title_url
 
 
 
 
 
 
 
 
511
  }
512
  export_resp = requests.post(
513
  "https://habulaj-recurve-videos-export.hf.space/video/export",
 
455
  # Usar o vídeo cortado se disponível, senão o original
456
  video_for_export = cropped_video_path if cropped_video_path and os.path.exists(cropped_video_path) else temp_file.name
457
 
458
+ # 1. Screenshot do vídeo cortado (frame do MEIO)
459
  screenshot_path = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg').name
460
+ # Obter duração do vídeo para calcular o meio
461
+ duration_probe = subprocess.run(
462
+ ["ffprobe", "-v", "quiet", "-show_entries", "format=duration", "-of", "csv=p=0", video_for_export],
463
+ capture_output=True, text=True
464
+ )
465
+ video_duration = float(duration_probe.stdout.strip()) if duration_probe.returncode == 0 and duration_probe.stdout.strip() else 10.0
466
+ middle_time = str(video_duration / 2)
467
+
468
  ffmpeg_ss = subprocess.run(
469
+ ["ffmpeg", "-y", "-i", video_for_export, "-ss", middle_time, "-frames:v", "1", screenshot_path],
470
  capture_output=True, text=True
471
  )
472
  if ffmpeg_ss.returncode != 0:
 
502
  # URL do vídeo para o export: cortado se disponível, senão original
503
  export_video_url = cropped_video_url if cropped_video_url else video_url
504
 
505
+ # 4. Obter dimensões do vídeo para cálculo horizontal/vertical
506
+ import cv2
507
+ cap = cv2.VideoCapture(video_for_export)
508
+ crop_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) if cap.isOpened() else 1080
509
+ crop_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) if cap.isOpened() else 1920
510
+ cap.release()
511
+
512
+ canvas_width = 1080
513
+ image_height = 616
514
+ video_x = int((canvas_width - crop_w) / 2)
515
+ video_template = "horizontal" if crop_w > crop_h else "vertical"
516
+ print(f"📐 Dimensões do vídeo: {crop_w}x{crop_h} | Template: {video_template}")
517
+
518
+ # 5. Montar title_url
519
  import urllib.parse
520
  title_params = urllib.parse.urlencode({
521
  "text": title_text,
 
524
  })
525
  title_url = f"https://habulaj-recurve-api-img.hf.space/cover/title?{title_params}"
526
 
527
+ # 6. Chamar API de video export
528
  print(f"🎬 Chamando video export API...")
529
  export_payload = {
530
  "video_url": export_video_url,
531
+ "title_url": title_url,
532
+ "image_height": image_height,
533
+ "cut_start": 0,
534
+ "cut_end": video_duration,
535
+ "video_x": video_x,
536
+ "video_y": 0,
537
+ "video_width": crop_w,
538
+ "video_height": crop_h,
539
+ "video_template": video_template
540
  }
541
  export_resp = requests.post(
542
  "https://habulaj-recurve-videos-export.hf.space/video/export",