Expertnocode commited on
Commit
4095171
·
verified ·
1 Parent(s): 51cb578

Upload utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils.py +232 -0
utils.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ from pathlib import Path
4
+ import requests
5
+
6
+ def text_to_speech_gtts(text, language="fr"):
7
+ """
8
+ Convertit du texte en audio en utilisant gTTS (Google Text-to-Speech)
9
+
10
+ Args:
11
+ text (str): Le texte à convertir
12
+ language (str): Code de langue (fr, en, es, etc.)
13
+
14
+ Returns:
15
+ str: Chemin vers le fichier audio généré
16
+ """
17
+ try:
18
+ from gtts import gTTS
19
+
20
+ # Créer un fichier temporaire
21
+ output_path = tempfile.mktemp(suffix='.mp3')
22
+
23
+ # Générer l'audio
24
+ tts = gTTS(text=text, lang=language, slow=False)
25
+ tts.save(output_path)
26
+
27
+ return output_path
28
+
29
+ except ImportError:
30
+ raise ImportError("gTTS n'est pas installé. Installez-le avec: pip install gTTS")
31
+ except Exception as e:
32
+ raise Exception(f"Erreur lors de la génération TTS: {str(e)}")
33
+
34
+ def text_to_speech_elevenlabs(text, api_key, voice_id="EXAVITQu4vr4xnSDxMaL"):
35
+ """
36
+ Convertit du texte en audio en utilisant ElevenLabs API
37
+
38
+ Args:
39
+ text (str): Le texte à convertir
40
+ api_key (str): Clé API ElevenLabs
41
+ voice_id (str): ID de la voix à utiliser
42
+
43
+ Returns:
44
+ str: Chemin vers le fichier audio généré
45
+ """
46
+ try:
47
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
48
+
49
+ headers = {
50
+ "Accept": "audio/mpeg",
51
+ "Content-Type": "application/json",
52
+ "xi-api-key": api_key
53
+ }
54
+
55
+ data = {
56
+ "text": text,
57
+ "model_id": "eleven_monolingual_v1",
58
+ "voice_settings": {
59
+ "stability": 0.5,
60
+ "similarity_boost": 0.5
61
+ }
62
+ }
63
+
64
+ response = requests.post(url, json=data, headers=headers)
65
+
66
+ if response.status_code == 200:
67
+ output_path = tempfile.mktemp(suffix='.mp3')
68
+ with open(output_path, 'wb') as f:
69
+ f.write(response.content)
70
+ return output_path
71
+ else:
72
+ raise Exception(f"Erreur API ElevenLabs: {response.status_code} - {response.text}")
73
+
74
+ except Exception as e:
75
+ raise Exception(f"Erreur lors de la génération TTS ElevenLabs: {str(e)}")
76
+
77
+ def merge_videos_side_by_side(video1_path, video2_path, output_path=None):
78
+ """
79
+ Fusionne deux vidéos côte à côte
80
+
81
+ Args:
82
+ video1_path (str): Chemin vers la première vidéo
83
+ video2_path (str): Chemin vers la deuxième vidéo
84
+ output_path (str): Chemin de sortie (optionnel)
85
+
86
+ Returns:
87
+ str: Chemin vers la vidéo fusionnée
88
+ """
89
+ import cv2
90
+
91
+ if output_path is None:
92
+ output_path = tempfile.mktemp(suffix='.mp4')
93
+
94
+ # Ouvrir les vidéos
95
+ cap1 = cv2.VideoCapture(video1_path)
96
+ cap2 = cv2.VideoCapture(video2_path)
97
+
98
+ # Obtenir les propriétés
99
+ fps = int(cap1.get(cv2.CAP_PROP_FPS))
100
+ height = int(cap1.get(cv2.CAP_PROP_FRAME_HEIGHT))
101
+ width1 = int(cap1.get(cv2.CAP_PROP_FRAME_WIDTH))
102
+ width2 = int(cap2.get(cv2.CAP_PROP_FRAME_WIDTH))
103
+
104
+ # Créer le writer pour la vidéo de sortie
105
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
106
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width1 + width2, height))
107
+
108
+ while True:
109
+ ret1, frame1 = cap1.read()
110
+ ret2, frame2 = cap2.read()
111
+
112
+ if not ret1 or not ret2:
113
+ break
114
+
115
+ # Redimensionner si nécessaire pour avoir la même hauteur
116
+ if frame1.shape[0] != frame2.shape[0]:
117
+ frame2 = cv2.resize(frame2, (width2, height))
118
+
119
+ # Fusionner horizontalement
120
+ merged = cv2.hconcat([frame1, frame2])
121
+ out.write(merged)
122
+
123
+ cap1.release()
124
+ cap2.release()
125
+ out.release()
126
+
127
+ return output_path
128
+
129
+ def get_video_duration(video_path):
130
+ """
131
+ Obtient la durée d'une vidéo en secondes
132
+
133
+ Args:
134
+ video_path (str): Chemin vers la vidéo
135
+
136
+ Returns:
137
+ float: Durée en secondes
138
+ """
139
+ import cv2
140
+
141
+ cap = cv2.VideoCapture(video_path)
142
+ fps = cap.get(cv2.CAP_PROP_FPS)
143
+ frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
144
+ duration = frame_count / fps if fps > 0 else 0
145
+ cap.release()
146
+
147
+ return duration
148
+
149
+ def resize_video(input_path, output_path=None, width=None, height=None, scale=None):
150
+ """
151
+ Redimensionne une vidéo
152
+
153
+ Args:
154
+ input_path (str): Chemin vers la vidéo source
155
+ output_path (str): Chemin de sortie (optionnel)
156
+ width (int): Nouvelle largeur (optionnel)
157
+ height (int): Nouvelle hauteur (optionnel)
158
+ scale (float): Facteur d'échelle (optionnel)
159
+
160
+ Returns:
161
+ str: Chemin vers la vidéo redimensionnée
162
+ """
163
+ import cv2
164
+
165
+ if output_path is None:
166
+ output_path = tempfile.mktemp(suffix='.mp4')
167
+
168
+ cap = cv2.VideoCapture(input_path)
169
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
170
+ orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
171
+ orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
172
+
173
+ # Calculer les nouvelles dimensions
174
+ if scale:
175
+ width = int(orig_width * scale)
176
+ height = int(orig_height * scale)
177
+ elif width and not height:
178
+ height = int(orig_height * (width / orig_width))
179
+ elif height and not width:
180
+ width = int(orig_width * (height / orig_height))
181
+ elif not width and not height:
182
+ width, height = orig_width, orig_height
183
+
184
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
185
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
186
+
187
+ while True:
188
+ ret, frame = cap.read()
189
+ if not ret:
190
+ break
191
+
192
+ resized = cv2.resize(frame, (width, height))
193
+ out.write(resized)
194
+
195
+ cap.release()
196
+ out.release()
197
+
198
+ return output_path
199
+
200
+ def create_solid_color_video(width, height, duration, fps, color=(0, 0, 0), output_path=None):
201
+ """
202
+ Crée une vidéo d'une couleur unie
203
+
204
+ Args:
205
+ width (int): Largeur
206
+ height (int): Hauteur
207
+ duration (float): Durée en secondes
208
+ fps (int): Images par seconde
209
+ color (tuple): Couleur BGR (default: noir)
210
+ output_path (str): Chemin de sortie (optionnel)
211
+
212
+ Returns:
213
+ str: Chemin vers la vidéo créée
214
+ """
215
+ import cv2
216
+ import numpy as np
217
+
218
+ if output_path is None:
219
+ output_path = tempfile.mktemp(suffix='.mp4')
220
+
221
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
222
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
223
+
224
+ frame = np.full((height, width, 3), color, dtype=np.uint8)
225
+
226
+ total_frames = int(duration * fps)
227
+ for _ in range(total_frames):
228
+ out.write(frame)
229
+
230
+ out.release()
231
+
232
+ return output_path