cngsm commited on
Commit
cb3db33
·
verified ·
1 Parent(s): 534d731

Upload video_analyzer.py

Browse files
Files changed (1) hide show
  1. video_analyzer.py +124 -0
video_analyzer.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import moviepy.editor as mp
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ import os
7
+ import json
8
+ from datetime import datetime
9
+
10
+ from transformers import BlipProcessor, BlipForConditionalGeneration
11
+ import torch
12
+
13
+ # === BLIP Setup ===
14
+ blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
15
+ blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
16
+
17
+ class VideoSceneAnalyzer:
18
+ def __init__(self, video_path, scene_duration=8):
19
+ self.video_path = video_path
20
+ self.scene_duration = scene_duration
21
+ self.clip = None
22
+ self.video_info = {}
23
+ self.scenes = []
24
+
25
+ def load_video(self):
26
+ try:
27
+ self.clip = mp.VideoFileClip(self.video_path)
28
+ self.video_info = {
29
+ 'duration': self.clip.duration,
30
+ 'fps': self.clip.fps,
31
+ 'size': self.clip.size,
32
+ 'aspect_ratio': self.clip.size[0] / self.clip.size[1],
33
+ 'total_frames': int(self.clip.duration * self.clip.fps)
34
+ }
35
+ print(f"✅ Vídeo carregado: {self.video_info}")
36
+ return True
37
+ except Exception as e:
38
+ print(f"❌ Erro ao carregar vídeo: {e}")
39
+ return False
40
+
41
+ def describe_image_and_generate_prompt(self, frame, scene_number):
42
+ temp_img_path = f"temp_scene_{scene_number:02d}.jpg"
43
+ Image.fromarray(np.uint8(frame)).save(temp_img_path)
44
+ image = Image.open(temp_img_path).convert("RGB")
45
+ inputs = blip_processor(images=image, return_tensors="pt")
46
+ out = blip_model.generate(**inputs)
47
+ caption = blip_processor.decode(out[0], skip_special_tokens=True)
48
+ descricao = f"Imagem da cena {scene_number}: {caption.strip().capitalize()}."
49
+ prompt = (
50
+ f"{caption.strip().capitalize()}. Realistic lighting, professional cinematography, dynamic scene composition. "
51
+ "--ar 16:9 --v 6 --style photorealistic --quality 2"
52
+ )
53
+ return descricao, prompt
54
+
55
+ def extract_scenes(self):
56
+ if not self.clip:
57
+ print("❌ Vídeo não carregado.")
58
+ return False
59
+
60
+ total_duration = self.video_info['duration']
61
+ num_scenes = int(total_duration // self.scene_duration)
62
+ print(f"📹 Extraindo {num_scenes} cenas de {self.scene_duration}s cada...")
63
+
64
+ output_dir = f"scenes_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
65
+ os.makedirs(output_dir, exist_ok=True)
66
+ self.scenes = []
67
+
68
+ for i in range(num_scenes):
69
+ start_time = i * self.scene_duration
70
+ end_time = min((i + 1) * self.scene_duration, total_duration)
71
+ mid_time = (start_time + end_time) / 2
72
+ print(f"🎬 Processando cena {i+1}/{num_scenes} ({start_time:.1f}s - {end_time:.1f}s)")
73
+
74
+ try:
75
+ frame = self.clip.get_frame(mid_time)
76
+ descricao, prompt = self.describe_image_and_generate_prompt(frame, i + 1)
77
+ img = Image.fromarray(np.uint8(frame))
78
+ img_path = os.path.join(output_dir, f"scene_{i+1:02d}.jpg")
79
+ img.save(img_path)
80
+
81
+ scene_info = {
82
+ 'scene_number': i + 1,
83
+ 'start_time': start_time,
84
+ 'end_time': end_time,
85
+ 'mid_time': mid_time,
86
+ 'image_path': img_path,
87
+ 'descricao_detalhada': descricao,
88
+ 'prompt_ia': prompt,
89
+ 'segment_info': {
90
+ 'duration': end_time - start_time,
91
+ 'position_percent': (start_time / total_duration) * 100
92
+ }
93
+ }
94
+ self.scenes.append(scene_info)
95
+ except Exception as e:
96
+ print(f"⚠️ Erro ao processar cena {i+1}: {e}")
97
+ continue
98
+
99
+ print(f"✅ {len(self.scenes)} cenas extraídas com sucesso!")
100
+ return True
101
+
102
+ def save_results(self, output_file=None):
103
+ if not output_file:
104
+ output_file = f"video_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
105
+
106
+ results = {
107
+ 'video_info': self.video_info,
108
+ 'analysis_settings': {
109
+ 'scene_duration': self.scene_duration,
110
+ 'total_scenes': len(self.scenes)
111
+ },
112
+ 'scenes': self.scenes,
113
+ 'generated_at': datetime.now().isoformat()
114
+ }
115
+
116
+ with open(output_file, 'w', encoding='utf-8') as f:
117
+ json.dump(results, f, indent=2, ensure_ascii=False)
118
+
119
+ print(f"💾 Resultados salvos em: {output_file}")
120
+ return output_file
121
+
122
+ def cleanup(self):
123
+ if self.clip:
124
+ self.clip.close()