File size: 32,863 Bytes
43cd37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# Audio_Files.py
#########################################
# Audio Processing Library
# This library is used to download or load audio files from a local directory.
#
####
#
# Functions:
#
# download_audio_file(url, save_path)
# process_audio(
# process_audio_file(audio_url, audio_file, whisper_model="small.en", api_name=None, api_key=None)
#
#
#########################################
# Imports
import json
import logging
import os
import subprocess
import tempfile
import time
import uuid
from datetime import datetime
from pathlib import Path
#
# External Imports
import requests
import yt_dlp
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import add_media_with_keywords, \
    check_media_and_whisper_model
from App_Function_Libraries.Metrics.metrics_logger import log_counter, log_histogram
from App_Function_Libraries.Summarization.Summarization_General_Lib import perform_summarization
from App_Function_Libraries.Utils.Utils import downloaded_files, \
    sanitize_filename, generate_unique_id, temp_files
from App_Function_Libraries.Video_DL_Ingestion_Lib import extract_metadata
from App_Function_Libraries.Audio.Audio_Transcription_Lib import speech_to_text
from App_Function_Libraries.Chunk_Lib import improved_chunking_process
#
#######################################################################################################################
# Function Definitions
#

MAX_FILE_SIZE = 500 * 1024 * 1024


def download_audio_file(url, current_whisper_model="", use_cookies=False, cookies=None):
    try:
        # Check if media already exists in the database and compare whisper models
        should_download, reason = check_media_and_whisper_model(
            url=url,
            current_whisper_model=current_whisper_model
        )

        if not should_download:
            logging.info(f"Skipping audio download: {reason}")
            return None

        logging.info(f"Proceeding with audio download: {reason}")

        # Set up the request headers
        headers = {}
        if use_cookies and cookies:
            try:
                cookie_dict = json.loads(cookies)
                headers['Cookie'] = '; '.join([f'{k}={v}' for k, v in cookie_dict.items()])
            except json.JSONDecodeError:
                logging.warning("Invalid cookie format. Proceeding without cookies.")

        # Make the request
        response = requests.get(url, headers=headers, stream=True)
        # Raise an exception for bad status codes
        response.raise_for_status()

        # Get the file size
        file_size = int(response.headers.get('content-length', 0))
        if file_size > 500 * 1024 * 1024:  # 500 MB limit
            raise ValueError("File size exceeds the 500MB limit.")

        # Generate a unique filename
        file_name = f"audio_{uuid.uuid4().hex[:8]}.mp3"
        save_path = os.path.join('downloads', file_name)

        # Ensure the downloads directory exists
        os.makedirs('downloads', exist_ok=True)


        # Download the file
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)

        logging.info(f"Audio file downloaded successfully: {save_path}")
        return save_path

    except requests.RequestException as e:
        logging.error(f"Error downloading audio file: {str(e)}")
        raise
    except ValueError as e:
        logging.error(str(e))
        raise
    except Exception as e:
        logging.error(f"Unexpected error downloading audio file: {str(e)}")
        raise

