File size: 27,654 Bytes
df66a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# from flask import Flask, render_template, jsonify, request, send_from_directory, send_file, redirect, url_for
# import os, json, threading, time
# from datetime import datetime
# from extract_signed_segments_from_annotations import ClipExtractor, VideoClip
# import logging

# app = Flask(__name__)
# logging.basicConfig(level=logging.INFO)

# VIDEO_DIR = os.path.abspath("data/videos")
# ANNOTATIONS_DIR = os.path.abspath("data/annotations")
# TEMP_DIR = os.path.abspath("data/temp")
# WORD_TIMESTAMPS_DIR = os.path.abspath("data/word_timestamps")
# ALIGNMENTS_DIR = os.path.abspath("data/alignments")
# TRANSCRIPTS_DIR = os.path.abspath("data/transcripts")

# # Ensure all required directories exist
# for directory in [VIDEO_DIR, ANNOTATIONS_DIR, TEMP_DIR, WORD_TIMESTAMPS_DIR, ALIGNMENTS_DIR, TRANSCRIPTS_DIR]:
#     os.makedirs(directory, exist_ok=True)

# # Global dictionary for clip extraction progress status keyed by video_id
# clip_extraction_status = {}
# # Global dictionary for transcription progress status keyed by video_id
# transcription_progress_status = {}

# def update_extraction_progress(video_id, current, total):
#     percent = int((current / total) * 100)
#     clip_extraction_status[video_id] = {"current": current, "total": total, "percent": percent}

# def run_clip_extraction(video_id):
#     try:
#         base_dir = app.root_path
#         extractor = ClipExtractor(base_dir)
#         # The extractor uses f"{video_id}.mp4" for the source video and f"{video_id}_annotations.json" for annotations.
#         extractor.extract_clips_from_annotations(
#             video_id,
#             progress_callback=lambda current, total: update_extraction_progress(video_id, current, total)
#         )
#         if video_id in clip_extraction_status:
#             status = clip_extraction_status[video_id]
#             if status.get("percent", 0) < 100:
#                 update_extraction_progress(video_id, status["total"], status["total"])
#             else:
#                 update_extraction_progress(video_id, 1, 1)
#     except Exception as e:
#         logging.error(f"Error during clip extraction for {video_id}: {str(e)}")
#         clip_extraction_status[video_id] = {"error": str(e)}

# def run_transcription(video_id):
#     try:
#         base_dir = app.root_path
#         output_path = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")

#         # Check if transcription already exists and is valid.
#         if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
#             app.logger.info(f"Using cached transcription for video {video_id}.")
#             transcription_progress_status[video_id] = {"status": "completed", "percent": 100}
#             return

#         video_path = os.path.join(base_dir, "data", "videos", f"{video_id}.mp4")
#         transcription_progress_status[video_id] = {"status": "started", "percent": 10}

#         # Run transcription via the imported function from get_transcription_with_amazon.py
#         from get_transcription_with_amazon import get_word_timestamps
#         word_timestamps = get_word_timestamps(video_path)

#         with open(output_path, "w") as f:
#             json.dump(word_timestamps, f, indent=4)

#         transcription_progress_status[video_id] = {"status": "completed", "percent": 100}
#     except Exception as e:
#         app.logger.error(f"Error during transcription for {video_id}: {str(e)}")
#         transcription_progress_status[video_id] = {"status": "error", "percent": 0, "message": str(e)}

# @app.route('/')
# def index():
#     return redirect(url_for('select_video'))

# @app.route('/select_video')
# def select_video():
#     if not os.path.exists(VIDEO_DIR):
#         return render_template('error.html', message="Video directory not found.")
#     videos = [f for f in os.listdir(VIDEO_DIR) if f.endswith('.mp4')]
#     video_ids = [os.path.splitext(v)[0] for v in videos]
#     return render_template('select_video.html', video_ids=video_ids)

# @app.route('/player/<video_id>')
# def player(video_id):
#     return render_template('player.html', video_id=video_id)

# @app.route('/videos')
# def get_videos():
#     if not os.path.exists(VIDEO_DIR):
#         return jsonify({'error': 'Video directory not found'}), 404
#     videos = [f for f in os.listdir(VIDEO_DIR) if f.endswith(('.mp4', '.avi', '.mov'))]
#     if not videos:
#         return jsonify({'error': 'No videos found'}), 404
#     return jsonify(videos)

