Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ import os
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
import PIL.Image
|
|
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
@@ -91,33 +93,55 @@ def is_youtube_url(url):
|
|
91 |
parsed = urlparse(url)
|
92 |
return any(domain in parsed.netloc for domain in ['youtube.com', 'youtu.be'])
|
93 |
|
|
|
|
|
|
|
94 |
def download_youtube_video(url):
|
95 |
-
"""Télécharge une vidéo YouTube
|
96 |
try:
|
97 |
with tempfile.TemporaryDirectory() as temp_dir:
|
98 |
-
|
99 |
-
'format': 'best[filesize<50M]', # Limite la taille du fichier
|
100 |
-
'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
|
101 |
-
'quiet': True,
|
102 |
-
'no_warnings': True,
|
103 |
-
'extract_flat': False
|
104 |
-
}
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
except Exception as e:
|
123 |
print(f"Erreur lors du téléchargement de la vidéo: {e}")
|
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
import PIL.Image
|
7 |
+
import subprocess
|
8 |
+
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
93 |
parsed = urlparse(url)
|
94 |
return any(domain in parsed.netloc for domain in ['youtube.com', 'youtu.be'])
|
95 |
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
def download_youtube_video(url):
|
100 |
+
"""Télécharge une vidéo YouTube en utilisant subprocess avec youtube-dl"""
|
101 |
try:
|
102 |
with tempfile.TemporaryDirectory() as temp_dir:
|
103 |
+
output_template = os.path.join(temp_dir, '%(title)s.%(ext)s')
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
# Commande youtube-dl avec options
|
106 |
+
command = [
|
107 |
+
'youtube-dl',
|
108 |
+
'--format', 'best[filesize<50M]', # Limite la taille du fichier
|
109 |
+
'--quiet',
|
110 |
+
'--no-warnings',
|
111 |
+
'--output', output_template,
|
112 |
+
url
|
113 |
+
]
|
114 |
+
|
115 |
+
print(f"Téléchargement de la vidéo: {url}")
|
116 |
+
|
117 |
+
# Exécution de la commande
|
118 |
+
process = subprocess.Popen(
|
119 |
+
command,
|
120 |
+
stdout=subprocess.PIPE,
|
121 |
+
stderr=subprocess.PIPE,
|
122 |
+
universal_newlines=True
|
123 |
+
)
|
124 |
+
|
125 |
+
# Attente et récupération du résultat
|
126 |
+
stdout, stderr = process.communicate()
|
127 |
+
|
128 |
+
if process.returncode != 0:
|
129 |
+
raise Exception(f"Erreur youtube-dl: {stderr}")
|
130 |
+
|
131 |
+
# Recherche du fichier téléchargé dans le dossier temporaire
|
132 |
+
downloaded_files = os.listdir(temp_dir)
|
133 |
+
if not downloaded_files:
|
134 |
+
raise FileNotFoundError("Aucun fichier n'a été téléchargé")
|
135 |
+
|
136 |
+
video_path = os.path.join(temp_dir, downloaded_files[0])
|
137 |
+
|
138 |
+
# Copie vers un fichier temporaire permanent
|
139 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(video_path)[1])
|
140 |
+
with open(video_path, 'rb') as f:
|
141 |
+
temp_file.write(f.read())
|
142 |
+
|
143 |
+
print(f"Vidéo téléchargée avec succès: {temp_file.name}")
|
144 |
+
return temp_file.name
|
145 |
|
146 |
except Exception as e:
|
147 |
print(f"Erreur lors du téléchargement de la vidéo: {e}")
|