From 42b77654a4866ae3e5d5954fca6830675423da38 Mon Sep 17 00:00:00 2001 From: Salem Yaslem Date: Thu, 11 Feb 2021 13:30:38 +0300 Subject: [PATCH] add record, stop, play functions --- src/ffmpeg_interface.rs | 92 +++++++++++++++++++++++++++++++---------- src/main.rs | 45 +++++++++++++------- 2 files changed, 102 insertions(+), 35 deletions(-) diff --git a/src/ffmpeg_interface.rs b/src/ffmpeg_interface.rs index ba4bb89..2cdba6d 100644 --- a/src/ffmpeg_interface.rs +++ b/src/ffmpeg_interface.rs @@ -1,66 +1,116 @@ use std::path::PathBuf; use std::process::Command; -use std::time::Duration; use std::thread::sleep; +use std::time::Duration; +use gtk::{CheckButton, SpinButton, ComboBoxText, FileChooser, Entry, ToggleButtonExt, SpinButtonExt, ComboBoxExt, FileChooserExt, EntryExt}; +#[derive(Clone)] pub struct Ffmpeg { - pub filename: PathBuf, - pub record_video: bool, - pub record_audio: bool, - pub audio_id: String, - pub record_mouse: bool, - pub follow_mouse: bool, - pub record_frames: String, - pub record_delay: u64, + pub filename: (FileChooser, Entry, ComboBoxText), + pub record_video: CheckButton, + pub record_audio: CheckButton, + pub audio_id: ComboBoxText, + pub record_mouse: CheckButton, + pub follow_mouse: CheckButton, + pub record_frames: SpinButton, + pub record_delay: SpinButton, + pub process_id: Option } + impl Ffmpeg { - pub fn record(self, x: i16, y: i16, width: i16, height: i16) -> u32 { + pub fn start_record(&mut self, x: i16, y: i16, width: i16, height: i16) -> u32 { + if self.process_id.is_some() { + Command::new("kill").arg(format!("{}", self.process_id.unwrap())).output().unwrap(); + } + let mut ffmpeg_command: Command = Command::new("ffmpeg"); // if recorder video switch is enabled, record video with specified width and hight - if self.record_video { + if self.record_video.get_active() { ffmpeg_command.arg("-video_size"); ffmpeg_command.arg(format!("{}x{}", width, height)); } // if show mouse switch is enabled, draw the mouse to video - if self.record_mouse { + if self.record_mouse.get_active() { ffmpeg_command.arg("-draw_mouse"); ffmpeg_command.arg("1"); } // if follow mouse switch is enabled, follow the mouse - if self.follow_mouse { + if self.follow_mouse.get_active() { ffmpeg_command.arg("-follow_mouse"); ffmpeg_command.arg("centered"); } ffmpeg_command.arg("-framerate"); - ffmpeg_command.arg(self.record_frames); + ffmpeg_command.arg(format!("{}", self.record_frames.get_value())); ffmpeg_command.arg("-f"); ffmpeg_command.arg("x11grab"); ffmpeg_command.arg("-i"); - ffmpeg_command.arg(format!("{}+{},{}", std::env::var("DISPLAY").expect(":1").as_str(), x, y)); + ffmpeg_command.arg(format!( + "{}+{},{}", + std::env::var("DISPLAY").expect(":1").as_str(), + x, + y + )); // if follow audio switch is enabled, record the audio - if self.record_audio { + if self.record_audio.get_active() { ffmpeg_command.arg("-f"); ffmpeg_command.arg("pulse"); ffmpeg_command.arg("-i"); - ffmpeg_command.arg(self.audio_id); + ffmpeg_command.arg(self.audio_id.get_active_id().unwrap().to_string()); ffmpeg_command.arg("-strict"); ffmpeg_command.arg("-2"); } - ffmpeg_command.arg("-q"); ffmpeg_command.arg("1"); - ffmpeg_command.arg(self.filename); + + + ffmpeg_command.arg({ + self.filename.0.get_filename() + .unwrap() + .join(PathBuf::from(format!( + "{}.{}", + if self.filename.1.get_text().to_string().trim().eq("") { + self.filename.1.get_text().to_string() + } else { + self.filename.1.get_text().to_string().trim().to_string() + }, + self.filename.2.get_active_id().unwrap().to_string() + ))) + }); ffmpeg_command.arg("-y"); // sleep for delay - sleep(Duration::new(self.record_delay, 0)); + sleep(Duration::from_secs(self.record_delay.get_value() as u64)); // start recording and return the process id - ffmpeg_command.spawn().unwrap().id() + self.process_id = Some(ffmpeg_command.spawn().unwrap().id()); + println!("{}", self.process_id.unwrap()); + self.process_id.unwrap() + } + + pub fn stop_record(self) { + if self.process_id.is_some() { + Command::new("kill").arg(format!("{}", self.process_id.unwrap())).output().unwrap(); + } + } + + pub fn play_record(self) { + Command::new("xdg-open").arg({ + self.filename.0.get_filename() + .unwrap() + .join(PathBuf::from(format!( + "{}.{}", + if self.filename.1.get_text().to_string().trim().eq("") { + self.filename.1.get_text().to_string() + } else { + self.filename.1.get_text().to_string().trim().to_string() + }, + self.filename.2.get_active_id().unwrap().to_string() + ))) + }).output().unwrap(); } } diff --git a/src/main.rs b/src/main.rs index 3acb663..e2b3ce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,14 +5,16 @@ mod config_management; mod ffmpeg_interface; // use gio::prelude::*; +use std::rc::Rc; +use std::cell::RefCell; use glib::signal::Inhibit; use gtk::prelude::*; use gtk::ComboBoxText; use gtk::{ - AboutDialog, Adjustment, Builder, Button, CheckButton, CssProvider, Entry, FileChooser, Label, - MenuItem, SpinButton, Window, + AboutDialog, Builder, Button, CheckButton, CssProvider, Entry, FileChooser, Label, MenuItem, + SpinButton, Window, }; -use std::path::{Path,PathBuf}; +use std::path::Path; use std::process::{Command, Stdio}; fn main() { @@ -213,18 +215,33 @@ fn main() { _area_chooser_window.show(); }); + // init record struct + let ffmpeg_record_interface: Rc> = Rc::new(RefCell::new(ffmpeg_interface::Ffmpeg { + filename: (folder_chooser, filename_entry, format_chooser_combobox), + record_video: video_switch, + record_audio: audio_switch, + audio_id: audio_source_combobox, + record_mouse: mouse_switch, + follow_mouse: follow_mouse_switch, + record_frames: frames_spin, + record_delay: delay_spin, + process_id: None + })); + + let mut _ffmpeg_record_interface = ffmpeg_record_interface.clone(); + record_button.connect_clicked(move |_| { - let record_options = ffmpeg_interface::Ffmpeg { - filename: folder_chooser.get_filename().unwrap().join(PathBuf::from(format!("{}.{}", if filename_entry.get_text().to_string().trim().eq("") { filename_entry.get_text().to_string() } else { filename_entry.get_text().to_string().trim().to_string() }, format_chooser_combobox.get_active_id().unwrap().to_string()))), - record_video: video_switch.get_active(), - record_audio: audio_switch.get_active(), - audio_id: audio_source_combobox.get_active_id().unwrap().to_string(), - record_mouse: mouse_switch.get_active(), - follow_mouse: follow_mouse_switch.get_active(), - record_frames: format!("{}", frames_spin.get_value()), - record_delay: delay_spin.get_value() as u64 - }; - record_options.record(0, 0, 512, 512); + _ffmpeg_record_interface.borrow_mut().start_record(0, 0, 512, 512); + }); + + let mut _ffmpeg_record_interface = ffmpeg_record_interface.clone(); + stop_button.connect_clicked(move |_| { + _ffmpeg_record_interface.borrow_mut().clone().stop_record(); + }); + + let mut _ffmpeg_record_interface = ffmpeg_record_interface.clone(); + play_button.connect_clicked(move |_| { + _ffmpeg_record_interface.borrow_mut().clone().play_record(); }); // Windows