Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sysinfo = "0.32.1"
csv = "1.3.1"
humantime = "2.1.0"
clipboard = "0.5.0"
clipboard-win = "5.4.0"
reqwest = { version = "0.12.1", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
once_cell = "1.19.0"
Expand Down
101 changes: 101 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use anyhow::{bail, Context, Result};
use std::path::{Path, PathBuf};
use std::process::Command;
use uuid::Uuid;
#[cfg(target_os = "windows")]
use clipboard_win::{formats::FileList, Clipboard, Getter, Setter};

/// Convert `input` to `output_ext` using ffmpeg and return the path
/// to the converted file.
///
/// The resulting file is created in the system temp directory with a
/// random name so the original file is never overwritten.
pub fn convert_with_ffmpeg(input: &Path, output_ext: &str) -> Result<PathBuf> {
let out_path = std::env::temp_dir().join(format!("converted-{}.{output_ext}", Uuid::new_v4()));

let status = Command::new("ffmpeg")
.args([
"-y",
"-i",
input
.to_str()
.context("Failed to convert input path to string")?,
out_path
.to_str()
.context("Failed to convert output path to string")?,
])
.status()
.context("Failed to execute ffmpeg")?;

if !status.success() {
bail!("ffmpeg failed to convert file");
}

Ok(out_path)
}

/// Convert the file currently stored in the clipboard to `output_ext`
/// using ffmpeg and put the resulting file back on the clipboard.
///
/// On non-Windows platforms this returns an error.
#[cfg(target_os = "windows")]
pub fn convert_clipboard_file(output_ext: &str) -> Result<PathBuf> {
let _clip = Clipboard::new_attempts(10)
.map_err(|e| anyhow::anyhow!("Failed to open clipboard: {e:?}"))?;

let mut files = Vec::<PathBuf>::new();
FileList
.read_clipboard(&mut files)
.map_err(|e| anyhow::anyhow!("Failed to read clipboard files: {e:?}"))?;

let input = files
.get(0)
.cloned()
.context("Clipboard does not contain a file")?;

let out = convert_with_ffmpeg(&input, output_ext)?;

let out_str = out.to_string_lossy().to_string();
FileList
.write_clipboard(&[out_str.as_str()])
.map_err(|e| anyhow::anyhow!("Failed to set clipboard files: {e:?}"))?;

Ok(out)
}

#[cfg(not(target_os = "windows"))]
pub fn convert_clipboard_file(_output_ext: &str) -> Result<PathBuf> {
bail!("convert_clipboard_file is only supported on Windows")
}

#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::fs;

#[test]
fn convert_with_ffmpeg_fails_if_ffmpeg_returns_error() {
let dir = tempfile::tempdir().unwrap();
let ffmpeg_path = dir.path().join("ffmpeg");
fs::write(&ffmpeg_path, "#!/bin/sh\nexit 1\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(&ffmpeg_path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&ffmpeg_path, perms).unwrap();
}

let old_path = env::var("PATH").unwrap_or_default();
env::set_var("PATH", format!("{}:{}", dir.path().display(), old_path));

let input = dir.path().join("in.wav");
fs::write(&input, b"dummy").unwrap();

let res = convert_with_ffmpeg(&input, "mp3");
assert!(res.is_err());

env::set_var("PATH", old_path);
}
}
31 changes: 31 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use timers::*;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::filter::FilterFn;
use tracing_subscriber::Registry;
mod convert;
mod default_device_sink;
mod timers;
mod transcribe;
Expand Down Expand Up @@ -567,6 +568,26 @@ fn call_fn(
}
}

"convert_clipboard_file" => {
let args = match serde_json::from_str::<serde_json::Value>(fn_args) {
Ok(json) => json,
Err(e) => return Some(format!("Failed to parse arguments: {}", e)),
};

let output_ext = match args["output_ext"].as_str() {
Some(ext) => ext,
None => return Some("Missing 'output_ext' argument.".to_string()),
};

match convert::convert_clipboard_file(output_ext) {
Ok(out) => Some(format!(
"Converted file copied to clipboard as {}",
out.display()
)),
Err(e) => Some(format!("Failed to convert clipboard file: {}", e)),
}
}

"set_speech_speed" => {
let args: serde_json::Value = serde_json::from_str(fn_args).unwrap();
if let Some(speed) = args["speed"].as_f64() {
Expand Down Expand Up @@ -1530,6 +1551,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
}))
.build().unwrap(),

ChatCompletionFunctionsArgs::default()
.name("convert_clipboard_file")
.description("Converts the file currently stored in the clipboard to another format using ffmpeg and copies the new file back to the clipboard.")
.parameters(json!({
"type": "object",
"properties": {"output_ext": {"type": "string"}},
"required": ["output_ext"],
}))
.build().unwrap(),

ChatCompletionFunctionsArgs::default()
.name("set_speech_speed")
.description("Sets how fast the AI voice speaks. Speed must be between 0.5 and 100.0.")
Expand Down