Spaces:
Sleeping
Sleeping
File size: 2,324 Bytes
923cd30 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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)}") |