Skip to content

Commit

Permalink
feat(x256): add x256 support mediar-ai#666
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred committed Nov 13, 2024
1 parent 6c33a98 commit b987066
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
39 changes: 37 additions & 2 deletions screenpipe-core/src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ pub fn find_ffmpeg_path() -> Option<PathBuf> {
FFMPEG_PATH.as_ref().map(|p| p.clone())
}

pub fn check_x265_support() -> bool {
if let Some(ffmpeg_path) = find_ffmpeg_path() {
match std::process::Command::new(ffmpeg_path)
.args(&["-codecs"])
.output()
{
Ok(output) => {
let output_str = String::from_utf8_lossy(&output.stdout);
output_str.contains("x265")
},
Err(_) => false
}
} else {
false
}
}

fn find_ffmpeg_path_internal() -> Option<PathBuf> {
debug!("Starting search for ffmpeg executable");

Expand Down Expand Up @@ -114,8 +131,24 @@ fn find_ffmpeg_path_internal() -> Option<PathBuf> {

fn handle_ffmpeg_installation() -> Result<(), String> {
if ffmpeg_is_installed() {
debug!("ffmpeg is already installed");
return Ok(());
#[cfg(target_os = "macos")]
{
if !check_x265_support() {
debug!("FFmpeg installed but x265 not found. Reinstalling...");
if let Some(path) = find_ffmpeg_path() {
let _ = std::fs::remove_file(path);
}
} else {
debug!("ffmpeg is already installed with x265 support");
return Ok(());
}
}

#[cfg(not(target_os = "macos"))]
{
debug!("ffmpeg is already installed");
return Ok(());
}
}

debug!("ffmpeg not found. installing...");
Expand All @@ -140,3 +173,5 @@ fn handle_ffmpeg_installation() -> Result<(), String> {
debug!("done! installed ffmpeg version {}", version);
Ok(())
}


2 changes: 1 addition & 1 deletion screenpipe-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod ffmpeg;
pub use ffmpeg::find_ffmpeg_path;
pub use ffmpeg::{find_ffmpeg_path, check_x265_support};
#[cfg(feature = "llm")]
pub mod llm;
#[cfg(feature = "llm")]
Expand Down
45 changes: 40 additions & 5 deletions screenpipe-server/src/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ impl VideoCapture {
}

pub async fn start_ffmpeg_process(output_file: &str, fps: f64) -> Result<Child, anyhow::Error> {
// Overriding fps with max fps if over the max and warning user
let fps = if fps > MAX_FPS {
warn!("Overriding FPS from {} to {}", fps, MAX_FPS);
MAX_FPS
Expand All @@ -167,12 +166,47 @@ pub async fn start_ffmpeg_process(output_file: &str, fps: f64) -> Result<Child,
"pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2",
];

// TODO: issue on macos https://github.com/mediar-ai/screenpipe/pull/580
#[cfg(target_os = "macos")]
args.extend_from_slice(&["-vcodec", "libx264", "-preset", "ultrafast", "-crf", "23"]);

let use_x265 = screenpipe_core::check_x265_support();

#[cfg(target_os = "macos")]
{
if use_x265 {
info!("Using x265 codec on macOS");
args.extend_from_slice(&[
"-vcodec",
"libx265",
"-preset",
"ultrafast",
"-crf",
"23",
"-x265-params",
"log-level=error:pools=4"
]);
} else {
warn!("x265 not available on macOS, using x264");
args.extend_from_slice(&[
"-vcodec",
"libx264",
"-preset",
"ultrafast",
"-crf",
"23"
]);
}
}

#[cfg(not(target_os = "macos"))]
args.extend_from_slice(&["-vcodec", "libx265", "-preset", "ultrafast", "-crf", "23"]);
args.extend_from_slice(&[
"-vcodec",
"libx265",
"-preset",
"ultrafast",
"-crf",
"23",
"-x265-params",
"log-level=error:pools=4"
]);

args.extend_from_slice(&["-pix_fmt", "yuv420p", output_file]);

Expand Down Expand Up @@ -387,3 +421,4 @@ pub async fn finish_ffmpeg_process(child: Child, stdin: Option<ChildStdin>) {
Err(e) => error!("Failed to wait for FFmpeg process: {}", e),
}
}

0 comments on commit b987066

Please sign in to comment.