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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
refactor: merge videos endpoint
Louis Beaumont479604c2d161
diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rsindex cc2afbcb3..40c9c8077 100644--- a/screenpipe-server/src/server.rs+++ b/screenpipe-server/src/server.rs@@ -13,7 +13,7 @@ use screenpipe_vision::monitor::list_monitors;use crate::{db::TagContentType,pipe_manager::{PipeInfo, PipeManager},- video_utils::{merge_frames_from_video, MergeFramesRequest, MergeFramesResponse},+ video_utils::{merge_videos, MergeVideosRequest, MergeVideosResponse},ContentType, DatabaseManager, SearchResult,};use crate::{plugin::ApiPluginLayer, video_utils::extract_frame};@@ -754,11 +754,11 @@ impl Server {async fn merge_frames_handler(State(state): State<Arc<AppState>>,- JsonResponse(payload): JsonResponse<MergeFramesRequest>,-) -> Result<JsonResponse<MergeFramesResponse>, (StatusCode, JsonResponse<Value>)> {+ JsonResponse(payload): JsonResponse<MergeVideosRequest>,+) -> Result<JsonResponse<MergeVideosResponse>, (StatusCode, JsonResponse<Value>)> {let output_dir = state.screenpipe_dir.join("videos");- match merge_frames_from_video(payload, output_dir, &state.db).await {+ match merge_videos(payload, output_dir).await {Ok(response) => Ok(JsonResponse(response)),Err(e) => {error!("Failed to merge frames: {}", e);@@ -932,32 +932,27 @@ curl -X POST "http://localhost:3030/pipes/update" \# Perform the search and store the response-# get frames from 20 min ago to 30 min ago-SEARCH_RESPONSE=$(curl -s "http://localhost:3030/search?q=&limit=5&offset=0&content_type=ocr&start_time=$(date -u -v-60M +%Y-%m-%dT%H:%M:%SZ)&end_time=$(date -u -v-55M +%Y-%m-%dT%H:%M:%SZ)")-# Extract the file_path and frame_id from the first OCR result-FILE_PATH=$(echo "$SEARCH_RESPONSE" | jq -r '.data[0].content.file_path')-OFFSET_INDEX=$(echo "$SEARCH_RESPONSE" | jq -r '.data[0].content.offset_index')+# First, let's search for some recent video content+SEARCH_RESPONSE1=$(curl -s "http://localhost:3030/search?q=&limit=5&offset=0&content_type=ocr&start_time=$(date -u -v-30M +%Y-%m-%dT%H:%M:%SZ)&end_time=$(date -u -v-25M +%Y-%m-%dT%H:%M:%SZ)")+SEARCH_RESPONSE2=$(curl -s "http://localhost:3030/search?q=&limit=5&offset=0&content_type=ocr&start_time=$(date -u -v-40M +%Y-%m-%dT%H:%M:%SZ)&end_time=$(date -u -v-35M +%Y-%m-%dT%H:%M:%SZ)")+SEARCH_RESPONSE3=$(curl -s "http://localhost:3030/search?q=&limit=5&offset=0&content_type=ocr&start_time=$(date -u -v-50M +%Y-%m-%dT%H:%M:%SZ)&end_time=$(date -u -v-45M +%Y-%m-%dT%H:%M:%SZ)")-echo "File Path: $FILE_PATH"-echo "Offset Index: $OFFSET_INDEX"+# Extract the file paths from the search results without creating JSON arrays+VIDEO_PATHS1=$(echo "$SEARCH_RESPONSE1" | jq -r '.data[].content.file_path' | sort -u)+VIDEO_PATHS2=$(echo "$SEARCH_RESPONSE2" | jq -r '.data[].content.file_path' | sort -u)+VIDEO_PATHS3=$(echo "$SEARCH_RESPONSE3" | jq -r '.data[].content.file_path' | sort -u)-# Define the number of surrounding frames and output format-SURROUNDING_FRAMES=300-OUTPUT_FORMAT="mp4"+# Merge the video paths and create a single JSON array+MERGED_VIDEO_PATHS=$(echo "$VIDEO_PATHS1"$'\n'"$VIDEO_PATHS2"$'\n'"$VIDEO_PATHS3" | sort -u | jq -R -s -c 'split("\n") | map(select(length > 0))')-# Create the JSON payload for merging frames+# Create the JSON payload for merging videosMERGE_PAYLOAD=$(jq -n \- --arg video_path "$FILE_PATH" \- --argjson frame_indexes "[$OFFSET_INDEX]" \- --argjson surrounding_frames "$SURROUNDING_FRAMES" \- --arg output_format "$OUTPUT_FORMAT" \+ --argjson video_paths "$MERGED_VIDEO_PATHS" \'{- video_path: $video_path,- frame_indexes: $frame_indexes,- surrounding_frames: $surrounding_frames,- output_format: $output_format+ video_paths: $video_paths}')+echo "Merge Payload: $MERGE_PAYLOAD"# Send the merge request and store the responseMERGE_RESPONSE=$(curl -s -X POST "http://localhost:3030/experimental/frames/merge" \@@ -965,4 +960,10 @@ MERGE_RESPONSE=$(curl -s -X POST "http://localhost:3030/experimental/frames/merg-d "$MERGE_PAYLOAD")echo "Merge Response: $MERGE_RESPONSE"++# Extract the merged video path from the response+MERGED_VIDEO_PATH=$(echo "$MERGE_RESPONSE" | jq -r '.video_path')++echo "Merged Video Path: $MERGED_VIDEO_PATH"+*/diff --git a/screenpipe-server/src/video_utils.rs b/screenpipe-server/src/video_utils.rsindex 86d001fcc..268de2247 100644--- a/screenpipe-server/src/video_utils.rs+++ b/screenpipe-server/src/video_utils.rs@@ -2,25 +2,19 @@ use anyhow::Result;use base64::{engine::general_purpose, Engine as _};use screenpipe_core::find_ffmpeg_path;use serde::{Deserialize, Serialize};-use std::collections::BTreeSet;-use std::fs::File;-use std::fs::Permissions;-use std::io::Write;-use std::os::unix::fs::PermissionsExt;-use std::path::{Path, PathBuf};-use tempfile::NamedTempFile;+use std::path::PathBuf;use tokio::io::AsyncReadExt;use tokio::process::Command;-use tracing::{info, warn};+use tracing::{debug, info};use uuid::Uuid;pub async fn extract_frame(file_path: &str, offset_index: i64) -> Result<String> {- let ffmpeg_path = find_ffmpeg_path().expect("Failed to find FFmpeg path");+ let ffmpeg_path = find_ffmpeg_path().expect("failed to find ffmpeg path");let offset_seconds = offset_index as f64 / 1000.0;let offset_str = format!("{:.3}", offset_seconds);- info!(+ debug!("extracting frame from {} at offset {}",file_path, offset_str);@@ -43,11 +37,11 @@ pub async fn extract_frame(file_path: &str, offset_index: i64) -> Result<String>.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::piped());- info!("ffmpeg command: {:?}", command);+ debug!("ffmpeg command: {:?}", command);let mut child = command.spawn()?;- let mut stdout = child.stdout.take().expect("Failed to open stdout");- let mut stderr = child.stderr.take().expect("Failed to open stderr");+ let mut stdout = child.stdout.take().expect("failed to open stdout");+ let mut stderr = child.stderr.take().expect("failed to open stderr");let mut frame_data = Vec::new();stdout.read_to_end(&mut frame_data).await?;@@ -56,355 +50,96 @@ pub async fn extract_frame(file_path: &str, offset_index: i64) -> Result<String>if !status.success() {let mut error_message = String::new();stderr.read_to_string(&mut error_message).await?;- info!("FFmpeg error: {}", error_message);- return Err(anyhow::anyhow!("FFmpeg process failed: {}", error_message));+ info!("ffmpeg error: {}", error_message);+ return Err(anyhow::anyhow!("ffmpeg process failed: {}", error_message));}if frame_data.is_empty() {- return Err(anyhow::anyhow!("Failed to extract frame: no data received"));+ return Err(anyhow::anyhow!("failed to extract frame: no data received"));}Ok(general_purpose::STANDARD.encode(frame_data))}#[derive(Deserialize)]-pub struct MergeFramesRequest {- pub video_path: String, // path to the starting video- pub frame_indexes: Vec<i64>, // target frame indices within the starting video- pub surrounding_frames: i64, // number of frames before and after each target frame+pub struct MergeVideosRequest {+ pub video_paths: Vec<String>,}#[derive(Serialize)]-pub struct MergeFramesResponse {+pub struct MergeVideosResponse {video_path: String,}-use crate::db::DatabaseManager;--pub async fn merge_frames_from_video(- request: MergeFramesRequest,+pub async fn merge_videos(+ request: MergeVideosRequest,output_dir: PathBuf,- db_manager: &DatabaseManager,-) -> Result<MergeFramesResponse> {- info!(- "merging frames from video: {:?}, indexes: {:?}, surrounding: {}",- request.video_path, request.frame_indexes, request.surrounding_frames- );-- let mut current_video = PathBuf::from(&request.video_path);-- // if the path is relative, resolve it against the output_dir- if !current_video.is_absolute() {- current_video = output_dir.join(current_video);- }-- let temp_dir = tempfile::Builder::new()- .prefix("screenpipe_")- .tempdir()?;- let temp_path = temp_dir.path().to_path_buf();- info!("Using temporary directory: {:?}", temp_path);-- // Ensure the temporary directory is writable- std::fs::set_permissions(&temp_path, Permissions::from_mode(0o755))?;-- let mut selected_frames = Vec::new();- let mut frame_indexes = request.frame_indexes.clone();- let mut remaining_frames = request.surrounding_frames * 2 + 1; // Total frames to collect-- let mut total_extracted_frames = 0;- let mut total_failed_extractions = 0;-- loop {- // check if the video file exists and is accessible- if !current_video.exists() || !is_file_accessible(¤t_video) {- warn!(- "Skipping inaccessible video (possibly being written): {:?}",- current_video- );- break; // stop processing and use frames collected so far- }-- // fetch total frames from the current video- let total_frames = db_manager.get_total_frames(¤t_video).await?;- info!(- "total frames in video {}: {}",- current_video.display(),- total_frames- );-- // calculate frame ranges with total_frames consideration- let frame_ranges = calculate_frame_ranges(&frame_indexes, remaining_frames, total_frames);- let extracted = extract_frame_ranges(¤t_video, &frame_ranges, &temp_path).await?;- info!(- "Extracted {} frames from {:?}",- extracted.len(),- current_video- );- total_extracted_frames += extracted.len();- selected_frames.extend(extracted.clone());-- // Count failed extractions- total_failed_extractions += frame_ranges.iter().map(|(start, end)| end - start + 1).sum::<i64>() as usize - extracted.len();-- // Update remaining frames- remaining_frames -= extracted.len() as i64;-- // Check if we've collected enough frames- if remaining_frames <= 0 {- break;- }-- // determine if we need to fetch the next video- if let Some(next_video) = db_manager- .get_next_video(¤t_video.to_string_lossy())- .await?- {- current_video = PathBuf::from(next_video);- if !current_video.is_absolute() {- current_video = output_dir.join(current_video);- }- frame_indexes = vec![0]; // reset frame index for the next video- } else {- break; // no more videos to fetch- }- }+) -> Result<MergeVideosResponse> {+ info!("merging videos: {:?}", request.video_paths);- info!("Total extracted frames: {}", total_extracted_frames);- info!("Total failed extractions: {}", total_failed_extractions);- info!("Total selected frames: {}", selected_frames.len());-- if selected_frames.is_empty() {- return Err(anyhow::anyhow!("No frames could be extracted"));- }-- // generate output video filenamelet output_filename = format!("output_{}.mp4", Uuid::new_v4());let output_path = output_dir.join(&output_filename);- info!("merging frames into video: {:?}", output_path);-- // merge extracted frames into a video- merge_frames_into_video(&selected_frames, &output_path, &temp_path).await?;-- Ok(MergeFramesResponse {- video_path: output_path.to_string_lossy().into_owned(),- })-}--// Helper function to check if a file is accessible (not being written)-fn is_file_accessible(path: &Path) -> bool {- std::fs::File::open(path).is_ok()-}--fn calculate_frame_ranges(- frame_indexes: &[i64],- remaining_frames: i64,- total_frames: i64,-) -> Vec<(i64, i64)> {- let mut ranges = BTreeSet::new();- for &index in frame_indexes {- let half_range = remaining_frames / 2;- let start = std::cmp::max(0, index - half_range);- let end = std::cmp::min(index + half_range, total_frames - 1);- ranges.insert((start, end));- }-- info!("frame ranges: {:?}", ranges);-- // Merge overlapping ranges- let mut merged_ranges = Vec::new();- let mut current_range = None;-- for range in ranges {- if let Some((start, end)) = current_range {- if range.0 <= end + 1 {- current_range = Some((start, std::cmp::max(end, range.1)));- } else {- merged_ranges.push((start, end));- current_range = Some(range);- }- } else {- current_range = Some(range);- }- }-- if let Some(range) = current_range {- merged_ranges.push(range);- }-- info!("merged frame ranges: {:?}", merged_ranges);-- merged_ranges-}--async fn extract_frame_ranges(- video_path: &Path,- frame_ranges: &[(i64, i64)],- temp_dir: &Path,-) -> Result<Vec<PathBuf>> {- let mut extracted = Vec::new();-- for &(start, end) in frame_ranges {- for frame_idx in start..=end {- let unique_id = Uuid::new_v4();- let frame_path = temp_dir.join(format!("frame_{}_{}.png", frame_idx, unique_id));- match extract_frame_by_index(video_path, frame_idx, &frame_path).await {- Ok(_) => {- if frame_path.exists() {- extracted.push(frame_path);- } else {- warn!("Frame not found after extraction: {:?}", frame_path);- }- }- Err(e) => {- warn!("Failed to extract frame {}: {}", frame_idx, e);- }- }- }- }-- Ok(extracted)-}--use std::fs;-use tokio::time::Duration;--async fn extract_frame_by_index(- video_path: &Path,- frame_index: i64,- output_path: &Path,-) -> Result<()> {- info!(- "extracting frame {} from {:?} to {:?}",- frame_index, video_path, output_path- );- let ffmpeg_path = find_ffmpeg_path().expect("Failed to find FFmpeg path");-- // Ensure the parent directory exists and is writable- if let Some(parent) = output_path.parent() {- tokio::fs::create_dir_all(parent).await?;- tokio::fs::set_permissions(parent, Permissions::from_mode(0o755)).await?;- }-- let output = tokio::process::Command::new(&ffmpeg_path)- .args(&[- "-v", "verbose", // Add verbose logging- "-i",- video_path.to_str().unwrap(),- "-vf",- &format!("select='eq(n,{})',showinfo", frame_index), // Add showinfo filter- "-vframes",- "1",- "-y",- output_path.to_str().unwrap(),- ])- .output()+ // create a temporary file to store the list of input videos+ let temp_file = output_dir.join("input_list.txt");+ let mut file = tokio::fs::File::create(&temp_file).await?;+ for video_path in &request.video_paths {+ // Escape single quotes in the file path+ let escaped_path = video_path.replace("'", "'\\''");+ tokio::io::AsyncWriteExt::write_all(+ &mut file,+ format!("file '{}'\n", escaped_path).as_bytes(),+ ).await?;-- // Log stdout and stderr regardless of success- let stdout = String::from_utf8_lossy(&output.stdout);- let stderr = String::from_utf8_lossy(&output.stderr);- info!("ffmpeg stdout: {}", stdout);- info!("ffmpeg stderr: {}", stderr);-- if output.status.success() {- // Check for file existence multiple times with increasing delays- for i in 0..5 {- tokio::time::sleep(Duration::from_millis(100 * (i + 1))).await;- if output_path.exists() {- let metadata = fs::metadata(output_path)?;- info!(- "frame extracted successfully: {:?}, size: {} bytes",- output_path,- metadata.len()- );- return Ok(());- }- }-- let error_msg = format!(- "ffmpeg reported success, but output file not found after multiple checks: {:?}. Parent dir exists: {}, is writable: {}",- output_path,- output_path.parent().map_or(false, |p| p.exists()),- output_path.parent().map_or(false, |p| !p.metadata().map(|m| m.permissions().readonly()).unwrap_or(true))- );- info!("{}", error_msg);- Err(anyhow::anyhow!(error_msg))- } else {- let error_msg = format!(- "ffmpeg failed to extract frame {} from {:?}. error: {}",- frame_index,- video_path,- stderr- );- info!("{}", error_msg);- Err(anyhow::anyhow!(error_msg))- }-}--async fn merge_frames_into_video(- frames: &[PathBuf],- output_path: &Path,- temp_dir: &Path,-) -> Result<()> {- let frame_list_file = NamedTempFile::new_in(temp_dir)?;- let frame_list_path = frame_list_file.path().to_path_buf();-- {- let mut file = File::create(&frame_list_path)?;- for frame in frames {- if frame.exists() {- writeln!(file, "file '{}'", frame.to_str().unwrap())?;- } else {- warn!("Frame file not found: {:?}", frame);- }- }}- // Log frame list content- let frame_list_content = std::fs::read_to_string(&frame_list_path)?;- info!("Frame list file content:\n{}", frame_list_content);-- let status = tokio::process::Command::new("ffmpeg")+ let ffmpeg_path = find_ffmpeg_path().expect("failed to find ffmpeg path");+ let status = Command::new(ffmpeg_path).args(&["-f","concat","-safe","0","-i",- frame_list_path.to_str().unwrap(),- "-c:v",- "libx264",- "-pix_fmt",- "yuv420p",- "-r",- "1",- "-y", // Overwrite output file if it exists+ temp_file.to_str().unwrap(),+ "-c",+ "copy",+ "-y",output_path.to_str().unwrap(),]).output().await?;- // Log ffmpeg's output+ // clean up the temporary file+ tokio::fs::remove_file(temp_file).await?;++ // log ffmpeg's outputlet stdout = String::from_utf8_lossy(&status.stdout);let stderr = String::from_utf8_lossy(&status.stderr);- info!("ffmpeg stdout: {}", stdout);- info!("ffmpeg stderr: {}", stderr);+ debug!("ffmpeg stdout: {}", stdout);+ debug!("ffmpeg stderr: {}", stderr);if status.status.success() {- if output_path.exists() {- info!("Video merged successfully: {:?}", output_path);- Ok(())- } else {- Err(anyhow::anyhow!(+ match output_path.try_exists() {+ Ok(true) => {+ info!("videos merged successfully: {:?}", output_path);+ Ok(MergeVideosResponse {+ video_path: output_path.to_string_lossy().into_owned(),+ })+ }+ Ok(false) => Err(anyhow::anyhow!("ffmpeg reported success, but output file not found: {:?}",output_path- ))+ )),+ Err(e) => Err(anyhow::anyhow!(+ "failed to check if output file exists: {:?}",+ e+ )),}} else {Err(anyhow::anyhow!(- "ffmpeg failed to merge frames into video. frame list: {:?}",- frame_list_path+ "ffmpeg failed to merge videos. error: {}",+ stderr))}}