# @app.route('/video/<path:filename>')
# def serve_video(filename):
#     if not os.path.exists(os.path.join(VIDEO_DIR, filename)):
#         return jsonify({'error': 'Video not found'}), 404
#     return send_from_directory(VIDEO_DIR, filename)

# @app.route('/save_annotations', methods=['POST'])
# def save_annotations():
#     data = request.json
#     if not data or 'video' not in data or 'timestamps' not in data:
#         return jsonify({'success': False, 'message': 'Invalid data'}), 400

#     annotation_file = os.path.join(ANNOTATIONS_DIR, f"{data['video']}_annotations.json")
#     annotation_data = {
#         "video_name": data['video'] + ".mp4",
#         "timestamps": sorted(data['timestamps']),
#         "annotation_date": datetime.now().isoformat()
#     }
#     with open(annotation_file, 'w') as f:
#         json.dump(annotation_data, f, indent=4)
#     return jsonify({'success': True, 'message': 'Annotations saved successfully'})

# @app.route('/get_annotations/<path:video_name>')
# def get_annotations(video_name):
#     annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_name}_annotations.json")
#     if not os.path.exists(annotation_file):
#         return jsonify({'error': 'No annotations found'}), 404
#     with open(annotation_file, 'r') as f:
#         annotations = json.load(f)
#     return jsonify(annotations)

# @app.route("/alignment/<video_id>")
# def alignment_mode(video_id):
#     annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_id}_annotations.json")
#     if not os.path.exists(annotation_file):
#         return render_template("error.html", message="No annotations found for this video. Please annotate the video first.")
#     with open(annotation_file, 'r') as f:
#         annotations = json.load(f)
#     return render_template(
#         "alignment.html",
#         video_id=video_id,
#         total_clips=len(annotations['timestamps']) - 1
#     )

# @app.route("/api/transcript/<video_id>")
# def get_transcript(video_id):
#     timestamps_file = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")
#     app.logger.info(f"Attempting to load word timestamps from: {timestamps_file}")
#     if not os.path.exists(timestamps_file):
#         app.logger.warning(f"Word timestamps file not found: {timestamps_file}")
#         return jsonify({
#             "status": "error",
#             "message": "No word timestamps found for this video"
#         }), 404
#     try:
#         with open(timestamps_file, 'r') as f:
#             word_data = json.load(f)
#         full_text = " ".join(item["punctuated_word"] for item in word_data)
#         words_with_times = [{
#             "word": item["punctuated_word"],
#             "start": float(item["start_time"]),
#             "end": float(item["end_time"])
#         } for item in word_data]
#         app.logger.info(f"Successfully created transcript ({len(full_text)} characters)")
#         return jsonify({
#             "status": "success",
#             "text": full_text,
#             "words": words_with_times
#         })
#     except Exception as e:
#         app.logger.error(f"Error processing word timestamps: {str(e)}")
#         return jsonify({
#             "status": "error",
#             "message": f"Error processing word timestamps: {str(e)}"
#         }), 500

# @app.route("/api/word_timestamps/<video_id>")
# def get_word_timestamps(video_id):
#     timestamps_file = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")
#     app.logger.info(f"Attempting to load word timestamps from: {timestamps_file}")
#     if not os.path.exists(timestamps_file):
#         app.logger.warning(f"Word timestamps file not found: {timestamps_file}")
#         return jsonify({
#             "status": "error",
#             "message": "No word timestamps found for this video"
#         }), 404
#     try:
#         with open(timestamps_file, 'r') as f:
#             word_data = json.load(f)
#         app.logger.info(f"Successfully loaded {len(word_data)} word timestamps")
#         return jsonify({
#             "status": "success",
#             "words": word_data
#         })
#     except Exception as e:
#         app.logger.error(f"Error processing word timestamps: {str(e)}")
#         return jsonify({
#             "status": "error",
#             "message": f"Error processing word timestamps: {str(e)}"
#         }), 500

