tauri-app: game review, with video showcase, stats and clip export
record-daemon / Build, check and test (push) Successful in 2m6s
record-daemon / Build, check and test (push) Successful in 2m6s
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
use ffmpeg_sidecar::{
|
||||
command::{ffmpeg_is_installed, FfmpegCommand},
|
||||
download::auto_download,
|
||||
event::{FfmpegEvent, LogLevel},
|
||||
paths::sidecar_path,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
@@ -77,6 +83,312 @@ fn get_recordings_dir() -> String {
|
||||
.unwrap_or_else(|| "./recordings".to_string())
|
||||
}
|
||||
|
||||
/// Find a video file in the recordings directory.
|
||||
/// Searches for exact filename match or pattern match.
|
||||
#[tauri::command]
|
||||
fn find_video_file(recordings_dir: String, filename: String) -> Option<String> {
|
||||
let recordings_path = PathBuf::from(&recordings_dir);
|
||||
|
||||
if !recordings_path.exists() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Try exact match first
|
||||
let exact_path = recordings_path.join(&filename);
|
||||
if exact_path.exists() {
|
||||
return Some(exact_path.to_string_lossy().to_string());
|
||||
}
|
||||
|
||||
// Try to find by pattern (date_time_*.mp4)
|
||||
if let Ok(entries) = fs::read_dir(&recordings_path) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.extension().map(|e| e == "mp4" || e == "mkv" || e == "mov").unwrap_or(false) {
|
||||
let file_name = path.file_name()?.to_string_lossy().to_string();
|
||||
|
||||
// Check if filename starts with the same date/time pattern
|
||||
// filename format: "2026-03-27_16-42-52_unknown.mp4"
|
||||
// We match the date_time prefix
|
||||
if file_name.starts_with(&filename.replace("_unknown.mp4", "")) {
|
||||
return Some(path.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Get list of video files in recordings directory.
|
||||
#[tauri::command]
|
||||
fn list_video_files(recordings_dir: String) -> Vec<String> {
|
||||
let recordings_path = PathBuf::from(&recordings_dir);
|
||||
let mut videos = Vec::new();
|
||||
|
||||
if !recordings_path.exists() {
|
||||
return videos;
|
||||
}
|
||||
|
||||
if let Ok(entries) = fs::read_dir(&recordings_path) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.extension().map(|e| e == "mp4" || e == "mkv" || e == "mov").unwrap_or(false) {
|
||||
videos.push(path.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by modification time, newest first
|
||||
videos.sort_by(|a, b| {
|
||||
let a_time = fs::metadata(a).and_then(|m| m.modified()).ok();
|
||||
let b_time = fs::metadata(b).and_then(|m| m.modified()).ok();
|
||||
b_time.cmp(&a_time)
|
||||
});
|
||||
|
||||
videos
|
||||
}
|
||||
|
||||
/// Ensure ffmpeg is available, downloading if necessary.
|
||||
fn ensure_ffmpeg() -> Result<(), String> {
|
||||
if ffmpeg_is_installed() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Auto-download ffmpeg for the current platform
|
||||
auto_download().map_err(|e| format!("Failed to download ffmpeg: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Export a clip from a video using ffmpeg-sidecar (stream copy for speed).
|
||||
#[tauri::command]
|
||||
fn export_clip(
|
||||
video_path: String,
|
||||
start_time: f64,
|
||||
end_time: f64,
|
||||
output_name: String,
|
||||
) -> Result<String, String> {
|
||||
// Ensure ffmpeg is available
|
||||
ensure_ffmpeg()?;
|
||||
|
||||
// Verify input file exists
|
||||
if !PathBuf::from(&video_path).exists() {
|
||||
return Err(format!("Video file not found: {}", video_path));
|
||||
}
|
||||
|
||||
// Get output directory
|
||||
let output_dir = get_default_output_dir()
|
||||
.map(|p| p.join("clips"))
|
||||
.unwrap_or_else(|| PathBuf::from("./clips"));
|
||||
|
||||
// Create clips directory if it doesn't exist
|
||||
if !output_dir.exists() {
|
||||
fs::create_dir_all(&output_dir)
|
||||
.map_err(|e| format!("Failed to create clips directory: {}", e))?;
|
||||
}
|
||||
|
||||
let output_path = output_dir.join(format!("{}.mp4", output_name));
|
||||
let duration = end_time - start_time;
|
||||
|
||||
// Build ffmpeg command with stream copy (fast, no re-encoding)
|
||||
// Note: Put -ss before -i for fast seeking (input seeking)
|
||||
let iter = FfmpegCommand::new()
|
||||
.arg("-y") // Overwrite output file
|
||||
.arg("-ss").arg(&format!("{:.3}", start_time))
|
||||
.arg("-i").arg(&video_path)
|
||||
.arg("-t").arg(&format!("{:.3}", duration))
|
||||
.arg("-c").arg("copy") // Stream copy for fast export
|
||||
.arg("-avoid_negative_ts").arg("make_zero")
|
||||
.arg(&output_path.to_string_lossy().to_string())
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn ffmpeg: {}", e))?
|
||||
.iter()
|
||||
.map_err(|e| format!("Failed to create ffmpeg iterator: {}", e))?;
|
||||
|
||||
// Process the ffmpeg command and collect all errors
|
||||
let mut errors = Vec::new();
|
||||
for event in iter {
|
||||
match event {
|
||||
FfmpegEvent::Error(e) => {
|
||||
errors.push(format!("FFmpeg error: {}", e));
|
||||
}
|
||||
FfmpegEvent::Log(LogLevel::Error, msg) => {
|
||||
errors.push(format!("FFmpeg log error: {}", msg));
|
||||
}
|
||||
FfmpegEvent::Log(LogLevel::Warning, msg) => {
|
||||
// Log warnings but don't fail
|
||||
eprintln!("FFmpeg warning: {}", msg);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if output file was created
|
||||
if !output_path.exists() {
|
||||
return Err(format!(
|
||||
"Export failed - output file not created. Errors: {}",
|
||||
errors.join("; ")
|
||||
));
|
||||
}
|
||||
|
||||
// Check if output file has content
|
||||
let metadata = fs::metadata(&output_path)
|
||||
.map_err(|e| format!("Failed to check output file: {}", e))?;
|
||||
if metadata.len() == 0 {
|
||||
return Err(format!(
|
||||
"Export failed - output file is empty. Errors: {}",
|
||||
errors.join("; ")
|
||||
));
|
||||
}
|
||||
|
||||
Ok(output_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// Export a clip with re-encoding for more precise cuts.
|
||||
#[tauri::command]
|
||||
fn export_clip_precise(
|
||||
video_path: String,
|
||||
start_time: f64,
|
||||
end_time: f64,
|
||||
output_name: String,
|
||||
quality: String,
|
||||
) -> Result<String, String> {
|
||||
// Ensure ffmpeg is available
|
||||
ensure_ffmpeg()?;
|
||||
|
||||
// Verify input file exists
|
||||
if !PathBuf::from(&video_path).exists() {
|
||||
return Err(format!("Video file not found: {}", video_path));
|
||||
}
|
||||
|
||||
let output_dir = get_default_output_dir()
|
||||
.map(|p| p.join("clips"))
|
||||
.unwrap_or_else(|| PathBuf::from("./clips"));
|
||||
|
||||
if !output_dir.exists() {
|
||||
fs::create_dir_all(&output_dir)
|
||||
.map_err(|e| format!("Failed to create clips directory: {}", e))?;
|
||||
}
|
||||
|
||||
let output_path = output_dir.join(format!("{}.mp4", output_name));
|
||||
let duration = end_time - start_time;
|
||||
|
||||
// Quality presets (CRF values - lower is better quality)
|
||||
let crf = match quality.as_str() {
|
||||
"low" => "28",
|
||||
"medium" => "23",
|
||||
"high" => "18",
|
||||
_ => "23",
|
||||
};
|
||||
|
||||
// Build ffmpeg command with re-encoding
|
||||
let iter = FfmpegCommand::new()
|
||||
.arg("-y") // Overwrite output file
|
||||
.arg("-ss").arg(&format!("{:.3}", start_time))
|
||||
.arg("-i").arg(&video_path)
|
||||
.arg("-t").arg(&format!("{:.3}", duration))
|
||||
.arg("-c:v").arg("libx264")
|
||||
.arg("-crf").arg(crf)
|
||||
.arg("-preset").arg("fast")
|
||||
.arg("-c:a").arg("aac")
|
||||
.arg("-b:a").arg("128k")
|
||||
.arg(&output_path.to_string_lossy().to_string())
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn ffmpeg: {}", e))?
|
||||
.iter()
|
||||
.map_err(|e| format!("Failed to create ffmpeg iterator: {}", e))?;
|
||||
|
||||
// Process the ffmpeg command and collect all errors
|
||||
let mut errors = Vec::new();
|
||||
for event in iter {
|
||||
match event {
|
||||
FfmpegEvent::Error(e) => {
|
||||
errors.push(format!("FFmpeg error: {}", e));
|
||||
}
|
||||
FfmpegEvent::Log(LogLevel::Error, msg) => {
|
||||
errors.push(format!("FFmpeg log error: {}", msg));
|
||||
}
|
||||
FfmpegEvent::Log(LogLevel::Warning, msg) => {
|
||||
eprintln!("FFmpeg warning: {}", msg);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if output file was created
|
||||
if !output_path.exists() {
|
||||
return Err(format!(
|
||||
"Export failed - output file not created. Errors: {}",
|
||||
errors.join("; ")
|
||||
));
|
||||
}
|
||||
|
||||
// Check if output file has content
|
||||
let metadata = fs::metadata(&output_path)
|
||||
.map_err(|e| format!("Failed to check output file: {}", e))?;
|
||||
if metadata.len() == 0 {
|
||||
return Err(format!(
|
||||
"Export failed - output file is empty. Errors: {}",
|
||||
errors.join("; ")
|
||||
));
|
||||
}
|
||||
|
||||
Ok(output_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// Check if ffmpeg is available.
|
||||
#[tauri::command]
|
||||
fn check_ffmpeg() -> bool {
|
||||
ffmpeg_is_installed()
|
||||
}
|
||||
|
||||
/// Download ffmpeg if not already installed.
|
||||
#[tauri::command]
|
||||
fn download_ffmpeg() -> Result<String, String> {
|
||||
ensure_ffmpeg()?;
|
||||
Ok(sidecar_path().unwrap_or_default().to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// Get video file metadata using ffprobe.
|
||||
#[tauri::command]
|
||||
fn get_video_metadata(video_path: String) -> Result<Value, String> {
|
||||
// Ensure ffmpeg is available
|
||||
ensure_ffmpeg()?;
|
||||
|
||||
// Use ffprobe via ffmpeg-sidecar
|
||||
let iter = FfmpegCommand::new()
|
||||
.arg("-v").arg("quiet")
|
||||
.arg("-print_format").arg("json")
|
||||
.arg("-show_format")
|
||||
.arg("-show_streams")
|
||||
.arg(&video_path)
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to spawn ffprobe: {}", e))?
|
||||
.iter()
|
||||
.map_err(|e| format!("Failed to create ffprobe iterator: {}", e))?;
|
||||
|
||||
let mut json_output = String::new();
|
||||
|
||||
for event in iter {
|
||||
match event {
|
||||
FfmpegEvent::Log(LogLevel::Info, msg) | FfmpegEvent::Log(LogLevel::Unknown, msg) => {
|
||||
// Capture JSON output
|
||||
if msg.trim().starts_with('{') || msg.trim().starts_with('[') {
|
||||
json_output.push_str(&msg);
|
||||
}
|
||||
}
|
||||
FfmpegEvent::Error(e) => {
|
||||
return Err(format!("FFprobe error: {}", e));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the JSON output
|
||||
serde_json::from_str(&json_output)
|
||||
.map_err(|e| format!("Failed to parse ffprobe output: {}", e))
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
@@ -84,7 +396,14 @@ pub fn run() {
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_game_history,
|
||||
get_timeline,
|
||||
get_recordings_dir
|
||||
get_recordings_dir,
|
||||
find_video_file,
|
||||
list_video_files,
|
||||
export_clip,
|
||||
export_clip_precise,
|
||||
check_ffmpeg,
|
||||
download_ffmpeg,
|
||||
get_video_metadata
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user