Skip to content

Commit 2cb138d

Browse files
committed
[rust] Use mpeg-4 video codec for recording desktop (balance size & quality(
1 parent 733f227 commit 2cb138d

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

rust/src/ffmpeg.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::files::{
2424
use crate::lock::Lock;
2525
use crate::logger::Logger;
2626
use crate::shell::{run_shell_command_with_stderr, Command};
27-
use crate::{format_four_args, format_one_arg, format_three_args, format_two_args, ENV_DISPLAY};
27+
use crate::{format_five_args, format_four_args, format_one_arg, format_two_args, ENV_DISPLAY};
2828
use anyhow::{anyhow, Error};
2929
use reqwest::Client;
3030
use std::fs::File;
@@ -38,10 +38,11 @@ const FFMPEG_WINDOWS_RELEASE_URL: &str =
3838
"https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-{}-essentials_build.7z";
3939
const FFMPEG_LINUX_RELEASE_URL: &str = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n{}-latest-linux64-gpl-{}.tar.xz";
4040
const FFMPEG_MACOS_RELEASE_URL: &str = "https://evermeet.cx/ffmpeg/ffmpeg-{}.zip";
41+
const FFMPEG_RECORD_DESKTOP_WINDOWS_COMMAND: &str = "{} -f gdigrab -i desktop -r {} -c:v {} -y {}";
42+
const FFMPEG_RECORD_DESKTOP_LINUX_COMMAND: &str = "{} -f x11grab -i {} -r {} -c:v {} -y {}";
43+
const FFMPEG_RECORD_DESKTOP_MACOS_COMMAND: &str = "{} -f avfoundation -i 0 -r {} -c:v {} -y {}";
4144
const FFMPEG_RECORD_FRAME_RATE: &str = "30";
42-
const FFMPEG_RECORD_DESKTOP_WINDOWS_COMMAND: &str = "{} -f gdigrab -i desktop -r {} -y {}";
43-
const FFMPEG_RECORD_DESKTOP_LINUX_COMMAND: &str = "{} -f x11grab -i {} -r {} -y {}";
44-
const FFMPEG_RECORD_DESKTOP_MACOS_COMMAND: &str = "{} -f avfoundation -i 0 -r {} -y {}";
45+
const FFMPEG_RECORD_VIDEO_CODEC: &str = "mpeg4";
4546
const FFMPEG_RECORDING_EXTENSION: &str = "avi";
4647
const FFMPEG_RECORDING_FOLDER: &str = "recordings";
4748
const FFMPEG_DEFAULT_DISPLAY: &str = ":0";
@@ -211,18 +212,20 @@ pub fn record_desktop_with_ffmpeg(
211212
));
212213
env_display = FFMPEG_DEFAULT_DISPLAY.to_string();
213214
}
214-
Command::new_single(format_four_args(
215+
Command::new_single(format_five_args(
215216
get_recording_command(os),
216217
&path_to_string(&ffmpeg_path),
217218
&env_display,
218219
FFMPEG_RECORD_FRAME_RATE,
220+
FFMPEG_RECORD_VIDEO_CODEC,
219221
&recording_name,
220222
))
221223
} else {
222-
Command::new_single(format_three_args(
224+
Command::new_single(format_four_args(
223225
get_recording_command(os),
224226
&path_to_string(&ffmpeg_path),
225227
FFMPEG_RECORD_FRAME_RATE,
228+
FFMPEG_RECORD_VIDEO_CODEC,
226229
&recording_name,
227230
))
228231
};

rust/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,13 +902,14 @@ pub trait SeleniumManager {
902902
Ok(())
903903
}
904904

905-
fn record_desktop(&self, ffmpeg: bool, record: bool) -> Result<(), Error> {
905+
fn record_desktop(&mut self, ffmpeg: bool, record: bool) -> Result<(), Error> {
906906
let mut ffmpeg_path: Option<PathBuf> = None;
907907
if ffmpeg {
908908
ffmpeg_path = Some(self.get_or_download_ffmpeg()?);
909909
}
910910
if record {
911911
let cache_path = self.get_cache_path()?.unwrap_or_default();
912+
self.set_fallback_driver_from_cache(false);
912913
record_desktop_with_ffmpeg(
913914
ffmpeg_path.unwrap_or(self.get_or_download_ffmpeg()?),
914915
self.get_os(),
@@ -1709,6 +1710,22 @@ pub fn format_four_args(string: &str, arg1: &str, arg2: &str, arg3: &str, arg4:
17091710
.replacen("{}", arg4, 1)
17101711
}
17111712

1713+
pub fn format_five_args(
1714+
string: &str,
1715+
arg1: &str,
1716+
arg2: &str,
1717+
arg3: &str,
1718+
arg4: &str,
1719+
arg5: &str,
1720+
) -> String {
1721+
string
1722+
.replacen("{}", arg1, 1)
1723+
.replacen("{}", arg2, 1)
1724+
.replacen("{}", arg3, 1)
1725+
.replacen("{}", arg4, 1)
1726+
.replacen("{}", arg5, 1)
1727+
}
1728+
17121729
// ----------------------------------------------------------
17131730
// Private functions
17141731
// ----------------------------------------------------------

0 commit comments

Comments
 (0)