def process_audio_files(audio_urls, audio_file, whisper_model, api_name, api_key, use_cookies, cookies, keep_original,

                        custom_keywords, custom_prompt_input, chunk_method, max_chunk_size, chunk_overlap,

                        use_adaptive_chunking, use_multi_level_chunking, chunk_language, diarize,

                        keep_timestamps, custom_title):

    start_time = time.time()  # Start time for processing
    processed_count = 0
    failed_count = 0
    progress = []
    all_transcriptions = []
    all_summaries = []
    #v2
    def format_transcription_with_timestamps(segments):
        if keep_timestamps:
            formatted_segments = []
            for segment in segments:
                start = segment.get('Time_Start', 0)
                end = segment.get('Time_End', 0)
                text = segment.get('Text', '').strip()  # Ensure text is stripped of leading/trailing spaces

                # Add the formatted timestamp and text to the list, followed by a newline
                formatted_segments.append(f"[{start:.2f}-{end:.2f}] {text}")

            # Join the segments with a newline to ensure proper formatting
            return "\n".join(formatted_segments)
        else:
            # Join the text without timestamps
            return "\n".join([segment.get('Text', '').strip() for segment in segments])

    def update_progress(message):
        progress.append(message)
        return "\n".join(progress)

    def cleanup_files():
        for file in temp_files:
            try:
                if os.path.exists(file):
                    os.remove(file)
                    update_progress(f"Temporary file {file} removed.")
            except Exception as e:
                update_progress(f"Failed to remove temporary file {file}: {str(e)}")

    def reencode_mp3(mp3_file_path):
        try:
            reencoded_mp3_path = mp3_file_path.replace(".mp3", "_reencoded.mp3")
            subprocess.run([ffmpeg_cmd, '-i', mp3_file_path, '-codec:a', 'libmp3lame', reencoded_mp3_path], check=True)
            update_progress(f"Re-encoded {mp3_file_path} to {reencoded_mp3_path}.")
            return reencoded_mp3_path
        except subprocess.CalledProcessError as e:
            update_progress(f"Error re-encoding {mp3_file_path}: {str(e)}")
            raise

    def convert_mp3_to_wav(mp3_file_path):
        try:
            wav_file_path = mp3_file_path.replace(".mp3", ".wav")
            subprocess.run([ffmpeg_cmd, '-i', mp3_file_path, wav_file_path], check=True)
            update_progress(f"Converted {mp3_file_path} to {wav_file_path}.")
            return wav_file_path
        except subprocess.CalledProcessError as e:
            update_progress(f"Error converting {mp3_file_path} to WAV: {str(e)}")
            raise

    try:
        # Check and set the ffmpeg command
        global ffmpeg_cmd
        if os.name == "nt":
            logging.debug("Running on Windows")
            ffmpeg_cmd = os.path.join(os.getcwd(), "Bin", "ffmpeg.exe")
        else:
            ffmpeg_cmd = 'ffmpeg'  # Assume 'ffmpeg' is in PATH for non-Windows systems

        # Ensure ffmpeg is accessible
        if not os.path.exists(ffmpeg_cmd) and os.name == "nt":
            raise FileNotFoundError(f"ffmpeg executable not found at path: {ffmpeg_cmd}")

        # Define chunk options early to avoid undefined errors
        chunk_options = {
            'method': chunk_method,
            'max_size': max_chunk_size,
            'overlap': chunk_overlap,
            'adaptive': use_adaptive_chunking,
            'multi_level': use_multi_level_chunking,
            'language': chunk_language
        }

        # Process multiple URLs
        urls = [url.strip() for url in audio_urls.split('\n') if url.strip()]

        for i, url in enumerate(urls):
            update_progress(f"Processing URL {i + 1}/{len(urls)}: {url}")

            # Download and process audio file
            audio_file_path = download_audio_file(url, use_cookies, cookies)
            if not os.path.exists(audio_file_path):
                update_progress(f"Downloaded file not found: {audio_file_path}")
                failed_count += 1
                log_counter(
                    metric_name="audio_files_failed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )
                continue

            temp_files.append(audio_file_path)
            update_progress("Audio file downloaded successfully.")

            # Re-encode MP3 to fix potential issues
            reencoded_mp3_path = reencode_mp3(audio_file_path)
            if not os.path.exists(reencoded_mp3_path):
                update_progress(f"Re-encoded file not found: {reencoded_mp3_path}")
                failed_count += 1
                log_counter(
                    metric_name="audio_files_failed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )
                continue

            temp_files.append(reencoded_mp3_path)

            # Convert re-encoded MP3 to WAV
            wav_file_path = convert_mp3_to_wav(reencoded_mp3_path)
            if not os.path.exists(wav_file_path):
                update_progress(f"Converted WAV file not found: {wav_file_path}")
                failed_count += 1
                log_counter(
                    metric_name="audio_files_failed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )
                continue

            temp_files.append(wav_file_path)

            # Initialize transcription
            transcription = ""

            # Transcribe audio
            if diarize:
                segments = speech_to_text(wav_file_path, whisper_model=whisper_model, diarize=True)
            else:
                segments = speech_to_text(wav_file_path, whisper_model=whisper_model)

            # Handle segments nested under 'segments' key
            if isinstance(segments, dict) and 'segments' in segments:
                segments = segments['segments']

            if isinstance(segments, list):
                # Log first 5 segments for debugging
                logging.debug(f"Segments before formatting: {segments[:5]}")
                transcription = format_transcription_with_timestamps(segments)
                logging.debug(f"Formatted transcription (first 500 chars): {transcription[:500]}")
                update_progress("Audio transcribed successfully.")
            else:
                update_progress("Unexpected segments format received from speech_to_text.")
                logging.error(f"Unexpected segments format: {segments}")
                failed_count += 1
                log_counter(
                    metric_name="audio_files_failed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )
                continue

            if not transcription.strip():
                update_progress("Transcription is empty.")
                failed_count += 1
                log_counter(
                    metric_name="audio_files_failed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )
            else:
                # Apply chunking
                chunked_text = improved_chunking_process(transcription, chunk_options)

                # Summarize
                logging.debug(f"Audio Transcription API Name: {api_name}")
                if api_name:
                    try:
                        summary = perform_summarization(api_name, chunked_text, custom_prompt_input, api_key)
                        update_progress("Audio summarized successfully.")
                    except Exception as e:
                        logging.error(f"Error during summarization: {str(e)}")
                        summary = "Summary generation failed"
                        failed_count += 1
                        log_counter(
                            metric_name="audio_files_failed_total",
                            labels={"whisper_model": whisper_model, "api_name": api_name},
                            value=1
                        )
                else:
                    summary = "No summary available (API not provided)"

                all_transcriptions.append(transcription)
                all_summaries.append(summary)

                # Use custom_title if provided, otherwise use the original filename
                title = custom_title if custom_title else os.path.basename(wav_file_path)

                # Add to database
                add_media_with_keywords(
                    url=url,
                    title=title,
                    media_type='audio',
                    content=transcription,
                    keywords=custom_keywords,
                    prompt=custom_prompt_input,
                    summary=summary,
                    transcription_model=whisper_model,
                    author="Unknown",
                    ingestion_date=datetime.now().strftime('%Y-%m-%d')
                )
                update_progress("Audio file processed and added to database.")
                processed_count += 1
                log_counter(
                    metric_name="audio_files_processed_total",
                    labels={"whisper_model": whisper_model, "api_name": api_name},
                    value=1
                )

        # Process uploaded file if provided
        if audio_file:
            url = generate_unique_id()
            if os.path.getsize(audio_file.name) > MAX_FILE_SIZE:
                update_progress(
                    f"Uploaded file size exceeds the maximum limit of {MAX_FILE_SIZE / (1024 * 1024):.2f}MB. Skipping this file.")
            else:
                try:
                    # Re-encode MP3 to fix potential issues
                    reencoded_mp3_path = reencode_mp3(audio_file.name)
                    if not os.path.exists(reencoded_mp3_path):
                        update_progress(f"Re-encoded file not found: {reencoded_mp3_path}")
                        return update_progress("Processing failed: Re-encoded file not found"), "", ""

                    temp_files.append(reencoded_mp3_path)

                    # Convert re-encoded MP3 to WAV
                    wav_file_path = convert_mp3_to_wav(reencoded_mp3_path)
                    if not os.path.exists(wav_file_path):
                        update_progress(f"Converted WAV file not found: {wav_file_path}")
                        return update_progress("Processing failed: Converted WAV file not found"), "", ""

                    temp_files.append(wav_file_path)

                    # Initialize transcription
                    transcription = ""

                    if diarize:
                        segments = speech_to_text(wav_file_path, whisper_model=whisper_model, diarize=True)
                    else:
                        segments = speech_to_text(wav_file_path, whisper_model=whisper_model)

                    # Handle segments nested under 'segments' key
                    if isinstance(segments, dict) and 'segments' in segments:
                        segments = segments['segments']

                    if isinstance(segments, list):
                        transcription = format_transcription_with_timestamps(segments)
                    else:
                        update_progress("Unexpected segments format received from speech_to_text.")
                        logging.error(f"Unexpected segments format: {segments}")

                    chunked_text = improved_chunking_process(transcription, chunk_options)

                    logging.debug(f"Audio Transcription API Name: {api_name}")
                    if api_name:
                        try:
                            summary = perform_summarization(api_name, chunked_text, custom_prompt_input, api_key)
                            update_progress("Audio summarized successfully.")
                        except Exception as e:
                            logging.error(f"Error during summarization: {str(e)}")
                            summary = "Summary generation failed"
                    else:
                        summary = "No summary available (API not provided)"

                    all_transcriptions.append(transcription)
                    all_summaries.append(summary)

                    # Use custom_title if provided, otherwise use the original filename
                    title = custom_title if custom_title else os.path.basename(wav_file_path)

                    add_media_with_keywords(
                        url="Uploaded File",
                        title=title,
                        media_type='audio',
                        content=transcription,
                        keywords=custom_keywords,
                        prompt=custom_prompt_input,
                        summary=summary,
                        transcription_model=whisper_model,
                        author="Unknown",
                        ingestion_date=datetime.now().strftime('%Y-%m-%d')
                    )
                    update_progress("Uploaded file processed and added to database.")
                    processed_count += 1
                    log_counter(
                        metric_name="audio_files_processed_total",
                        labels={"whisper_model": whisper_model, "api_name": api_name},
                        value=1
                    )
                except Exception as e:
                    update_progress(f"Error processing uploaded file: {str(e)}")
                    logging.error(f"Error processing uploaded file: {str(e)}")
                    failed_count += 1
                    log_counter(
                        metric_name="audio_files_failed_total",
                        labels={"whisper_model": whisper_model, "api_name": api_name},
                        value=1
                    )
                    return update_progress("Processing failed: Error processing uploaded file"), "", ""
        # Final cleanup
        if not keep_original:
            cleanup_files()

        end_time = time.time()
        processing_time = end_time - start_time
        # Log processing time
        log_histogram(
            metric_name="audio_processing_time_seconds",
            value=processing_time,
            labels={"whisper_model": whisper_model, "api_name": api_name}
        )

        # Optionally, log total counts
        log_counter(
            metric_name="total_audio_files_processed",
            labels={"whisper_model": whisper_model, "api_name": api_name},
            value=processed_count
        )

        log_counter(
            metric_name="total_audio_files_failed",
            labels={"whisper_model": whisper_model, "api_name": api_name},
            value=failed_count
        )


        final_progress = update_progress("All processing complete.")
        final_transcriptions = "\n\n".join(all_transcriptions)
        final_summaries = "\n\n".join(all_summaries)

        return final_progress, final_transcriptions, final_summaries

    except Exception as e:
        logging.error(f"Error processing audio files: {str(e)}")
        log_counter(
            metric_name="audio_files_failed_total",
            labels={"whisper_model": whisper_model, "api_name": api_name},
            value=1
        )
        cleanup_files()
        return update_progress(f"Processing failed: {str(e)}"), "", ""


def format_transcription_with_timestamps(segments, keep_timestamps):
    """

    Formats the transcription segments with or without timestamps.



    Parameters:

        segments (list): List of transcription segments.

        keep_timestamps (bool): Whether to include timestamps.



    Returns:

        str: Formatted transcription.

    """
    if keep_timestamps:
        formatted_segments = []
        for segment in segments:
            start = segment.get('Time_Start', 0)
            end = segment.get('Time_End', 0)
            text = segment.get('Text', '').strip()

            formatted_segments.append(f"[{start:.2f}-{end:.2f}] {text}")
        return "\n".join(formatted_segments)
    else:
        return "\n".join([segment.get('Text', '').strip() for segment in segments])


def download_youtube_audio(url):
    try:
        # Determine ffmpeg path based on the operating system.
        ffmpeg_path = './Bin/ffmpeg.exe' if os.name == 'nt' else 'ffmpeg'

        # Create a temporary directory
        with tempfile.TemporaryDirectory() as temp_dir:
            # Extract information about the video
            with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
                info_dict = ydl.extract_info(url, download=False)
                sanitized_title = sanitize_filename(info_dict['title'])

            # Setup the temporary filenames
            temp_video_path = Path(temp_dir) / f"{sanitized_title}_temp.mp4"
            temp_audio_path = Path(temp_dir) / f"{sanitized_title}.mp3"

            # Initialize yt-dlp with options for downloading
            ydl_opts = {
                'format': 'bestaudio[ext=m4a]/best[height<=480]',  # Prefer best audio, or video up to 480p
                'ffmpeg_location': ffmpeg_path,
                'outtmpl': str(temp_video_path),
                'noplaylist': True,
                'quiet': True
            }

            # Execute yt-dlp to download the video/audio
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                ydl.download([url])

            # Check if the file exists
            if not temp_video_path.exists():
                raise FileNotFoundError(f"Expected file was not found: {temp_video_path}")

            # Use ffmpeg to extract audio
            ffmpeg_command = [
                ffmpeg_path,
                '-i', str(temp_video_path),
                '-vn',  # No video
                '-acodec', 'libmp3lame',
                '-b:a', '192k',
                str(temp_audio_path)
            ]
            subprocess.run(ffmpeg_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

            # Check if the audio file was created
            if not temp_audio_path.exists():
                raise FileNotFoundError(f"Expected audio file was not found: {temp_audio_path}")

            # Create a persistent directory for the download if it doesn't exist
            persistent_dir = Path("downloads")
            persistent_dir.mkdir(exist_ok=True)

            # Move the file from the temporary directory to the persistent directory
            persistent_file_path = persistent_dir / f"{sanitized_title}.mp3"
            os.replace(str(temp_audio_path), str(persistent_file_path))

            # Add the file to the list of downloaded files
            downloaded_files.append(str(persistent_file_path))

            return str(persistent_file_path), f"Audio downloaded successfully: {sanitized_title}.mp3"
    except Exception as e:
        return None, f"Error downloading audio: {str(e)}"


def process_podcast(url, title, author, keywords, custom_prompt, api_name, api_key, whisper_model,

                    keep_original=False, enable_diarization=False, use_cookies=False, cookies=None,

                    chunk_method=None, max_chunk_size=300, chunk_overlap=0, use_adaptive_chunking=False,

                    use_multi_level_chunking=False, chunk_language='english', keep_timestamps=True):
    """

    Processes a podcast by downloading the audio, transcribing it, summarizing the transcription,

    and adding the results to the database. Metrics are logged throughout the process.



    Parameters:

        url (str): URL of the podcast.

        title (str): Title of the podcast.

        author (str): Author of the podcast.

        keywords (str): Comma-separated keywords.

        custom_prompt (str): Custom prompt for summarization.

        api_name (str): API name for summarization.

        api_key (str): API key for summarization.

        whisper_model (str): Whisper model to use for transcription.

        keep_original (bool): Whether to keep the original audio file.

        enable_diarization (bool): Whether to enable speaker diarization.

        use_cookies (bool): Whether to use cookies for authenticated downloads.

        cookies (str): JSON-formatted cookies string.

        chunk_method (str): Method for chunking text.

        max_chunk_size (int): Maximum size for each text chunk.

        chunk_overlap (int): Overlap size between chunks.

        use_adaptive_chunking (bool): Whether to use adaptive chunking.

        use_multi_level_chunking (bool): Whether to use multi-level chunking.

        chunk_language (str): Language for chunking.

        keep_timestamps (bool): Whether to keep timestamps in transcription.



    Returns:

        tuple: (progress_message, transcription, summary, title, author, keywords, error_message)

    """
    start_time = time.time()  # Start time for processing
    error_message = ""
    temp_files = []

    # Define labels for metrics
    labels = {
        "whisper_model": whisper_model,
        "api_name": api_name if api_name else "None"
    }

    def update_progress(message):
        """

        Updates the progress messages.



        Parameters:

            message (str): Progress message to append.



        Returns:

            str: Combined progress messages.

        """
        progress.append(message)
        return "\n".join(progress)

    def cleanup_files():
        if not keep_original:
            for file in temp_files:
                try:
                    if os.path.exists(file):
                        os.remove(file)
                        update_progress(f"Temporary file {file} removed.")
                except Exception as e:
                    update_progress(f"Failed to remove temporary file {file}: {str(e)}")

    progress = []  # Initialize progress messages

    try:
        # Handle cookies if required
        if use_cookies:
            cookies = json.loads(cookies)

        # Download the podcast audio file
        audio_file = download_audio_file(url, whisper_model, use_cookies, cookies)
        if not audio_file:
            raise RuntimeError("Failed to download podcast audio.")
        temp_files.append(audio_file)
        update_progress("Podcast downloaded successfully.")

        # Extract metadata from the podcast
        metadata = extract_metadata(url)
        title = title or metadata.get('title', 'Unknown Podcast')
        author = author or metadata.get('uploader', 'Unknown Author')

        # Format metadata for storage
        metadata_text = f"""

Metadata:

Title: {title}

Author: {author}

Series: {metadata.get('series', 'N/A')}

Episode: {metadata.get('episode', 'N/A')}

Season: {metadata.get('season', 'N/A')}

Upload Date: {metadata.get('upload_date', 'N/A')}

Duration: {metadata.get('duration', 'N/A')} seconds

Description: {metadata.get('description', 'N/A')}

"""

        # Update keywords with metadata information
        new_keywords = []
        if metadata.get('series'):
            new_keywords.append(f"series:{metadata['series']}")
        if metadata.get('episode'):
            new_keywords.append(f"episode:{metadata['episode']}")
        if metadata.get('season'):
            new_keywords.append(f"season:{metadata['season']}")

        keywords = f"{keywords},{','.join(new_keywords)}" if keywords else ','.join(new_keywords)
        update_progress(f"Metadata extracted - Title: {title}, Author: {author}, Keywords: {keywords}")

        # Transcribe the podcast audio
        try:
            if enable_diarization:
                segments = speech_to_text(audio_file, whisper_model=whisper_model, diarize=True)
            else:
                segments = speech_to_text(audio_file, whisper_model=whisper_model)
            # SEems like this could be optimized... FIXME
            def format_segment(segment):
                start = segment.get('start', 0)
                end = segment.get('end', 0)
                text = segment.get('Text', '')

            if isinstance(segments, dict) and 'segments' in segments:
                segments = segments['segments']

            if isinstance(segments, list):
                transcription = format_transcription_with_timestamps(segments, keep_timestamps)
                update_progress("Podcast transcribed successfully.")
            else:
                raise ValueError("Unexpected segments format received from speech_to_text.")

            if not transcription.strip():
                raise ValueError("Transcription is empty.")
        except Exception as e:
            error_message = f"Transcription failed: {str(e)}"
            raise RuntimeError(error_message)

        # Apply chunking to the transcription
        chunk_options = {
            'method': chunk_method,
            'max_size': max_chunk_size,
            'overlap': chunk_overlap,
            'adaptive': use_adaptive_chunking,
            'multi_level': use_multi_level_chunking,
            'language': chunk_language
        }
        chunked_text = improved_chunking_process(transcription, chunk_options)

        # Combine metadata and transcription
        full_content = metadata_text + "\n\nTranscription:\n" + transcription

        # Summarize the transcription if API is provided
        summary = None
        if api_name:
            try:
                summary = perform_summarization(api_name, chunked_text, custom_prompt, api_key)
                update_progress("Podcast summarized successfully.")
            except Exception as e:
                error_message = f"Summarization failed: {str(e)}"
                raise RuntimeError(error_message)
        else:
            summary = "No summary available (API not provided)"

        # Add the processed podcast to the database
        try:
            add_media_with_keywords(
                url=url,
                title=title,
                media_type='podcast',
                content=full_content,
                keywords=keywords,
                prompt=custom_prompt,
                summary=summary or "No summary available",
                transcription_model=whisper_model,
                author=author,
                ingestion_date=datetime.now().strftime('%Y-%m-%d')
            )
            update_progress("Podcast added to database successfully.")
        except Exception as e:
            error_message = f"Error adding podcast to database: {str(e)}"
            raise RuntimeError(error_message)

        # Cleanup temporary files if required
        cleanup_files()

        # Calculate processing time
        end_time = time.time()
        processing_time = end_time - start_time

        # Log successful processing
        log_counter(
            metric_name="podcasts_processed_total",
            labels=labels,
            value=1
        )

        # Log processing time
        log_histogram(
            metric_name="podcast_processing_time_seconds",
            value=processing_time,
            labels=labels
        )

        # Return the final outputs
        final_progress = update_progress("Processing complete.")
        return (final_progress, full_content, summary or "No summary generated.",
                title, author, keywords, error_message)

    except Exception as e:
        # Calculate processing time up to the point of failure
        end_time = time.time()
        processing_time = end_time - start_time

        # Log failed processing
        log_counter(
            metric_name="podcasts_failed_total",
            labels=labels,
            value=1
        )

        # Log processing time even on failure
        log_histogram(
            metric_name="podcast_processing_time_seconds",
            value=processing_time,
            labels=labels
        )

        logging.error(f"Error processing podcast: {str(e)}")
        cleanup_files()
        final_progress = update_progress(f"Processing failed: {str(e)}")
        return (final_progress, "", "", "", "", "", str(e))


#
#
#######################################################################################################################