# D:\jan-contract\components\video_recorder.py
import os
import streamlit as st
import datetime
import streamlit.components.v1 as components
VIDEO_CONSENT_DIR = "video_consents"
os.makedirs(VIDEO_CONSENT_DIR, exist_ok=True)
def record_consent_video():
"""
Production-grade Video Recorder using RecordRTC.
Features:
- Camera Selection (Fixes 'wrong camera' issues)
- RecordRTC Library (Handles cross-browser compatibility)
- Client-side Encoding (Works on Vercel/Heroku)
"""
st.markdown("### 📹 Record Video Consent")
st.info("Ensure you grant camera permissions when prompted by your browser.")
# We use RecordRTC via CDN for maximum robustness
html_code = """
Ready. Select camera and click Start.
"""
# Height 600 to accommodate camera dropdown
components.html(html_code, height=600)
st.write("---")
st.markdown("### 📤 Upload Your Recording")
st.caption("Once you've saved the video above, upload it here to confirm.")
uploaded_file = st.file_uploader("Drop your recorded video here", type=["webm", "mp4", "mov"])
if uploaded_file is not None:
try:
# Process the uploaded file
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
ext = os.path.splitext(uploaded_file.name)[1] or ".webm"
video_filename = os.path.join(VIDEO_CONSENT_DIR, f"consent_upload_{timestamp}{ext}")
with open(video_filename, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success("✅ Consent Video Received!")
st.video(video_filename)
return video_filename
except Exception as e:
st.error(f"Error saving file: {e}")
return None
return None