# @app.route("/api/clips/<video_id>")
# def get_video_clips(video_id):
#     try:
#         annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_id}_annotations.json")
#         if not os.path.exists(annotation_file):
#             raise FileNotFoundError("Annotations not found")
#         with open(annotation_file, 'r') as f:
#             annotations = json.load(f)
#         timestamps = annotations['timestamps']
#         clips = []
#         for i in range(len(timestamps)-1):
#             clips.append({
#                 "index": i,
#                 "start": timestamps[i],
#                 "end": timestamps[i+1],
#                 "path": f"/clip/{video_id}/{i}"
#             })
#         return jsonify({
#             "status": "success",
#             "clips": clips
#         })
#     except Exception as e:
#         app.logger.error(f"Error getting clips: {str(e)}")
#         return jsonify({
#             "status": "error",
#             "message": str(e)
#         }), 500

# @app.route("/clip/<video_id>/<int:clip_index>")
# def serve_clip(video_id, clip_index):
#     clip_path = os.path.join(
#         TEMP_DIR,
#         f"{video_id}_clip_{clip_index:03d}.mp4"
#     )
#     app.logger.info(f"Attempting to serve clip: {clip_path}")
#     if not os.path.exists(clip_path):
#         app.logger.error(f"Clip not found: {clip_path}")
#         return jsonify({
#             "status": "error",
#             "message": "Clip not found"
#         }), 404
#     return send_file(clip_path, mimetype="video/mp4")

# @app.route("/api/save_alignments", methods=["POST"])
# def save_alignments():
#     try:
#         data = request.json
#         if not data or 'video_id' not in data or 'alignments' not in data:
#             return jsonify({'success': False, 'message': 'Invalid data'}), 400
#         output_path = os.path.join(ALIGNMENTS_DIR, f"{data['video_id']}.json")
#         with open(output_path, "w") as f:
#             json.dump(data['alignments'], f, indent=2)
#         return jsonify({
#             "success": True,
#             "message": "Alignments saved successfully"
#         })
#     except Exception as e:
#         app.logger.error(f"Error saving alignments: {str(e)}")
#         return jsonify({
#             "success": False,
#             "message": str(e)
#         }), 500

# @app.route("/api/extract_clips/<video_id>")
# def extract_clips_for_video(video_id):
#     status = clip_extraction_status.get(video_id, {})
#     if status.get("percent", 0) < 100:
#         thread = threading.Thread(target=run_clip_extraction, args=(video_id,))
#         thread.start()
#     if video_id not in transcription_progress_status or transcription_progress_status.get(video_id, {}).get("percent", 0) < 100:
#         thread_trans = threading.Thread(target=run_transcription, args=(video_id,))
#         thread_trans.start()
#     return jsonify({"status": "started"})

# @app.route("/api/clip_progress/<video_id>")
# def clip_progress(video_id):
#     progress = clip_extraction_status.get(video_id, {"current": 0, "total": 0, "percent": 0})
#     return jsonify(progress)

# @app.route("/api/transcription_progress/<video_id>")
# def transcription_progress(video_id):
#     progress = transcription_progress_status.get(video_id, {"status": "not started", "percent": 0})
#     return jsonify(progress)

# if __name__ == '__main__':
#     app.run(host='0.0.0.0', port=5000, debug=True)

from flask import Flask, render_template, jsonify, request, send_from_directory, send_file, redirect, url_for, session
import os, json, threading, time
from datetime import datetime
from extract_signed_segments_from_annotations import ClipExtractor, VideoClip
import logging
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', 'dev_key_for_testing')
logging.basicConfig(level=logging.INFO)

# Directory paths
VIDEO_DIR = os.path.abspath("data/videos")
ANNOTATIONS_DIR = os.path.abspath("data/annotations")
TEMP_DIR = os.path.abspath("data/temp")
WORD_TIMESTAMPS_DIR = os.path.abspath("data/word_timestamps")
ALIGNMENTS_DIR = os.path.abspath("data/alignments")
TRANSCRIPTS_DIR = os.path.abspath("data/transcripts")

# Ensure all required directories exist
for directory in [VIDEO_DIR, ANNOTATIONS_DIR, TEMP_DIR, WORD_TIMESTAMPS_DIR, ALIGNMENTS_DIR, TRANSCRIPTS_DIR]:
    os.makedirs(directory, exist_ok=True)

