CosmickVisions commited on
Commit
a6b5cd5
·
verified ·
1 Parent(s): cc984ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -1
app.py CHANGED
@@ -35,6 +35,7 @@ from langchain.chains import ConversationalRetrievalChain
35
  from langchain.memory import ConversationBufferMemory
36
  from langchain_community.document_loaders import TextLoader
37
  import re
 
38
 
39
  # Set page config
40
  st.set_page_config(
@@ -1679,9 +1680,38 @@ def main():
1679
  mime="video/mp4"
1680
  )
1681
 
 
 
 
 
 
 
 
 
 
 
1682
  # Display processed video
1683
  st.video(processed_video)
1684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1685
  # Show detailed analysis results
1686
  st.markdown("### Detailed Analysis Results")
1687
 
@@ -2155,4 +2185,84 @@ if __name__ == "__main__":
2155
  st.sidebar.error(f"Error with credentials: {str(e)}")
2156
  client = None
2157
 
2158
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  from langchain.memory import ConversationBufferMemory
36
  from langchain_community.document_loaders import TextLoader
37
  import re
38
+ import base64
39
 
40
  # Set page config
41
  st.set_page_config(
 
1680
  mime="video/mp4"
1681
  )
1682
 
1683
+ # Add clearer instructions
1684
+ st.markdown("""
1685
+ ### Video Playback Options
1686
+ - **Option 1:** Download and play the video locally (recommended)
1687
+ - **Option 2:** Try the embedded player below (may not work in all browsers)
1688
+
1689
+ ⚠️ Note: If the player controls aren't responding, please download the video
1690
+ for the best viewing experience.
1691
+ """)
1692
+
1693
  # Display processed video
1694
  st.video(processed_video)
1695
 
1696
+ # Add custom HTML video player with better compatibility
1697
+ st.markdown("### Processed Video")
1698
+
1699
+ # Create a base64 encoded version of the video for embedding
1700
+ b64_video = base64.b64encode(processed_video).decode()
1701
+
1702
+ # Create custom HTML with video element that has full controls
1703
+ video_html = f"""
1704
+ <video width="100%" controls>
1705
+ <source src="data:video/mp4;base64,{b64_video}" type="video/mp4">
1706
+ Your browser does not support the video tag.
1707
+ </video>
1708
+ """
1709
+ st.markdown(video_html, unsafe_allow_html=True)
1710
+
1711
+ # Keep the regular player as a fallback option
1712
+ st.markdown("### Alternative Player (if HTML player doesn't work)")
1713
+ st.video(processed_video)
1714
+
1715
  # Show detailed analysis results
1716
  st.markdown("### Detailed Analysis Results")
1717
 
 
2185
  st.sidebar.error(f"Error with credentials: {str(e)}")
2186
  client = None
2187
 
2188
+ main()
2189
+
2190
+ # Add this function to your app
2191
+ def extract_video_frames(video_bytes, num_frames=5):
2192
+ """Extract frames from video bytes for thumbnail display"""
2193
+ import cv2
2194
+ import numpy as np
2195
+ import tempfile
2196
+ from PIL import Image
2197
+ import io
2198
+
2199
+ # Save video bytes to a temporary file
2200
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
2201
+ temp_file.write(video_bytes)
2202
+ temp_video_path = temp_file.name
2203
+
2204
+ # Open the video file
2205
+ cap = cv2.VideoCapture(temp_video_path)
2206
+
2207
+ # Get video properties
2208
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
2209
+
2210
+ # Calculate frame intervals
2211
+ interval = max(1, frame_count // (num_frames + 1))
2212
+
2213
+ # Extract frames at intervals
2214
+ frames = []
2215
+ for i in range(1, num_frames + 1):
2216
+ frame_position = i * interval
2217
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_position)
2218
+ ret, frame = cap.read()
2219
+ if ret:
2220
+ # Convert to RGB (from BGR)
2221
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
2222
+ # Convert to PIL Image
2223
+ pil_img = Image.fromarray(frame_rgb)
2224
+ # Save to bytes
2225
+ img_byte_arr = io.BytesIO()
2226
+ pil_img.save(img_byte_arr, format='JPEG')
2227
+ frames.append(img_byte_arr.getvalue())
2228
+
2229
+ # Clean up
2230
+ cap.release()
2231
+ import os
2232
+ os.unlink(temp_video_path)
2233
+
2234
+ return frames
2235
+
2236
+ # Then add this where you display the video
2237
+ if processed_video:
2238
+ # Offer download of processed video
2239
+ st.success("Video processing complete!")
2240
+ st.download_button(
2241
+ label="Download Processed Video",
2242
+ data=processed_video,
2243
+ file_name=f"processed_{uploaded_file.name}",
2244
+ mime="video/mp4"
2245
+ )
2246
+
2247
+ # Extract and display thumbnail frames
2248
+ st.markdown("### Video Preview Frames")
2249
+ st.info("The video may not play in all browsers. Here are preview frames from the processed video.")
2250
+
2251
+ try:
2252
+ frames = extract_video_frames(processed_video, num_frames=4)
2253
+
2254
+ if frames:
2255
+ cols = st.columns(len(frames))
2256
+ for i, frame in enumerate(frames):
2257
+ with cols[i]:
2258
+ st.image(frame, caption=f"Frame {i+1}", use_container_width=True)
2259
+
2260
+ st.markdown("Download the full video for proper playback.")
2261
+ else:
2262
+ st.warning("Could not extract preview frames from the video.")
2263
+ except Exception as e:
2264
+ st.error(f"Error extracting frames: {str(e)}")
2265
+
2266
+ # Regular video display as fallback
2267
+ st.markdown("### Processed Video (Player may not work in all browsers)")
2268
+ st.video(processed_video)