Adjoumani commited on
Commit
b7ec892
·
verified ·
1 Parent(s): 433231c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -3
app.py CHANGED
@@ -385,10 +385,16 @@ class AudioProcessor:
385
  time.sleep(60)
386
  return self._combine_summaries(summaries)
387
  raise e"""
 
 
 
 
 
388
 
389
  class VideoProcessor:
390
  def __init__(self):
391
  self.supported_formats = ['.mp4', '.avi', '.mov', '.mkv']
 
392
  self.ydl_opts = {
393
  'format': 'bestaudio/best',
394
  'postprocessors': [{
@@ -396,7 +402,8 @@ class VideoProcessor:
396
  'preferredcodec': 'mp3',
397
  'preferredquality': '192',
398
  }],
399
- 'outtmpl': 'temp_audio.%(ext)s'
 
400
  }
401
 
402
 
@@ -424,7 +431,7 @@ class VideoProcessor:
424
  ydl.download([url])
425
  return 'temp_audio.mp3' """
426
 
427
- def download_youtube_audio(self, url: str) -> str:
428
  try:
429
  # Fichier cookies
430
  cookie_file_path = "cookies.txt"
@@ -452,11 +459,44 @@ class VideoProcessor:
452
 
453
  return audio_path
454
  except Exception as e:
455
- raise RuntimeError(f"Erreur lors du téléchargement : {str(e)}")
456
 
457
 
458
 
 
459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  def extract_audio_from_video(self, video_path: str) -> str:
461
  try:
462
  audio_path = f"{os.path.splitext(video_path)[0]}.mp3"
 
385
  time.sleep(60)
386
  return self._combine_summaries(summaries)
387
  raise e"""
388
+
389
+ from urllib.parse import urlparse, parse_qs
390
+ from typing import Optional
391
+ import subprocess
392
+
393
 
394
  class VideoProcessor:
395
  def __init__(self):
396
  self.supported_formats = ['.mp4', '.avi', '.mov', '.mkv']
397
+ self.cookie_file_path = "cookies.txt" # Fichier cookies utilisé par yt-dlp
398
  self.ydl_opts = {
399
  'format': 'bestaudio/best',
400
  'postprocessors': [{
 
402
  'preferredcodec': 'mp3',
403
  'preferredquality': '192',
404
  }],
405
+ 'outtmpl': 'temp_audio.%(ext)s',
406
+ 'cookiefile': self.cookie_file_path # Ajout des cookies ici
407
  }
408
 
409
 
 
431
  ydl.download([url])
432
  return 'temp_audio.mp3' """
433
 
434
+ """def download_youtube_audio(self, url: str) -> str:
435
  try:
436
  # Fichier cookies
437
  cookie_file_path = "cookies.txt"
 
459
 
460
  return audio_path
461
  except Exception as e:
462
+ raise RuntimeError(f"Erreur lors du téléchargement : {str(e)}")"""
463
 
464
 
465
 
466
+
467
 
468
+ def update_cookies(self):
469
+ """
470
+ Met à jour les cookies en les récupérant dynamiquement depuis le navigateur ou une commande yt-dlp.
471
+ """
472
+ try:
473
+ # Exécuter une commande yt-dlp pour récupérer les cookies du navigateur
474
+ subprocess.run(
475
+ ["yt-dlp", "--cookies-from-browser", "chrome", "--dump-cookies", self.cookie_file_path],
476
+ check=True
477
+ )
478
+ print(f"Cookies mis à jour et enregistrés dans {self.cookie_file_path}.")
479
+ except Exception as e:
480
+ raise RuntimeError(f"Erreur lors de la mise à jour des cookies : {str(e)}")
481
+
482
+ def download_youtube_audio(self, url: str) -> str:
483
+ try:
484
+ # Mettre à jour les cookies avant chaque téléchargement
485
+ self.update_cookies()
486
+
487
+ # Téléchargement avec yt-dlp
488
+ with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
489
+ ydl.download([url])
490
+
491
+ # Vérifier si le fichier audio existe
492
+ audio_path = 'temp_audio.mp3'
493
+ if not os.path.exists(audio_path):
494
+ raise FileNotFoundError(f"Le fichier {audio_path} n'a pas été généré.")
495
+
496
+ return audio_path
497
+ except Exception as e:
498
+ raise RuntimeError(f"Erreur lors du téléchargement : {str(e)}")
499
+
500
  def extract_audio_from_video(self, video_path: str) -> str:
501
  try:
502
  audio_path = f"{os.path.splitext(video_path)[0]}.mp3"