Skip to content

Commit

Permalink
tested ffmpeg combining
Browse files Browse the repository at this point in the history
  • Loading branch information
hatomist committed Nov 22, 2024
1 parent 852267f commit 54850bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
26 changes: 18 additions & 8 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,19 +961,13 @@ impl PyKRec {
Ok(Self { inner: krec })
}

fn combine_with_video(
&self,
video_path: &str,
output_path: &str,
uuid: &str,
task: &str,
) -> PyResult<()> {
fn combine_with_video(&self, video_path: &str, output_path: &str) -> PyResult<()> {
// First save the KRec to a temporary file
let temp_path = format!("{}.tmp.krec", output_path);
self.save(&temp_path)?;

// Combine with video
krec_rs::combine_with_video(video_path, &temp_path, output_path, uuid, task)
krec_rs::combine_with_video(video_path, &temp_path, output_path)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyIOError, _>(e.to_string()))?;

// Clean up temporary file
Expand Down Expand Up @@ -1303,6 +1297,18 @@ impl FrameIterator {
}
}

#[pyfunction]
fn combine_with_video(video_path: &str, krec_path: &str, output_path: &str) -> PyResult<()> {
krec_rs::combine_with_video(video_path, krec_path, output_path)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyIOError, _>(e.to_string()))
}

#[pyfunction]
fn extract_from_video(video_path: &str, output_path: &str) -> PyResult<()> {
krec_rs::extract_from_video(video_path, output_path)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyIOError, _>(e.to_string()))
}

#[pymodule]
fn krec(_py: Python, m: &PyModule) -> PyResult<()> {
let _ = krec_rs::init();
Expand All @@ -1315,5 +1321,9 @@ fn krec(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PyKRecFrame>()?;
m.add_class::<PyKRecHeader>()?;
m.add_class::<PyKRec>()?;
m.add_class::<FrameIterator>()?;
m.add_function(wrap_pyfunction!(combine_with_video, m)?)?;
m.add_function(wrap_pyfunction!(extract_from_video, m)?)?;

Ok(())
}
35 changes: 31 additions & 4 deletions src/ffmpeg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::KRec;
use color_eyre::{eyre::eyre, Result};
use std::path::Path;
use thiserror::Error;
Expand All @@ -14,8 +15,6 @@ pub fn combine_with_video(
video_path: impl AsRef<Path>,
krec_path: impl AsRef<Path>,
output_path: impl AsRef<Path>,
uuid: &str,
task: &str,
) -> Result<()> {
info!("Combining video with KRec data");
debug!(
Expand All @@ -25,6 +24,30 @@ pub fn combine_with_video(
output_path.as_ref().display()
);

// Read the KRec file to get UUID and task
let krec = KRec::load(
krec_path
.as_ref()
.to_str()
.ok_or_else(|| eyre!("Invalid KRec path: {}", krec_path.as_ref().display()))?,
)?;

if krec.header.uuid.is_empty() {
return Err(eyre!("KRec file missing UUID"));
}

if krec.header.task.is_empty() {
return Err(eyre!("KRec file missing task"));
}

if krec.header.robot_platform.is_empty() {
return Err(eyre!("KRec file missing robot platform"));
}

if krec.header.robot_serial.is_empty() {
return Err(eyre!("KRec file missing robot serial"));
}

let status = std::process::Command::new("ffmpeg")
.args([
"-i",
Expand All @@ -34,9 +57,13 @@ pub fn combine_with_video(
"-metadata:s:t",
"mimetype=application/octet-stream",
"-metadata:s:t",
&format!("uuid={}", uuid),
&format!("uuid={}", krec.header.uuid),
"-metadata:s:t",
&format!("task={}", krec.header.task),
"-metadata:s:t",
&format!("robot_platform={}", krec.header.robot_platform),
"-metadata:s:t",
&format!("task={}", task),
&format!("robot_serial={}", krec.header.robot_serial),
"-c",
"copy",
&output_path.as_ref().to_string_lossy(),
Expand Down

0 comments on commit 54850bb

Please sign in to comment.