Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

impl ocr and transcription in cache + other stuff

Louis Beaumont12fb832cce91
diff --git a/screenpipe-app-tauri/app/timeline/page.tsx b/screenpipe-app-tauri/app/timeline/page.tsx
index cc49a385d..00d9906d2 100644
--- a/screenpipe-app-tauri/app/timeline/page.tsx
+++ b/screenpipe-app-tauri/app/timeline/page.tsx
@@ -17,6 +17,7 @@ export default function Timeline() {
const [currentIndex, setCurrentIndex] = useState(0);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
+ const [loadedTimeRange, setLoadedTimeRange] = useState<{start: Date, end: Date} | null>(null);
const eventSourceRef = useRef<EventSource | null>(null);
const retryTimeoutRef = useRef<NodeJS.Timeout>();
const retryCount = useRef(0);
@@ -34,6 +35,12 @@ export default function Timeline() {
startTime.setHours(0, 1, 0, 0);
const url = `http://localhost:3030/stream/frames?start_time=${startTime.toISOString()}&end_time=${endTime.toISOString()}&order=descending`;
+ // Set the initial loaded time range
+ setLoadedTimeRange({
+ start: startTime,
+ end: endTime
+ });
+
console.log("starting stream:", url);
const eventSource = new EventSource(url);
@@ -80,6 +87,24 @@ export default function Timeline() {
};
};
+ const getLoadedTimeRangeStyles = () => {
+ if (!loadedTimeRange) return { left: '0%', right: '100%' };
+
+ const startOfDay = new Date();
+ startOfDay.setHours(0, 0, 0, 0);
+ const endOfDay = new Date();
+ endOfDay.setHours(23, 59, 59, 999);
+
+ const totalMs = endOfDay.getTime() - startOfDay.getTime();
+ const startPercent = ((loadedTimeRange.start.getTime() - startOfDay.getTime()) / totalMs) * 100;
+ const endPercent = ((loadedTimeRange.end.getTime() - startOfDay.getTime()) / totalMs) * 100;
+
+ return {
+ left: `${startPercent}%`,
+ right: `${100 - endPercent}%`
+ };
+ };
+
useEffect(() => {
setupEventSource();
@@ -195,6 +220,15 @@ export default function Timeline() {
{/* Timeline bar */}
<div className="w-4/5 mx-auto my-8 relative">
<div className="h-[60px] bg-[#111] border-4 border-[#444] shadow-[0_0_16px_rgba(0,0,0,0.8),inset_0_0_8px_rgba(255,255,255,0.1)] cursor-crosshair relative">
+ {/* Unloaded regions overlay - making it more visible with a different color and opacity */}
+ <div
+ className="absolute inset-0 pointer-events-none"
+ style={{
+ ...getLoadedTimeRangeStyles(),
+ background: 'linear-gradient(to right, rgba(255,255,255,0.8), rgba(255,255,255,0.2))'
+ }}
+ />
+
{/* Grid lines */}
<div
className="absolute inset-0"
@@ -207,7 +241,7 @@ export default function Timeline() {
{/* Current position indicator */}
<div
- className="absolute top-0 h-full w-1 bg-[#0f0] shadow-[0_0_12px_#0f0] opacity-80"
+ className="absolute top-0 h-full w-1 bg-[#0f0] shadow-[0_0_12px_#0f0] opacity-80 z-10"
style={{
left: `${getCurrentTimePercentage()}%`,
}}
diff --git a/screenpipe-server/src/db.rs b/screenpipe-server/src/db.rs
index 1e3115e41..23a8e85d1 100644
--- a/screenpipe-server/src/db.rs
+++ b/screenpipe-server/src/db.rs
@@ -1153,21 +1153,36 @@ impl DatabaseManager {
pub async fn get_frame_metadata(
&self,
file_path: &str,
- ) -> Result<Vec<(String, String)>, SqlxError> {
+ ) -> Result<Vec<(DateTime<Utc>, String, String, String, String)>, SqlxError> {
debug!("getting frame metadata for file: {}", file_path);
- sqlx::query_as::<_, (String, String)>(
+ sqlx::query_as::<_, (DateTime<Utc>, String, String, String, String)>(
r#"
- SELECT ocr_text.app_name, ocr_text.window_name
+ SELECT
+ frames.timestamp,
+ ocr_text.app_name,
+ ocr_text.window_name,
+ ocr_text.text as ocr_text,
+ audio_transcriptions.transcription
FROM video_chunks
JOIN frames ON frames.video_chunk_id = video_chunks.id
JOIN ocr_text ON frames.id = ocr_text.frame_id
+ LEFT JOIN audio_transcriptions ON frames.timestamp = audio_transcriptions.timestamp
WHERE video_chunks.file_path = ?1
- ORDER BY frames.offset_index ASC
+ ORDER BY frames.timestamp ASC
"#,
)
.bind(file_path)
.fetch_all(&self.pool)
.await
+ .map(|rows| {
+ rows.into_iter()
+ .map(
+ |(timestamp, app_name, window_name, ocr_text, transcription)| {
+ (timestamp, app_name, window_name, ocr_text, transcription)
+ },
+ )
+ .collect()
+ })
}
}
diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rs
index dfb2cdf5b..a60b372db 100644
--- a/screenpipe-server/src/server.rs
+++ b/screenpipe-server/src/server.rs
@@ -1181,11 +1181,13 @@ pub struct StreamFramesRequest {
#[derive(Serialize)]
pub struct StreamFramesResponse {
- frame: String, // base64 encoded frame
timestamp: DateTime<Utc>,
+ frame: String,
file_path: String,
- app_name: Option<String>,
- window_name: Option<String>,
+ app_name: String,
+ window_name: String,
+ transcription: String,
+ ocr_text: String,
}
pub fn create_router() -> Router<Arc<AppState>> {
@@ -1266,11 +1268,13 @@ async fn stream_frames_handler(
while let Some(frame_info) = frame_rx.recv().await {
let response = StreamFramesResponse {
- frame: BASE64_STANDARD.encode(&frame_info.data),
timestamp: frame_info.timestamp,
+ frame: BASE64_STANDARD.encode(&frame_info.data),
file_path: String::new(),
- app_name: Some(frame_info.app_name),
- window_name: Some(frame_info.window_name),
+ app_name: frame_info.metadata.app_name,
+ window_name: frame_info.metadata.window_name,
+ transcription: frame_info.metadata.transcription,
+ ocr_text: frame_info.metadata.ocr_text,
};
if let Ok(json) = serde_json::to_string(&response) {
diff --git a/screenpipe-server/src/video_cache.rs b/screenpipe-server/src/video_cache.rs
index 907fe21b9..c810cf746 100644
--- a/screenpipe-server/src/video_cache.rs
+++ b/screenpipe-server/src/video_cache.rs
@@ -35,7 +35,7 @@ use std::time::SystemTime;
use tokio::io::AsyncReadExt;
use tokio::process::Command;
use tokio::sync::mpsc::{channel, Sender};
-use tokio::sync::{oneshot, RwLock};
+use tokio::sync::RwLock;
use tracing::{debug, error};
use glob::glob;
@@ -46,15 +46,21 @@ use tokio::io::BufReader;
use crate::DatabaseManager;
-type FrameChannel = Sender<(DateTime<Utc>, Vec<u8>, String, String, String)>;
+type FrameChannel = Sender<(DateTime<Utc>, Vec<u8>, FrameMetadata)>;
+
+#[derive(Debug, Clone)]
+pub struct FrameMetadata {
+ pub app_name: String,
+ pub window_name: String,
+ pub transcription: String,
+ pub ocr_text: String,
+}
#[derive(Debug, Clone)]
struct PreviewFrame {
timestamp: DateTime<Utc>,
data: Arc<Vec<u8>>,
- file_path: String,
- app_name: String,
- window_name: String,
+ metadata: FrameMetadata,
is_preview: bool,
}
@@ -65,6 +71,7 @@ struct VideoFile {
end_time: DateTime<Utc>,
size: u64,
is_complete: bool,
+ metadata: BTreeMap<DateTime<Utc>, FrameMetadata>,
}
struct VideoIndex {
@@ -116,6 +123,7 @@ impl VideoIndex {
end_time,
size: metadata.len(),
is_complete,
+ metadata: BTreeMap::new(),
},
);
@@ -134,45 +142,6 @@ impl VideoIndex {
}
}
-enum CacheMsg {
- GetFrame(i64, oneshot::Sender<Option<PreviewFrame>>),
- InsertFrame(i64, PreviewFrame),
-}
-
-struct FrameCacheManager {
- recent_frames: LruCache<i64, PreviewFrame>,
- preview_frames: LruCache<i64, PreviewFrame>,
-}
-
-impl FrameCacheManager {
- fn new() -> Self {
- Self {
- recent_frames: LruCache::new(NonZeroUsize::new(300).unwrap()),
- preview_frames: LruCache::new(NonZeroUsize::new(720).unwrap()),
- }
- }
-
- fn handle_message(&mut self, msg: CacheMsg) {
- match msg {
- CacheMsg::GetFrame(timestamp, resp) => {
- let value = self
- .recent_frames
- .get(&timestamp)
- .or_else(|| self.preview_frames.get(&timestamp))
- .cloned();
- let _ = resp.send(value);
- }
- CacheMsg::InsertFrame(timestamp, frame) => {
- if frame.is_preview {
- self.preview_frames.put(timestamp, frame);
- } else {
- self.recent_frames.put(timestamp, frame);
- }
- }
- }
- }
-}
-
#[derive(Clone)]
pub struct FrameCacheConfig {
pub prefetch_size: Duration,
@@ -195,14 +164,14 @@ struct VideoChunk {
file_path: String,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
+ metadata: BTreeMap<DateTime<Utc>, FrameMetadata>,
}
#[derive(Debug, Clone)]
pub struct FrameInfo {
pub timestamp: DateTime<Utc>,
pub data: Vec<u8>,
- pub app_name: String,
- pub window_name: String,
+ pub metadata: FrameMetadata,
}
#[derive(Clone)]
@@ -210,7 +179,8 @@ pub struct FrameCache {
pub screenpipe_dir: PathBuf,
config: FrameCacheConfig,
video_index: Arc<RwLock<VideoIndex>>,
- cache_manager_tx: Sender<CacheMsg>,
+ recent_frames: Arc<RwLock<LruCache<i64, PreviewFrame>>>,
+ preview_frames: Arc<RwLock<LruCache<i64, PreviewFrame>>>,
db: Arc<DatabaseManager>,
}
@@ -227,7 +197,8 @@ impl FrameCache {
debug!("initializing frame cache");
let video_index = Arc::new(RwLock::new(VideoIndex::new()));
- let (cache_manager_tx, mut cache_manager_rx) = channel::<CacheMsg>(100);
+ let recent_frames = Arc::new(RwLock::new(LruCache::new(NonZeroUsize::new(300).unwrap())));
+ let preview_frames = Arc::new(RwLock::new(LruCache::new(NonZeroUsize::new(720).unwrap())));
// Initial index build
{
@@ -235,16 +206,15 @@ impl FrameCache {
index.refresh_index(&screenpipe_dir).await?;
}
- // Spawn index refresh and preload task
- let refresh_index = video_index.clone();
- let refresh_dir = screenpipe_dir.clone();
let cache = Self {
screenpipe_dir: screenpipe_dir.clone(),
config: config.clone(),
video_index: video_index.clone(),
- cache_manager_tx: cache_manager_tx.clone(),
+ recent_frames: recent_frames.clone(),
+ preview_frames: preview_frames.clone(),
db: db.clone(),
};
+
let cache_clone = cache.clone();
tokio::spawn(async move {
@@ -254,8 +224,8 @@ impl FrameCache {
// Refresh index
{
- let mut index = refresh_index.write().await;
- if let Err(e) = index.refresh_index(&refresh_dir).await {
+ let mut index = cache_clone.video_index.write().await;
+ if let Err(e) = index.refresh_index(&screenpipe_dir).await {
error!("failed to refresh index: {}", e);
}
}
@@ -264,28 +234,21 @@ impl FrameCache {
// Preload high-res recent frames (last 5 minutes)
let hi_res_start = now - Duration::minutes(5);
- if let Err(e) = cache.preload_frames(&hi_res_start, &now, false).await {
+ if let Err(e) = cache_clone.preload_frames(&hi_res_start, &now, false).await {
error!("failed to preload high-res frames: {}", e);
}
// Preload low-res frames (last hour)
let low_res_start = now - Duration::minutes(60);
- if let Err(e) = cache.preload_frames(&low_res_start, &now, true).await {
+ if let Err(e) = cache_clone.preload_frames(&low_res_start, &now, true).await {
error!("failed to preload low-res frames: {}", e);
}
- debug!("index refresh and frame preload complete (both hi-res and low-res)");
+ debug!("index refresh and frame preload complete");
}
});
- tokio::spawn(async move {
- let mut manager = FrameCacheManager::new();
- while let Some(msg) = cache_manager_rx.recv().await {
- manager.handle_message(msg);
- }
- });
-
- Ok(cache_clone)
+ Ok(cache)
}
async fn extract_frames_batch(
@@ -378,18 +341,23 @@ impl FrameCache {
while reader.read_exact(&mut buf).await.is_ok() {
frame_data.push(buf[0]);
if last_byte == 0xFF && buf[0] == 0xD9 {
+ let frame_metadata = FrameMetadata {
+ app_name: metadata
+ .get(frame_count)
+ .map_or("".to_string(), |(_, app, _, _, _)| app.clone()),
+ window_name: metadata
+ .get(frame_count)
+ .map_or("".to_string(), |(_, _, win, _, _)| win.clone()),
+ transcription: metadata
+ .get(frame_count)
+ .map_or("".to_string(), |(_, _, _, trans, _)| trans.clone()),
+ ocr_text: metadata
+ .get(frame_count)
+ .map_or("".to_string(), |(_, _, _, _, ocr)| ocr.clone()),
+ };
+
if let Err(e) = frame_tx
- .send((
- frame_time,
- frame_data,
- file_path.to_string(),
- metadata
- .get(frame_count)
- .map_or("".to_string(), |(app, _)| app.clone()),
- metadata
- .get(frame_count)
- .map_or("".to_string(), |(_, win)| win.clone()),
- ))
+ .send((frame_time, frame_data, frame_metadata))
.await
{
error_count += 1;
@@ -412,15 +380,11 @@ impl FrameCache {
Ok(())
};
- // Add 30 second timeout
match tokio::time::timeout(std::time::Duration::from_secs(30), frame_extraction).await {
Ok(result) => result,
Err(_) => {
- // Force kill the ffmpeg process
let _ = child.kill().await;
- Err(anyhow::anyhow!(
- "frame extraction timed out after 30 seconds"
- ))
+ Err(anyhow::anyhow!("frame extraction timed out"))
}
}?;
@@ -431,15 +395,13 @@ impl FrameCache {
Ok(())
}
+
async fn is_video_file_complete(&self, file_path: &str) -> bool {
- // Add file age check first
if let Ok(metadata) = tokio::fs::metadata(file_path).await {
if let Ok(modified) = metadata.modified() {
let age = SystemTime::now()
.duration_since(modified)
.unwrap_or_default();
-
- // Skip validation for files modified in last 60s
if age.as_secs() < 60 {
return false;
}
@@ -454,7 +416,6 @@ impl FrameCache {
}
};
- // Just do a quick check if ffmpeg can read the file
match Command::new(&ffmpeg_path)
.args(&["-v", "error", "-i", file_path, "-f", "null", "-"])
.output()
@@ -477,44 +438,7 @@ impl FrameCache {
}
}
}
- pub async fn extract_frames_range(
- &self,
- start: &DateTime<Utc>,
- end: &DateTime<Utc>,
- frame_tx: FrameChannel,
- descending: bool,
- ) -> Result<()> {
- let mut chunks = self.find_video_chunks(*start, *end).await?;
- // Sort chunks based on the descending flag
- if descending {
- chunks.sort_by(|a, b| b.start_time.cmp(&a.start_time));
- } else {
- chunks.sort_by(|a, b| a.start_time.cmp(&b.start_time));
- }
-
- for chunk in chunks {
- if !self.is_video_file_complete(&chunk.file_path).await {
- continue;
- }
-
- if let Err(e) = self
- .extract_frames_batch(
- &chunk.file_path,
- chunk.start_time,
- chunk.end_time,
- self.config.fps,
- frame_tx.clone(),
- false,
- )
- .await
- {
- error!("failed to extract frames from {}: {}", chunk.file_path, e);
- }
- }
-
- Ok(())
- }
async fn find_video_chunks(
&self,
start: DateTime<Utc>,
@@ -531,6 +455,7 @@ impl FrameCache {
file_path: file.path.to_string_lossy().to_string(),
start_time: file.start_time,
end_time: file.end_time,
+ metadata: file.metadata.clone(),
})
.collect();
@@ -580,22 +505,21 @@ impl FrameCache {
}
}
- while let Some((timestamp, frame_data, file_path, app_name, window_name)) =
- frame_rx.recv().await
- {
+ while let Some((timestamp, frame_data, metadata)) = frame_rx.recv().await {
let frame = PreviewFrame {
timestamp,
data: Arc::new(frame_data),
- file_path,
- app_name,
- window_name,
- is_preview,
+ metadata,
+ is_preview: false,
};
- let _ = self
- .cache_manager_tx
- .send(CacheMsg::InsertFrame(timestamp.timestamp(), frame))
- .await;
+ let cache = if is_preview {
+ &self.preview_frames
+ } else {
+ &self.recent_frames
+ };
+
+ cache.write().await.put(timestamp.timestamp(), frame);
}
Ok(())
@@ -620,14 +544,12 @@ impl FrameCache {
.await
});
- // Forward frames to the provided channel
- while let Some((timestamp, data, _, app_name, window_name)) = extract_rx.recv().await {
+ while let Some((timestamp, data, metadata)) = extract_rx.recv().await {
frame_tx
.send(FrameInfo {
timestamp,
data,
- app_name,
- window_name,
+ metadata,
})
.await?;
}
@@ -635,4 +557,42 @@ impl FrameCache {
extract_handle.await??;
Ok(())
}
+
+ pub async fn extract_frames_range(
+ &self,
+ start: &DateTime<Utc>,
+ end: &DateTime<Utc>,
+ frame_tx: FrameChannel,
+ descending: bool,
+ ) -> Result<()> {
+ let mut chunks = self.find_video_chunks(*start, *end).await?;
+
+ if descending {
+ chunks.sort_by(|a, b| b.start_time.cmp(&a.start_time));
+ } else {
+ chunks.sort_by(|a, b| a.start_time.cmp(&b.start_time));
+ }
+
+ for chunk in chunks {
+ if !self.is_video_file_complete(&chunk.file_path).await {
+ continue;
+ }
+
+ if let Err(e) = self
+ .extract_frames_batch(
+ &chunk.file_path,
+ chunk.start_time,
+ chunk.end_time,
+ self.config.fps,
+ frame_tx.clone(),
+ false,
+ )
+ .await
+ {
+ error!("failed to extract frames from {}: {}", chunk.file_path, e);
+ }
+ }
+
+ Ok(())
+ }
}