Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -122,77 +122,77 @@ def is_youtube_url(url):
|
|
| 122 |
|
| 123 |
|
| 124 |
def download_youtube_video(url):
|
| 125 |
-
"""Télécharge une vidéo YouTube en utilisant
|
| 126 |
try:
|
| 127 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 128 |
output_template = os.path.join(temp_dir, '%(title)s.%(ext)s')
|
| 129 |
|
| 130 |
-
#
|
| 131 |
command = [
|
| 132 |
'yt-dlp',
|
| 133 |
'--format', 'best[filesize<50M]',
|
| 134 |
'--quiet',
|
| 135 |
'--no-warnings',
|
| 136 |
'--output', output_template,
|
| 137 |
-
'--
|
| 138 |
-
'--
|
| 139 |
-
'--
|
| 140 |
-
'--
|
| 141 |
-
'--
|
| 142 |
-
'--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
url
|
| 144 |
]
|
| 145 |
|
| 146 |
logger.info(f"Début du téléchargement de la vidéo: {url}")
|
| 147 |
|
| 148 |
-
# Check if yt-dlp is installed
|
| 149 |
try:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
| 156 |
command,
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
universal_newlines=True
|
| 160 |
)
|
| 161 |
|
| 162 |
-
stdout, stderr = process.communicate()
|
| 163 |
-
|
| 164 |
if process.returncode != 0:
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
universal_newlines=True
|
| 188 |
-
)
|
| 189 |
-
stdout, stderr = process.communicate()
|
| 190 |
-
if process.returncode != 0:
|
| 191 |
-
logger.error(f"Erreur yt-dlp: {stderr}")
|
| 192 |
-
raise Exception(f"Erreur yt-dlp: {stderr}")
|
| 193 |
-
else:
|
| 194 |
-
logger.error(f"Erreur yt-dlp: {stderr}")
|
| 195 |
-
raise Exception(f"Erreur yt-dlp: {stderr}")
|
| 196 |
|
| 197 |
downloaded_files = os.listdir(temp_dir)
|
| 198 |
if not downloaded_files:
|
|
@@ -201,6 +201,14 @@ def download_youtube_video(url):
|
|
| 201 |
|
| 202 |
video_path = os.path.join(temp_dir, downloaded_files[0])
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(video_path)[1])
|
| 205 |
with open(video_path, 'rb') as f:
|
| 206 |
temp_file.write(f.read())
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
def download_youtube_video(url):
|
| 125 |
+
"""Télécharge une vidéo YouTube en utilisant yt-dlp avec une configuration adaptée aux conteneurs"""
|
| 126 |
try:
|
| 127 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 128 |
output_template = os.path.join(temp_dir, '%(title)s.%(ext)s')
|
| 129 |
|
| 130 |
+
# Configuration optimisée pour environnement conteneurisé
|
| 131 |
command = [
|
| 132 |
'yt-dlp',
|
| 133 |
'--format', 'best[filesize<50M]',
|
| 134 |
'--quiet',
|
| 135 |
'--no-warnings',
|
| 136 |
'--output', output_template,
|
| 137 |
+
'--extract-audio', # Tenter d'abord l'extraction audio si la vidéo échoue
|
| 138 |
+
'--format-sort', 'res,ext:mp4:m4a', # Prioriser les formats plus légers
|
| 139 |
+
'--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 140 |
+
'--referer', 'https://www.youtube.com/',
|
| 141 |
+
'--geo-bypass', # Contourner les restrictions géographiques
|
| 142 |
+
'--no-check-certificates', # Ignorer les erreurs de certificat
|
| 143 |
+
'--force-ipv4', # Forcer IPv4 pour plus de stabilité
|
| 144 |
+
'--ignore-errors', # Continuer en cas d'erreurs mineures
|
| 145 |
+
'--no-playlist', # Éviter le téléchargement de playlists
|
| 146 |
+
'--extractor-retries', '3',
|
| 147 |
+
'--file-access-retries', '3',
|
| 148 |
+
'--fragment-retries', '3',
|
| 149 |
+
'--retry-sleep', '5',
|
| 150 |
url
|
| 151 |
]
|
| 152 |
|
| 153 |
logger.info(f"Début du téléchargement de la vidéo: {url}")
|
| 154 |
|
|
|
|
| 155 |
try:
|
| 156 |
+
# Vérifier la version de yt-dlp
|
| 157 |
+
version_process = subprocess.run(['yt-dlp', '--version'],
|
| 158 |
+
capture_output=True,
|
| 159 |
+
text=True,
|
| 160 |
+
check=True)
|
| 161 |
+
logger.info(f"Version de yt-dlp: {version_process.stdout.strip()}")
|
| 162 |
+
except subprocess.SubprocessError as e:
|
| 163 |
+
logger.error(f"Erreur lors de la vérification de yt-dlp: {e}")
|
| 164 |
+
raise RuntimeError("yt-dlp n'est pas correctement installé")
|
| 165 |
|
| 166 |
+
# Premier essai avec la configuration standard
|
| 167 |
+
process = subprocess.run(
|
| 168 |
command,
|
| 169 |
+
capture_output=True,
|
| 170 |
+
text=True
|
|
|
|
| 171 |
)
|
| 172 |
|
|
|
|
|
|
|
| 173 |
if process.returncode != 0:
|
| 174 |
+
logger.warning(f"Premier essai échoué: {process.stderr}")
|
| 175 |
+
logger.info("Tentative avec configuration alternative...")
|
| 176 |
+
|
| 177 |
+
# Configuration alternative
|
| 178 |
+
alternative_command = command.copy()
|
| 179 |
+
alternative_command.extend([
|
| 180 |
+
'--format', 'worst', # Utiliser la qualité la plus basse
|
| 181 |
+
'--prefer-insecure', # Préférer les connexions non-sécurisées si nécessaire
|
| 182 |
+
'--socket-timeout', '30',
|
| 183 |
+
'--external-downloader', 'aria2c', # Utiliser aria2c comme downloader alternatif
|
| 184 |
+
'--external-downloader-args', 'aria2c:"--min-split-size=1M --max-connection-per-server=16 --max-concurrent-downloads=16 --split=16"'
|
| 185 |
+
])
|
| 186 |
+
|
| 187 |
+
process = subprocess.run(
|
| 188 |
+
alternative_command,
|
| 189 |
+
capture_output=True,
|
| 190 |
+
text=True
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
if process.returncode != 0:
|
| 194 |
+
logger.error(f"Échec du téléchargement après tentatives alternatives: {process.stderr}")
|
| 195 |
+
raise Exception(f"Échec du téléchargement: {process.stderr}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
downloaded_files = os.listdir(temp_dir)
|
| 198 |
if not downloaded_files:
|
|
|
|
| 201 |
|
| 202 |
video_path = os.path.join(temp_dir, downloaded_files[0])
|
| 203 |
|
| 204 |
+
# Vérifier la taille du fichier
|
| 205 |
+
file_size = os.path.getsize(video_path)
|
| 206 |
+
if file_size == 0:
|
| 207 |
+
raise ValueError("Le fichier téléchargé est vide")
|
| 208 |
+
|
| 209 |
+
logger.info(f"Fichier téléchargé: {video_path} ({file_size} bytes)")
|
| 210 |
+
|
| 211 |
+
# Copier le fichier vers un emplacement temporaire
|
| 212 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(video_path)[1])
|
| 213 |
with open(video_path, 'rb') as f:
|
| 214 |
temp_file.write(f.read())
|