# Global dictionaries for progress tracking
clip_extraction_status = {}
transcription_progress_status = {}

# Check if we're running on Hugging Face Spaces
is_hf_space = os.getenv('SPACE_ID') is not None

# Login required decorator
def login_required(f):
    from functools import wraps
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'user' not in session:
            return redirect(url_for('login'))
        return f(*args, **kwargs)
    return decorated_function

# Allow specific users (for testing)
def is_allowed_user(username):
    allowed_users = ['Perilon']  # Add your username for testing
    return username in allowed_users or not is_hf_space  # Allow all users in local dev

def update_extraction_progress(video_id, current, total):
    percent = int((current / total) * 100)
    clip_extraction_status[video_id] = {"current": current, "total": total, "percent": percent}

def run_clip_extraction(video_id):
    try:
        base_dir = app.root_path
        extractor = ClipExtractor(base_dir)
        extractor.extract_clips_from_annotations(
            video_id,
            progress_callback=lambda current, total: update_extraction_progress(video_id, current, total)
        )
        if video_id in clip_extraction_status:
            status = clip_extraction_status[video_id]
            if status.get("percent", 0) < 100:
                update_extraction_progress(video_id, status["total"], status["total"])
        else:
            update_extraction_progress(video_id, 1, 1)
    except Exception as e:
        logging.error(f"Error during clip extraction for {video_id}: {str(e)}")
        clip_extraction_status[video_id] = {"error": str(e)}

def run_transcription(video_id):
    try:
        base_dir = app.root_path
        output_path = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")

        # Check if transcription already exists and is valid.
        if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
            app.logger.info(f"Using cached transcription for video {video_id}.")
            transcription_progress_status[video_id] = {"status": "completed", "percent": 100}
            return

        video_path = os.path.join(base_dir, "data", "videos", f"{video_id}.mp4")
        transcription_progress_status[video_id] = {"status": "started", "percent": 10}

        # Run transcription via the imported function from get_transcription_with_amazon.py
        from get_transcription_with_amazon import get_word_timestamps
        word_timestamps = get_word_timestamps(video_path)

        with open(output_path, "w") as f:
            json.dump(word_timestamps, f, indent=4)

        transcription_progress_status[video_id] = {"status": "completed", "percent": 100}
    except Exception as e:
        app.logger.error(f"Error during transcription for {video_id}: {str(e)}")
        transcription_progress_status[video_id] = {"status": "error", "percent": 0, "message": str(e)}

# Authentication routes
@app.route('/login')
def login():
    if is_hf_space:
        # For Hugging Face Spaces, redirect to the built-in OAuth
        return redirect('/auth/login')
    else:
        # For local development, just set a mock user
        session['user'] = {'name': 'LocalDeveloper', 'is_mock': True}
        return redirect(url_for('index'))

@app.route('/auth/callback')
def auth_callback():
    # This route will be called by Hugging Face after successful authentication
    if is_hf_space:
        # In Hugging Face Spaces, the user info is available in the request headers
        username = request.headers.get('X-Spaces-Username')
        if username:
            session['user'] = {'name': username, 'is_hf': True}
            return redirect(url_for('index'))
        else:
            return render_template('error.html', message="Authentication failed. No username provided.")
    return redirect(url_for('login'))

@app.route('/auth')
def auth():
    # This route will be handled by Hugging Face Spaces when deployed
    # For local development, we'll just redirect to index
    if not is_hf_space:
        session['user'] = {'name': 'Perilon', 'is_mock': True}
    return redirect(url_for('index'))

@app.before_request
def check_auth():
    # Skip authentication for login/logout routes
    if request.path in ['/login', '/logout', '/auth/callback'] or request.path.startswith('/static/'):
        return

    # In Hugging Face Spaces, check the username header
    if is_hf_space:
        username = request.headers.get('X-Spaces-Username')
        if username and is_allowed_user(username):
            # Update the session with the current user
            if 'user' not in session or session['user'].get('name') != username:
                session['user'] = {'name': username, 'is_hf': True}
        elif 'user' not in session:
            return redirect(url_for('login'))
    # For local development, we already set a mock user in the login route
    elif 'user' not in session:
        return redirect(url_for('login'))

