Repository navigation
Expand file tree
/
Copy pathdescribe.py
More file actions
411 lines (328 loc) · 15.1 KB
/
Copy pathdescribe.py
File metadata and controls
411 lines (328 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import torch
from PIL import Image
from decord import VideoReader, cpu
from scenedetect import detect, ContentDetector
import yt_dlp
import os
import tempfile
import base64
import re
import ollama
from langchain_ollama import OllamaLLM
from langchain.prompts import PromptTemplate
def encode_image(image_path):
"""Resmi base64 formatına çevirir"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def extract_overall_impression(text):
"""Overall Impression kısmının açıklamasını çıkarır"""
# YÖNTEM 1: Overall Impression'dan sonraki ilk paragrafı al
if "**Overall Impression:**" in text:
parts = text.split("**Overall Impression:**", 1)
if len(parts) > 1:
after_impression = parts[1].strip()
# Bir sonraki ** işaretine kadar olan kısmı al
next_section = after_impression.split("**")[0].strip()
return next_section
# YÖNTEM 2: Regex ile daha hassas çıkarma
pattern = r'\*\*Overall Impression:\*\*\s*(.*?)(?=\*\*|\n\n|\Z)'
match = re.search(pattern, text, re.DOTALL)
if match:
return match.group(1).strip()
return "Overall Impression bulunamadı"
# Çeviri için ChatOllama kullanacağız, model yüklemeye gerek yok
def download_tiktok_specific(url):
"""TikTok videosunu indirir - özel fonksiyon"""
try:
print("TikTok video indiriliyor...")
# TikTok için çok esnek format ayarları
ydl_opts = {
'format': 'best[ext=mp4]/best', # En basit format seçimi
'outtmpl': os.path.join(tempfile.gettempdir(), '%(id)s.%(ext)s'),
'writesubtitles': False,
'writeautomaticsub': False,
'noplaylist': True,
'extract_flat': False,
'quiet': False, # Debug için ses açık
'no_warnings': False,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
print("Video bilgileri alınıyor...")
info = ydl.extract_info(url, download=True)
video_id = info.get('id', 'tiktok_video')
# Dosya uzantısını kontrol et
ext = info.get('ext', 'mp4')
video_path = os.path.join(tempfile.gettempdir(), f"{video_id}.{ext}")
# Eğer mp4 değilse mp4'e çevir
if ext != 'mp4':
mp4_path = os.path.join(tempfile.gettempdir(), f"{video_id}.mp4")
print(f"Converting {ext} to mp4...")
try:
import subprocess
subprocess.run(['ffmpeg', '-i', video_path, '-c', 'copy', mp4_path],
check=True, capture_output=True)
os.remove(video_path)
video_path = mp4_path
except:
print("FFmpeg conversion failed, using original file")
print(f"✅ TikTok video downloaded: {video_path}")
return video_path
except Exception as e:
raise Exception(f"TikTok download error: {str(e)}")
def download_video_from_url(url):
"""Download video from YouTube, TikTok or other platforms"""
try:
print(f"Downloading video from: {url}")
# URL tipini kontrol et
if 'tiktok.com' in url.lower():
print("TikTok video detected...")
# TikTok için özel fonksiyon kullan
return download_tiktok_specific(url)
elif any(x in url.lower() for x in ['youtube.com', 'youtu.be']):
print("YouTube video detected...")
platform = "YouTube"
else:
print("Other platform video detected...")
platform = "Other"
# YouTube ve diğer platformlar için standart ayarlar
ydl_opts = {
'format': 'best[ext=mp4]',
'outtmpl': os.path.join(tempfile.gettempdir(), '%(id)s.%(ext)s'),
'quiet': True,
'no_warnings': True
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
video_id = info.get('id', 'video')
video_path = os.path.join(tempfile.gettempdir(), f"{video_id}.mp4")
print(f"✅ {platform} video downloaded successfully: {video_path}")
return video_path
except Exception as e:
raise Exception(f"Video download error: {str(e)}")
# Geriye uyumluluk için eski fonksiyon adını koruyalım
def download_youtube_video(url):
"""Backward compatibility wrapper"""
return download_video_from_url(url)
def detect_scenes(video_path):
"""Detect scene transitions in video"""
try:
print("Detecting scenes...")
# Önce video süresini kontrol et
from moviepy.editor import VideoFileClip
video = VideoFileClip(video_path)
duration = video.duration
video.close()
print(f"Video duration: {duration:.2f} seconds")
# Sahne tespiti dene
try:
scenes = detect(video_path, ContentDetector(threshold=30.0))
scene_times = [scene[0].get_seconds() for scene in scenes]
print(f"Number of scenes detected: {len(scene_times)}")
except Exception as scene_error:
print(f"Automatic scene detection failed: {str(scene_error)}")
scene_times = []
# Eğer hiç sahne tespit edilmediyse veya çok az sahne varsa
if len(scene_times) == 0 or (len(scene_times) == 1 and duration > 10):
print("Creating manual scenes for TikTok/short video...")
scene_times = []
# Kısa videolar için (TikTok vb) - 5 saniyelik sahneler
if duration <= 60:
interval = min(5, duration / 3) # En az 3 sahne
# Uzun videolar için - 10 saniyelik sahneler
else:
interval = 10
current_time = 0
while current_time < duration:
scene_times.append(current_time)
current_time += interval
print(f"Created {len(scene_times)} manual scenes with {interval}s intervals")
return scene_times
except Exception as e:
print(f"Scene detection error: {str(e)}")
# Son çare: Sadece başlangıç sahnesi
return [0.0]
def encode_video(video_path):
"""Extract one frame from each scene"""
try:
print(f"Processing video: {video_path}")
scene_times = detect_scenes(video_path)
vr = VideoReader(video_path, ctx=cpu(0))
fps = vr.get_avg_fps()
total_frames = len(vr)
frames = []
frame_times = []
for i in range(len(scene_times)):
start_time = scene_times[i]
if i < len(scene_times) - 1:
end_time = scene_times[i + 1]
else:
end_time = total_frames / fps
middle_time = (start_time + end_time) / 2
frame_idx = int(middle_time * fps)
if frame_idx >= total_frames:
frame_idx = total_frames - 1
try:
frame = vr.get_batch([frame_idx]).asnumpy()[0]
frames.append(Image.fromarray(frame.astype('uint8')))
frame_times.append(start_time)
print(f"Scene {i+1}/{len(scene_times)} processed - Time: {start_time:.2f}s")
except Exception as e:
print(f"Error processing scene {i+1}: {str(e)}")
return frames, scene_times
except Exception as e:
raise Exception(f"Video processing error: {str(e)}")
def calculate_optimal_description_length(scene_duration):
"""Calculate optimal description length based on scene duration"""
# Average speech rate: 2.5 words/second
WORDS_PER_SECOND = 2.5
max_words = int(scene_duration * WORDS_PER_SECOND)
# Minimum 3, maximum 15 words
return max(3, min(15, max_words))
def generate_frame_descriptions(frames, scene_times):
"""Generate English descriptions for each scene using Ollama"""
try:
print("Describing scenes with Ollama...")
print(f"Total scenes: {len(frames)}")
descriptions = []
temp_dir = tempfile.mkdtemp()
for i, frame in enumerate(frames):
try:
# Frame'i geçici dosya olarak kaydet
temp_image_path = os.path.join(temp_dir, f"frame_{i}.jpg")
frame.save(temp_image_path)
# Base64'e çevir
base64_image = encode_image(temp_image_path)
# Sahne süresine göre optimal uzunluk hesapla
scene_duration = scene_times[i+1] - scene_times[i] if i < len(scene_times)-1 else 5
optimal_length = calculate_optimal_description_length(scene_duration)
# Ollama ile görüntü analizi
response = ollama.chat(
model='gemma3:4b', # Vision modeli kullan
messages=[
{
'role': 'user',
'content': f'Describe the main action in this scene in {optimal_length} words or less. Focus on key actions and subjects.',
'images': [base64_image]
}
]
)
# Yanıtı işle
full_description = response['message']['content']
# Overall Impression varsa çıkar, yoksa tam metni kullan
scene_desc = extract_overall_impression(full_description)
if scene_desc == "Overall Impression bulunamadı":
scene_desc = full_description
# Kelime sayısını sınırla
words = scene_desc.split()[:optimal_length]
scene_desc = ' '.join(words)
if not scene_desc.endswith('.'):
scene_desc = scene_desc.rstrip(',') + '.'
descriptions.append(scene_desc)
print(f"Scene {i+1}/{len(frames)} described: {scene_desc}")
# Geçici dosyayı sil
os.remove(temp_image_path)
except Exception as e:
print(f"Error describing scene {i+1}: {str(e)}")
descriptions.append(f"Scene {i+1}")
continue
# Geçici dizini temizle
try:
os.rmdir(temp_dir)
except:
pass
return descriptions
except Exception as e:
print(f"Description error: {str(e)}")
return [f"Scene {i+1}" for i in range(len(frames))]
def translate_and_enhance_with_ollama(descriptions, target_lang):
"""Translate descriptions using Ollama"""
try:
print(f"Translating descriptions to {target_lang}...")
# If target language is English, no translation needed
if target_lang == 'en':
return descriptions
# Dil kodlarını tam isme çevir
lang_names = {
'en': 'English',
'tr': 'Turkish'
}
source_lang = lang_names.get('en', 'English')
target_lang_name = lang_names.get(target_lang, 'Turkish')
print(f"Starting translation from English to {target_lang_name}")
print(f"Using model: qwen3:8b")
enhanced_descriptions = []
for i, desc in enumerate(descriptions):
print(f"\nTranslating ({i+1}/{len(descriptions)})")
print(f"Original: {desc}")
try:
# Ollama ile çeviri
response = ollama.chat(
model='gemma3:4b',
messages=[
{
'role': 'user',
'content': f'You are a professional translator. Translate the following {source_lang} text to {target_lang_name}. Only provide the translation, no explanations: "{desc}"'
}
],
options={
'temperature': 0.1
}
)
enhanced = response['message']['content'].strip().replace('\n', ' ')
enhanced_descriptions.append(enhanced)
print(f"Translated: {enhanced}")
except Exception as e:
print(f"Error translating description {i+1}: {str(e)}")
enhanced_descriptions.append(desc) # Hata durumunda orijinali kullan
return enhanced_descriptions
except Exception as e:
print(f"Translation error: {str(e)}")
return descriptions
def summarize_with_ollama(scene_descriptions, lang='en'):
"""Create summary in selected language"""
try:
print("Generating summary...")
llm = OllamaLLM(model="phi4:latest")
# Summary templates for English and Turkish
summary_templates = {
'tr': """
Aşağıda bir videodan farklı sahnelerin açıklamaları bulunmaktadır.
Bu açıklamaları kullanarak videonun tutarlı ve anlamlı bir özetini oluşturun.
Sahneler:
{scene_descriptions}
Lütfen:
1. Gereksiz bilgileri birleştirin
2. Önemli ayrıntıları koruyun
3. Kronolojik sırayı koruyun
4. Akıcı ve bağlantılı cümleler kullanın
5. Ana olaylara ve eylemlere odaklanın
6. Doğal olarak akan bir anlatı oluşturun
Videonun özünü yakalayan kapsamlı bir özet yazın:
""",
'en': """
Below are descriptions of different scenes from a video.
Create a coherent and meaningful summary of the video using these descriptions.
Scenes:
{scene_descriptions}
Please:
1. Combine redundant information
2. Preserve important details
3. Maintain chronological order
4. Use fluid and connected sentences
5. Focus on the main events and actions
6. Create a narrative that flows naturally
Write a comprehensive summary that captures the essence of the video:
"""
}
template = summary_templates.get(lang, summary_templates['en'])
# Sahne açıklamalarını madde işaretleriyle birleştir
scenes = "\n".join([f"- {desc}" for desc in scene_descriptions])
# Özet oluştur
prompt = template.format(scene_descriptions=scenes)
final_summary = llm.invoke(prompt)
# Gereksiz boşlukları ve fazla satırları temizle
final_summary = final_summary.strip()
return final_summary
except Exception as e:
print(f"Summary generation error: {str(e)}")
return ' '.join(scene_descriptions)