-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_clipper.py
More file actions
207 lines (176 loc) Β· 8.83 KB
/
test_clipper.py
File metadata and controls
207 lines (176 loc) Β· 8.83 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
#!/usr/bin/env python3
"""
Test script for the generate_clips function in clipper.py
This script creates a sample transcript and tests the clip generation functionality.
"""
import os
import sys
import tempfile
import shutil
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Add the project root to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from services.clipper import generate_clips
def create_sample_transcript():
"""
Create a sample whisper transcript of 2 minutes of conversation
Format: [{"start": float, "end": float, "text": str}, ...]
"""
transcript = [
{"start": 0.0, "end": 3.5, "text": "Hello everyone, welcome to today's presentation about artificial intelligence."},
{"start": 3.5, "end": 7.2, "text": "We're going to explore how AI is transforming various industries."},
{"start": 7.2, "end": 11.8, "text": "First, let's talk about machine learning and its applications in healthcare."},
{"start": 11.8, "end": 15.4, "text": "Machine learning algorithms can analyze medical images with incredible accuracy."},
{"start": 15.4, "end": 19.1, "text": "They help doctors detect diseases earlier than traditional methods."},
{"start": 19.1, "end": 23.7, "text": "Moving on to the financial sector, AI is revolutionizing fraud detection."},
{"start": 23.7, "end": 27.3, "text": "Banks use sophisticated algorithms to identify suspicious transactions."},
{"start": 27.3, "end": 31.0, "text": "This has significantly reduced financial crimes and improved security."},
{"start": 31.0, "end": 35.6, "text": "In the automotive industry, self-driving cars are becoming a reality."},
{"start": 35.6, "end": 39.2, "text": "These vehicles use computer vision and sensor fusion technologies."},
{"start": 39.2, "end": 43.8, "text": "They can navigate complex traffic situations better than human drivers."},
{"start": 43.8, "end": 47.5, "text": "Natural language processing is another exciting field in AI."},
{"start": 47.5, "end": 51.1, "text": "Chatbots and virtual assistants are becoming more conversational."},
{"start": 51.1, "end": 55.7, "text": "They can understand context and provide more helpful responses."},
{"start": 55.7, "end": 59.4, "text": "Let's discuss the ethical implications of artificial intelligence."},
{"start": 59.4, "end": 63.0, "text": "We need to ensure AI systems are fair and unbiased."},
{"start": 63.0, "end": 67.6, "text": "Transparency in AI decision-making is crucial for public trust."},
{"start": 67.6, "end": 71.2, "text": "Data privacy is another important consideration in AI development."},
{"start": 71.2, "end": 75.8, "text": "Companies must protect user information while training AI models."},
{"start": 75.8, "end": 79.5, "text": "The future of AI looks promising with continued research and innovation."},
{"start": 79.5, "end": 83.1, "text": "We're seeing breakthroughs in quantum computing and neural networks."},
{"start": 83.1, "end": 87.7, "text": "These advances will enable even more powerful AI applications."},
{"start": 87.7, "end": 91.4, "text": "Education is also being transformed by artificial intelligence."},
{"start": 91.4, "end": 95.0, "text": "Personalized learning platforms adapt to individual student needs."},
{"start": 95.0, "end": 99.6, "text": "This helps students learn more effectively at their own pace."},
{"start": 99.6, "end": 103.2, "text": "AI is also making significant contributions to climate science."},
{"start": 103.2, "end": 107.8, "text": "It helps predict weather patterns and analyze environmental data."},
{"start": 107.8, "end": 111.5, "text": "This information is crucial for addressing climate change challenges."},
{"start": 111.5, "end": 115.1, "text": "In conclusion, AI is reshaping our world in remarkable ways."},
{"start": 115.1, "end": 120.0, "text": "Thank you for your attention, and I hope you found this presentation informative."}
]
return transcript
def create_test_video():
"""
Create a simple test video using FFmpeg (2 minutes long)
"""
video_path = "test_video.mp4"
# Check if FFmpeg is available
ffmpeg_path = os.getenv("FFMPEG_PATH", "ffmpeg")
# Create a 2-minute test video with color bars and audio tone
cmd = [
ffmpeg_path,
"-f", "lavfi",
"-i", "testsrc2=duration=120:size=1280x720:rate=30",
"-f", "lavfi",
"-i", "sine=frequency=1000:duration=120",
"-c:v", "libx264",
"-c:a", "aac",
"-t", "120",
"-y",
video_path
]
print(f"π¬ Creating test video: {video_path}")
print(f"π§ Running command: {' '.join(cmd)}")
import subprocess
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"β Failed to create test video: {result.stderr}")
return None
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
print(f"β
Test video created successfully: {video_path}")
return video_path
else:
print(f"β Test video creation failed - file not found or empty")
return None
def test_generate_clips():
"""
Test the generate_clips function
"""
print("π§ͺ Starting generate_clips test...")
# Create sample transcript
print("π Creating sample transcript...")
transcript = create_sample_transcript()
print(f"β
Created transcript with {len(transcript)} segments (total duration: {transcript[-1]['end']:.1f}s)")
# Create test video
print("π¬ Creating test video...")
video_path = create_test_video()
if not video_path:
print("β Cannot proceed without test video")
return False
# Create temporary output directory
output_dir = "test_clips_output"
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir, exist_ok=True)
try:
print(f"π― Testing generate_clips function...")
print(f" Video: {video_path}")
print(f" Clip duration: 30s")
print(f" Output directory: {output_dir}")
# Test the function
clips = generate_clips(
video_path=video_path,
clip_duration="30s", # 30 second clips
transcript=transcript,
output_dir=output_dir,
cache_file=os.path.join(output_dir, "cache.json"),
platform="test",
source_id="test_video"
)
print(f"π generate_clips completed successfully!")
print(f"π Generated {len(clips)} clips")
# Verify results
if len(clips) == 0:
print("β No clips were generated!")
return False
print("\nπ Clip details:")
for i, clip in enumerate(clips):
print(f" Clip {i+1}:")
print(f" Index: {clip['index']}")
print(f" Video: {clip['video']}")
print(f" SRT: {clip['srt']}")
print(f" Start: {clip['start_time']}s")
print(f" End: {clip['end_time']}s")
print(f" Transcript length: {len(clip['transcript_text'])} chars")
# Check if files exist
video_exists = os.path.exists(clip['video'])
srt_exists = os.path.exists(clip['srt'])
video_size = os.path.getsize(clip['video']) if video_exists else 0
print(f" Video file exists: {video_exists} ({video_size} bytes)")
print(f" SRT file exists: {srt_exists}")
if not video_exists or video_size == 0:
print(f" β Video file issue!")
return False
if not srt_exists:
print(f" β SRT file missing!")
return False
print(f"\nβ
All {len(clips)} clips generated successfully!")
print(f"π Output directory: {os.path.abspath(output_dir)}")
return True
except Exception as e:
print(f"β Test failed with exception: {e}")
import traceback
print(f"π Traceback: {traceback.format_exc()}")
return False
finally:
# Cleanup
if os.path.exists(video_path):
try:
os.remove(video_path)
print(f"π§Ή Cleaned up test video: {video_path}")
except:
pass
if __name__ == "__main__":
print("π Starting clipper.py test suite")
print("=" * 50)
success = test_generate_clips()
print("=" * 50)
if success:
print("π All tests passed!")
sys.exit(0)
else:
print("β Tests failed!")
sys.exit(1)