Spaces:
Running
Running
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +124 -0
- ffmpeg +3 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
ffmpeg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import logging
|
4 |
+
from typing import Optional
|
5 |
+
from fastapi import FastAPI, HTTPException
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Configuraci贸n de logging
|
9 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
# Directorios y rutas
|
13 |
+
UPLOAD_DIR = os.path.join(os.getcwd(), "uploads")
|
14 |
+
FFMPEG_PATH = "./ffmpeg"
|
15 |
+
|
16 |
+
# Crear directorios
|
17 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
18 |
+
|
19 |
+
def sanitize_filename(filename: str) -> str:
|
20 |
+
"""Limpiar y validar nombre de archivo para prevenir riesgos de seguridad."""
|
21 |
+
return ''.join(c for c in filename if c.isalnum() or c in ('.', '_', '-')).rstrip()
|
22 |
+
|
23 |
+
def ensure_unique_filename(directory: str, filename: str) -> str:
|
24 |
+
"""Generar un nombre de archivo 煤nico para evitar sobreescrituras."""
|
25 |
+
base, ext = os.path.splitext(filename)
|
26 |
+
counter = 1
|
27 |
+
new_filename = filename
|
28 |
+
while os.path.exists(os.path.join(directory, new_filename)):
|
29 |
+
new_filename = f"{base}_{counter}{ext}"
|
30 |
+
counter += 1
|
31 |
+
return new_filename
|
32 |
+
|
33 |
+
def make_ffmpeg_executable(ffmpeg_path: str):
|
34 |
+
"""Asegurar permisos correctos para FFmpeg."""
|
35 |
+
try:
|
36 |
+
subprocess.run(["chmod", "+x", ffmpeg_path], check=True)
|
37 |
+
logger.info(f"Permisos de FFmpeg configurados para {ffmpeg_path}")
|
38 |
+
except subprocess.CalledProcessError as e:
|
39 |
+
logger.error(f"Error al configurar permisos de FFmpeg: {e}")
|
40 |
+
raise
|
41 |
+
|
42 |
+
def convert_video_to_mp4(input_file: str, output_dir: str) -> str:
|
43 |
+
"""Convertir video a formato MP4 con configuraciones optimizadas."""
|
44 |
+
try:
|
45 |
+
# Generar nombre de archivo 煤nico
|
46 |
+
output_filename = ensure_unique_filename(
|
47 |
+
output_dir,
|
48 |
+
os.path.basename(input_file).rsplit('.', 1)[0] + "_converted.mp4"
|
49 |
+
)
|
50 |
+
output_file = os.path.join(output_dir, output_filename)
|
51 |
+
|
52 |
+
# Comando FFmpeg con configuraciones optimizadas
|
53 |
+
ffmpeg_command = [
|
54 |
+
FFMPEG_PATH,
|
55 |
+
'-i', input_file,
|
56 |
+
'-vf', 'fps=24', # Cambiar la tasa de fotogramas
|
57 |
+
'-c:v', 'libx264', # Codificador de video
|
58 |
+
'-c:a', 'libfdk_aac', # Codificador de audio
|
59 |
+
'-profile:a', 'aac_he_v2', # Perfil de audio
|
60 |
+
'-crf', '28', # Tasa de compresi贸n
|
61 |
+
'-b:a', '32k', # Tasa de bits de audio
|
62 |
+
'-preset', 'slow', # Preajuste de codificaci贸n
|
63 |
+
'-movflags', '+faststart', # Web optimization
|
64 |
+
output_file
|
65 |
+
]
|
66 |
+
|
67 |
+
# Ejecutar conversi贸n
|
68 |
+
result = subprocess.run(
|
69 |
+
ffmpeg_command,
|
70 |
+
check=True,
|
71 |
+
capture_output=True,
|
72 |
+
text=True
|
73 |
+
)
|
74 |
+
logger.info(f"Video convertido exitosamente: {output_file}")
|
75 |
+
return output_file
|
76 |
+
|
77 |
+
except subprocess.CalledProcessError as e:
|
78 |
+
logger.error(f"Error de conversi贸n de FFmpeg: {e.stderr}")
|
79 |
+
raise HTTPException(status_code=500, detail=f"Fallo en la conversi贸n de video: {e.stderr}")
|
80 |
+
except Exception as e:
|
81 |
+
logger.error(f"Error inesperado durante la conversi贸n: {e}")
|
82 |
+
raise HTTPException(status_code=500, detail="Error inesperado durante la conversi贸n de video")
|
83 |
+
|
84 |
+
def process_video(file_path: str) -> str:
|
85 |
+
"""Procesar el video completo."""
|
86 |
+
return convert_video_to_mp4(file_path, UPLOAD_DIR)
|
87 |
+
|
88 |
+
def gradio_interface(video: Optional[str]) -> Optional[str]:
|
89 |
+
"""Interfaz de Gradio para conversi贸n de video."""
|
90 |
+
if not video:
|
91 |
+
raise gr.Error("No se ha subido ning煤n video. Por favor, sube un video.")
|
92 |
+
|
93 |
+
try:
|
94 |
+
converted_video = process_video(video)
|
95 |
+
return converted_video
|
96 |
+
except Exception as e:
|
97 |
+
raise gr.Error(f"Conversi贸n fallida: {str(e)}")
|
98 |
+
|
99 |
+
# Asegurar permisos de FFmpeg
|
100 |
+
make_ffmpeg_executable(FFMPEG_PATH)
|
101 |
+
|
102 |
+
# Crear aplicaci贸n FastAPI
|
103 |
+
app = FastAPI()
|
104 |
+
|
105 |
+
# Configurar interfaz de Gradio
|
106 |
+
iface = gr.Interface(
|
107 |
+
fn=gradio_interface,
|
108 |
+
inputs=gr.Video(
|
109 |
+
label="Subir Video",
|
110 |
+
type="filepath", # Asegurar ruta de archivo directa
|
111 |
+
sources=["upload"] # Limitar fuente de subida
|
112 |
+
),
|
113 |
+
outputs=gr.File(
|
114 |
+
label="Descargar MP4 Convertido",
|
115 |
+
type="file"
|
116 |
+
),
|
117 |
+
title="馃帴 Convertidor de Video",
|
118 |
+
description="Convierte videos a formato MP4 optimizado con configuraciones est谩ndar",
|
119 |
+
theme="huggingface" # Tema de HuggingFace
|
120 |
+
)
|
121 |
+
|
122 |
+
# Lanzar interfaz de Gradio
|
123 |
+
if __name__ == "__main__":
|
124 |
+
iface.launch(share=True)
|
ffmpeg
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d6d9c33e7a0ccb8a4774efbbe690697ecf3c8134abc1b34d6072499d478d7c8
|
3 |
+
size 67504768
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.79.0
|
2 |
+
uvicorn==0.18.3
|
3 |
+
requests==2.28.1
|
4 |
+
ffmpeg-python==0.2.0
|
5 |
+
py7zr
|
6 |
+
jdk4py
|