@app.route('/logout')
def logout():
    session.clear()  # Clear the entire session
    if is_hf_space:
        return redirect('/auth/logout')
    return redirect(url_for('login'))

# Main application routes
@app.route('/')
@login_required
def index():
    return redirect(url_for('select_video'))

@app.route('/select_video')
@login_required
def select_video():
    if not os.path.exists(VIDEO_DIR):
        return render_template('error.html', message="Video directory not found.")
    videos = [f for f in os.listdir(VIDEO_DIR) if f.endswith('.mp4')]
    video_ids = [os.path.splitext(v)[0] for v in videos]
    return render_template('select_video.html', video_ids=video_ids, user=session.get('user'))

@app.route('/player/<video_id>')
@login_required
def player(video_id):
    return render_template('player.html', video_id=video_id, user=session.get('user'))

@app.route('/videos')
@login_required
def get_videos():
    if not os.path.exists(VIDEO_DIR):
        return jsonify({'error': 'Video directory not found'}), 404
    videos = [f for f in os.listdir(VIDEO_DIR) if f.endswith(('.mp4', '.avi', '.mov'))]
    if not videos:
        return jsonify({'error': 'No videos found'}), 404
    return jsonify(videos)

@app.route('/video/<path:filename>')
@login_required
def serve_video(filename):
    if not os.path.exists(os.path.join(VIDEO_DIR, filename)):
        return jsonify({'error': 'Video not found'}), 404
    return send_from_directory(VIDEO_DIR, filename)

@app.route('/save_annotations', methods=['POST'])
@login_required
def save_annotations():
    data = request.json
    if not data or 'video' not in data or 'timestamps' not in data:
        return jsonify({'success': False, 'message': 'Invalid data'}), 400

    annotation_file = os.path.join(ANNOTATIONS_DIR, f"{data['video']}_annotations.json")
    annotation_data = {
        "video_name": data['video'] + ".mp4",
        "timestamps": sorted(data['timestamps']),
        "annotation_date": datetime.now().isoformat(),
        "annotated_by": session.get('user', {}).get('name', 'unknown')
    }
    with open(annotation_file, 'w') as f:
        json.dump(annotation_data, f, indent=4)
    return jsonify({'success': True, 'message': 'Annotations saved successfully'})

@app.route('/get_annotations/<path:video_name>')
@login_required
def get_annotations(video_name):
    annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_name}_annotations.json")
    if not os.path.exists(annotation_file):
        return jsonify({'error': 'No annotations found'}), 404
    with open(annotation_file, 'r') as f:
        annotations = json.load(f)
    return jsonify(annotations)

@app.route("/alignment/<video_id>")
@login_required
def alignment_mode(video_id):
    annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_id}_annotations.json")
    if not os.path.exists(annotation_file):
        return render_template("error.html", message="No annotations found for this video. Please annotate the video first.")
    with open(annotation_file, 'r') as f:
        annotations = json.load(f)
    return render_template(
        "alignment.html",
        video_id=video_id,
        total_clips=len(annotations['timestamps']) - 1,
        user=session.get('user')
    )

@app.route("/api/transcript/<video_id>")
@login_required
def get_transcript(video_id):
    timestamps_file = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")
    app.logger.info(f"Attempting to load word timestamps from: {timestamps_file}")
    if not os.path.exists(timestamps_file):
        app.logger.warning(f"Word timestamps file not found: {timestamps_file}")
        return jsonify({
            "status": "error",
            "message": "No word timestamps found for this video"
        }), 404
    try:
        with open(timestamps_file, 'r') as f:
            word_data = json.load(f)
        full_text = " ".join(item["punctuated_word"] for item in word_data)
        words_with_times = [{
            "word": item["punctuated_word"],
            "start": float(item["start_time"]),
            "end": float(item["end_time"])
        } for item in word_data]
        app.logger.info(f"Successfully created transcript ({len(full_text)} characters)")
        return jsonify({
            "status": "success",
            "text": full_text,
            "words": words_with_times
        })
    except Exception as e:
        app.logger.error(f"Error processing word timestamps: {str(e)}")
        return jsonify({
            "status": "error",
            "message": f"Error processing word timestamps: {str(e)}"
        }), 500

