katospiegel commited on
Commit
5aed115
·
1 Parent(s): 5bf6e88

feat: youtube compatibility in ephemeral

Browse files
Files changed (2) hide show
  1. app/app.py +27 -18
  2. requirements.txt +1 -1
app/app.py CHANGED
@@ -26,7 +26,7 @@ import createpdf
26
  import paragraphsCreator
27
 
28
  from pydub import AudioSegment
29
- from pytube import YouTube
30
 
31
 
32
 
@@ -448,32 +448,39 @@ def convert_mpx_to_wav(file_path):
448
  return wav_file_path
449
 
450
 
451
- def download_youtube_video(url, output_path='downloads'):
452
- if url.startswith('http://') or url.startswith('https://'):
453
- yt = YouTube(url)
454
- video = yt.streams.filter(only_audio=True).first()
455
- if not os.path.exists(output_path):
456
- os.makedirs(output_path)
457
- output_file = video.download(output_path)
 
 
 
 
 
 
 
 
 
 
 
458
  base, ext = os.path.splitext(output_file)
459
- new_file = base + '.mp4'
460
- os.rename(output_file, new_file)
461
  return new_file
462
- else:
463
- raise ValueError("The provided URL is not a valid HTTP link")
464
 
465
 
466
 
467
  def main(args):
468
- diarization, _, sample_rate = diarize_audio(args.hf_token, args.input_file)
469
-
470
  # Really dirty way to handle youtube links
471
  # We should refactor this to be more robust
472
  # with different input sources
473
- possible_link = args.input_file.replace("/odtp/odtp-input/")
474
  if possible_link.startswith('http://') or possible_link.startswith('https://'):
475
- file_path = download_youtube_video(args.input_file, output_path=os.path.dirname(args.output_file))
476
- file_path = convert_mpx_to_wav(file_path)
477
  elif args.input_file.lower().endswith('.mp3'):
478
  file_path = convert_mpx_to_wav(args.input_file)
479
  elif args.input_file.lower().endswith('.wav'):
@@ -482,6 +489,8 @@ def main(args):
482
  file_path = convert_mpx_to_wav(args.input_file)
483
  else:
484
  raise ValueError("Input file must be an MP3, WAV or MP4 file")
 
 
485
 
486
  # Create the correct ASR facade
487
  asr_model = create_asr_facade(args.model, quantize=args.quantize)
@@ -538,7 +547,7 @@ def main(args):
538
  # Process each grouped segment
539
  for start, end, speaker in grouped_segments:
540
  clip_path = f"/tmp/speaker_{speaker}_start_{start:.1f}_end_{end:.1f}.wav"
541
- clip_audio(args.input_file, sample_rate, start, end, clip_path)
542
 
543
  # Important: we call asr_model instead of model
544
  result = asr_model.transcribe(start=start, end=end, options=whisper_options)
 
26
  import paragraphsCreator
27
 
28
  from pydub import AudioSegment
29
+ import yt_dlp
30
 
31
 
32
 
 
448
  return wav_file_path
449
 
450
 
451
+ def download_youtube_video(url, output_path='/tmp'):
452
+ ydl_opts = {
453
+ 'format': 'bestaudio/best',
454
+ 'outtmpl': os.path.join(output_path, '%(title)s.%(ext)s'),
455
+ 'postprocessors': [{
456
+ 'key': 'FFmpegExtractAudio',
457
+ 'preferredcodec': 'wav',
458
+ 'preferredquality': '192',
459
+ }],
460
+ }
461
+
462
+ if not os.path.exists(output_path):
463
+ os.makedirs(output_path)
464
+
465
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
466
+ info_dict = ydl.extract_info(url, download=True)
467
+ output_file = ydl.prepare_filename(info_dict)
468
+
469
  base, ext = os.path.splitext(output_file)
470
+ new_file = base + '.wav'
471
+ print(new_file)
472
  return new_file
 
 
473
 
474
 
475
 
476
  def main(args):
 
 
477
  # Really dirty way to handle youtube links
478
  # We should refactor this to be more robust
479
  # with different input sources
480
+ possible_link = args.input_file.replace("/odtp/odtp-input/", "")
481
  if possible_link.startswith('http://') or possible_link.startswith('https://'):
482
+ file_path = download_youtube_video(possible_link, output_path=os.path.dirname(args.output_file))
483
+ #file_path = convert_mpx_to_wav(file_path)
484
  elif args.input_file.lower().endswith('.mp3'):
485
  file_path = convert_mpx_to_wav(args.input_file)
486
  elif args.input_file.lower().endswith('.wav'):
 
489
  file_path = convert_mpx_to_wav(args.input_file)
490
  else:
491
  raise ValueError("Input file must be an MP3, WAV or MP4 file")
492
+
493
+ diarization, _, sample_rate = diarize_audio(args.hf_token, file_path)
494
 
495
  # Create the correct ASR facade
496
  asr_model = create_asr_facade(args.model, quantize=args.quantize)
 
547
  # Process each grouped segment
548
  for start, end, speaker in grouped_segments:
549
  clip_path = f"/tmp/speaker_{speaker}_start_{start:.1f}_end_{end:.1f}.wav"
550
+ clip_audio(file_path, sample_rate, start, end, clip_path)
551
 
552
  # Important: we call asr_model instead of model
553
  result = asr_model.transcribe(start=start, end=end, options=whisper_options)
requirements.txt CHANGED
@@ -10,4 +10,4 @@ gradio==5.5.0
10
  numpy==1.24.4
11
  md2pdf==1.0.1
12
  transformers==4.48.0
13
- pytube
 
10
  numpy==1.24.4
11
  md2pdf==1.0.1
12
  transformers==4.48.0
13
+ yt-dlp