BackendJunsen / app /services /processor.py
2nzi's picture
update backend with video upload on HF
923cd30 verified
raw
history blame
2.32 kB
from firebase_admin import firestore
import os
from ..core.config import settings
from ..core.firebase import db
from huggingface_hub import HfApi, create_repo
import tempfile
from .video_processing.hf_upload import HFUploader
from .video_processing.compression import compress_video
async def process_video(video_uuid: str, content: bytes):
temp_files = []
try:
video_ref = db.collection('videos').document(video_uuid)
video_data = video_ref.get().to_dict()
hf_uploader = HFUploader()
sport_id = video_data['sport_id']
# Ensure folder structure exists
hf_uploader.ensure_folder_structure(sport_id)
# Create temp files
temp_raw_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
temp_compressed_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
temp_files.extend([temp_raw_file.name, temp_compressed_file.name])
# Write raw video and close file
temp_raw_file.write(content)
temp_raw_file.close()
# Compress video
compress_video(temp_raw_file.name, temp_compressed_file.name)
temp_compressed_file.close()
# Upload both versions
raw_url = hf_uploader.upload_video(
temp_raw_file.name,
f"{sport_id}/raw/{video_uuid}.mp4"
)
compressed_url = hf_uploader.upload_video(
temp_compressed_file.name,
f"{sport_id}/compressed/{video_uuid}.mp4"
)
# Update Firestore
video_ref.update({
"raw_video_url": raw_url,
"compressed_video_url": compressed_url,
"status": "ready"
})
except Exception as e:
print(f"Erreur lors du traitement de la vidéo {video_uuid}: {str(e)}")
video_ref.update({"status": "error", "error": str(e)})
finally:
# Clean up temp files
for temp_file in temp_files:
try:
if os.path.exists(temp_file):
os.unlink(temp_file)
except Exception as e:
print(f"Erreur lors de la suppression du fichier temporaire {temp_file}: {str(e)}")