Skip to content

Commit 37b7473

Browse files
committed
[rust] Read DISPLAY env for recording command in Linux and macOS
1 parent 7f949cf commit 37b7473

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

rust/src/ffmpeg.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ use crate::files::{
2424
use crate::lock::Lock;
2525
use crate::logger::Logger;
2626
use crate::shell::Command;
27-
use crate::{format_one_arg, format_three_args, format_two_args, run_shell_command_with_log};
27+
use crate::{
28+
format_four_args, format_one_arg, format_three_args, format_two_args,
29+
run_shell_command_with_log, ENV_DISPLAY,
30+
};
2831
use anyhow::Error;
2932
use reqwest::Client;
30-
use std::fs;
3133
use std::fs::File;
3234
use std::path::{Path, PathBuf};
35+
use std::{env, fs};
3336
use xz2::read::XzDecoder;
3437

3538
pub const FFMPEG_NAME: &str = "ffmpeg";
@@ -40,9 +43,8 @@ const FFMPEG_LINUX_RELEASE_URL: &str = "https://github.com/BtbN/FFmpeg-Builds/re
4043
const FFMPEG_MACOS_RELEASE_URL: &str = "https://evermeet.cx/ffmpeg/ffmpeg-{}.zip";
4144
const FFMPEG_RECORD_FRAME_RATE: &str = "30";
4245
const FFMPEG_RECORD_DESKTOP_WINDOWS_COMMAND: &str = "{} -f gdigrab -i desktop -r {} -q:v 1 -y {}";
43-
const FFMPEG_RECORD_DESKTOP_LINUX_COMMAND: &str =
44-
"{} -f x11grab -i $DISPLAY -r {} -vcodec huffyuv -y {}";
45-
const FFMPEG_RECORD_DESKTOP_MACOS_COMMAND: &str = "{} -f avfoundation -i $DISPLAY -r {} -y {}";
46+
const FFMPEG_RECORD_DESKTOP_LINUX_COMMAND: &str = "{} -f x11grab -i {} -r {} -vcodec huffyuv -y {}";
47+
const FFMPEG_RECORD_DESKTOP_MACOS_COMMAND: &str = "{} -f avfoundation -i {} -r {} -y {}";
4648
const FFMPEG_RECORDING_EXTENSION: &str = "avi";
4749
const FFMPEG_RECORDING_FOLDER: &str = "recordings";
4850

@@ -207,12 +209,23 @@ pub fn record_desktop_with_ffmpeg(
207209
"Recording desktop with {} to {}",
208210
FFMPEG_NAME, &recording_name
209211
));
210-
let command = Command::new_single(format_three_args(
211-
get_recording_command(os),
212-
&path_to_string(&ffmpeg_path),
213-
FFMPEG_RECORD_FRAME_RATE,
214-
&recording_name,
215-
));
212+
let command = if WINDOWS.is(os) {
213+
Command::new_single(format_three_args(
214+
get_recording_command(os),
215+
&path_to_string(&ffmpeg_path),
216+
FFMPEG_RECORD_FRAME_RATE,
217+
&recording_name,
218+
))
219+
} else {
220+
let env_display = env::var(ENV_DISPLAY).unwrap_or_default();
221+
Command::new_single(format_four_args(
222+
get_recording_command(os),
223+
&path_to_string(&ffmpeg_path),
224+
&env_display,
225+
FFMPEG_RECORD_FRAME_RATE,
226+
&recording_name,
227+
))
228+
};
216229
run_shell_command_with_log(log, os, command).unwrap();
217230
Ok(())
218231
}

rust/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub const SINGLE_QUOTE: &str = "'";
9999
pub const ENV_PROGRAM_FILES: &str = "PROGRAMFILES";
100100
pub const ENV_PROGRAM_FILES_X86: &str = "PROGRAMFILES(X86)";
101101
pub const ENV_LOCALAPPDATA: &str = "LOCALAPPDATA";
102+
pub const ENV_DISPLAY: &str = "DISPLAY";
102103
pub const ENV_X86: &str = " (x86)";
103104
pub const ARCH_X86: &str = "x86";
104105
pub const ARCH_AMD64: &str = "amd64";
@@ -1700,6 +1701,14 @@ pub fn format_three_args(string: &str, arg1: &str, arg2: &str, arg3: &str) -> St
17001701
.replacen("{}", arg3, 1)
17011702
}
17021703

1704+
pub fn format_four_args(string: &str, arg1: &str, arg2: &str, arg3: &str, arg4: &str) -> String {
1705+
string
1706+
.replacen("{}", arg1, 1)
1707+
.replacen("{}", arg2, 1)
1708+
.replacen("{}", arg3, 1)
1709+
.replacen("{}", arg4, 1)
1710+
}
1711+
17031712
// ----------------------------------------------------------
17041713
// Private functions
17051714
// ----------------------------------------------------------

rust/tests/record_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ fn test_record() {
4545
}
4646
};
4747
println!("Recording test status code: {:?}", status_code);
48-
panic!("Forced error");
4948
}

0 commit comments

Comments
 (0)