@app.route("/api/word_timestamps/<video_id>")
@login_required
def get_word_timestamps(video_id):
    timestamps_file = os.path.join(WORD_TIMESTAMPS_DIR, f"{video_id}_word_timestamps.json")
    app.logger.info(f"Attempting to load word timestamps from: {timestamps_file}")
    if not os.path.exists(timestamps_file):
        app.logger.warning(f"Word timestamps file not found: {timestamps_file}")
        return jsonify({
            "status": "error",
            "message": "No word timestamps found for this video"
        }), 404
    try:
        with open(timestamps_file, 'r') as f:
            word_data = json.load(f)
        app.logger.info(f"Successfully loaded {len(word_data)} word timestamps")
        return jsonify({
            "status": "success",
            "words": word_data
        })
    except Exception as e:
        app.logger.error(f"Error processing word timestamps: {str(e)}")
        return jsonify({
            "status": "error",
            "message": f"Error processing word timestamps: {str(e)}"
        }), 500

@app.route("/api/clips/<video_id>")
@login_required
def get_video_clips(video_id):
    try:
        annotation_file = os.path.join(ANNOTATIONS_DIR, f"{video_id}_annotations.json")
        if not os.path.exists(annotation_file):
            raise FileNotFoundError("Annotations not found")
        with open(annotation_file, 'r') as f:
            annotations = json.load(f)
        timestamps = annotations['timestamps']
        clips = []
        for i in range(len(timestamps)-1):
            clips.append({
                "index": i,
                "start": timestamps[i],
                "end": timestamps[i+1],
                "path": f"/clip/{video_id}/{i}"
            })
        return jsonify({
            "status": "success",
            "clips": clips
        })
    except Exception as e:
        app.logger.error(f"Error getting clips: {str(e)}")
        return jsonify({
            "status": "error",
            "message": str(e)
        }), 500

@app.route("/clip/<video_id>/<int:clip_index>")
@login_required
def serve_clip(video_id, clip_index):
    clip_path = os.path.join(
        TEMP_DIR,
        f"{video_id}_clip_{clip_index:03d}.mp4"
    )
    app.logger.info(f"Attempting to serve clip: {clip_path}")
    if not os.path.exists(clip_path):
        app.logger.error(f"Clip not found: {clip_path}")
        return jsonify({
            "status": "error",
            "message": "Clip not found"
        }), 404
    return send_file(clip_path, mimetype="video/mp4")

@app.route("/api/save_alignments", methods=["POST"])
@login_required
def save_alignments():
    try:
        data = request.json
        if not data or 'video_id' not in data or 'alignments' not in data:
            return jsonify({'success': False, 'message': 'Invalid data'}), 400

        # Add user information to the alignments
        for alignment in data['alignments']:
            if alignment:
                alignment['aligned_by'] = session.get('user', {}).get('name', 'unknown')

        output_path = os.path.join(ALIGNMENTS_DIR, f"{data['video_id']}.json")
        with open(output_path, "w") as f:
            json.dump(data['alignments'], f, indent=2)
        return jsonify({
            "success": True,
            "message": "Alignments saved successfully"
        })
    except Exception as e:
        app.logger.error(f"Error saving alignments: {str(e)}")
        return jsonify({
            "success": False,
            "message": str(e)
        }), 500

@app.route("/api/extract_clips/<video_id>")
@login_required
def extract_clips_for_video(video_id):
    status = clip_extraction_status.get(video_id, {})
    if status.get("percent", 0) < 100:
        thread = threading.Thread(target=run_clip_extraction, args=(video_id,))
        thread.start()
    if video_id not in transcription_progress_status or transcription_progress_status.get(video_id, {}).get("percent", 0) < 100:
        thread_trans = threading.Thread(target=run_transcription, args=(video_id,))
        thread_trans.start()
    return jsonify({"status": "started"})

@app.route("/api/clip_progress/<video_id>")
@login_required
def clip_progress(video_id):
    progress = clip_extraction_status.get(video_id, {"current": 0, "total": 0, "percent": 0})
    return jsonify(progress)

@app.route("/api/transcription_progress/<video_id>")
@login_required
def transcription_progress(video_id):
    progress = transcription_progress_status.get(video_id, {"status": "not started", "percent": 0})
    return jsonify(progress)

if __name__ == '__main__':
    port = int(os.getenv('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)