From a8f025ec2a4af5473df44499973fb9aa03b17be8 Mon Sep 17 00:00:00 2001 From: Salem Yaslem Date: Wed, 22 Feb 2023 21:44:01 +0300 Subject: [PATCH] use of Childs instead of kill command (Work cross multiple operating systems and can wait for it) --- src/ffmpeg_interface.rs | 54 +++++++++++++++++++++-------------------- src/main.rs | 4 +-- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/ffmpeg_interface.rs b/src/ffmpeg_interface.rs index bab88ce..bd7b01b 100644 --- a/src/ffmpeg_interface.rs +++ b/src/ffmpeg_interface.rs @@ -2,12 +2,10 @@ extern crate subprocess; use chrono::prelude::*; use gettextrs::gettext; use gtk::prelude::*; -use gtk::{ - CheckButton, ComboBoxText, Entry, FileChooserNative, ProgressBar, SpinButton, Window, -}; use gtk::{ButtonsType, DialogFlags, MessageDialog, MessageType}; +use gtk::{CheckButton, ComboBoxText, Entry, FileChooserNative, ProgressBar, SpinButton, Window}; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, Child}; use std::sync::mpsc::Sender; use std::thread::sleep; use std::time::Duration; @@ -54,8 +52,10 @@ pub struct Ffmpeg { pub record_frames: SpinButton, pub record_delay: SpinButton, pub command: Entry, - pub video_process_id: Option, - pub audio_process_id: Option, + + // TODO: fix Clone derive for process childs + pub video_process: Option, + pub audio_process: Option, pub saved_filename: Option, pub unbound: Option>, pub progress_widget: ProgressWidget, @@ -63,6 +63,14 @@ pub struct Ffmpeg { pub overwrite: CheckButton, } +impl std::ops::Deref for Ffmpeg { + type Target = Option; + + fn deref(&self) -> &Self::Target { + &self.video_process + } +} + impl Ffmpeg { pub fn start_record( &mut self, @@ -104,9 +112,8 @@ impl Ffmpeg { &gettext("File already exist. Do you want to overwrite it?"), ); - message_dialog.connect_response(|message_dialog: &MessageDialog, _| { - message_dialog.hide() - }); + message_dialog + .connect_response(|message_dialog: &MessageDialog, _| message_dialog.hide()); message_dialog.show(); @@ -126,7 +133,7 @@ impl Ffmpeg { self.saved_filename.as_ref().unwrap() )); ffmpeg_command.arg("-y"); - self.audio_process_id = Some(ffmpeg_command.spawn().unwrap().id()); + self.video_process = Some(ffmpeg_command.spawn().unwrap()); } if self.record_video.is_active() { @@ -169,8 +176,8 @@ impl Ffmpeg { // sleep for delay sleep(Duration::from_secs(self.record_delay.value() as u64)); // start recording and return the process id - self.video_process_id = Some(ffmpeg_command.spawn().unwrap().id()); - return (self.video_process_id, self.audio_process_id); + self.video_process = Some(ffmpeg_command.spawn().unwrap()); + return (Some(self.video_process.unwrap().id()), Some(self.audio_process.unwrap().id())); } (None, None) @@ -181,24 +188,18 @@ impl Ffmpeg { // kill the process to stop recording self.progress_widget.set_progress("".to_string(), 1, 6); - if self.video_process_id.is_some() { + if self.video_process.is_some() { self.progress_widget .set_progress("Stop Recording Video".to_string(), 1, 6); - Command::new("kill") - .arg(format!("{}", self.video_process_id.unwrap())) - .output() - .unwrap(); + self.video_process.unwrap().kill().unwrap(); } self.progress_widget.set_progress("".to_string(), 2, 6); - if self.audio_process_id.is_some() { + if self.audio_process.is_some() { self.progress_widget .set_progress("Stop Recording Audio".to_string(), 2, 6); - Command::new("kill") - .arg(format!("{}", self.audio_process_id.unwrap())) - .output() - .unwrap(); + self.audio_process.unwrap().kill().unwrap(); } let is_video_record = std::path::Path::new( @@ -254,7 +255,7 @@ impl Ffmpeg { let audio_filename = format!("{}.temp.audio", self.saved_filename.as_ref().unwrap()); - Command::new("ffmpeg") + let output = Command::new("ffmpeg") .args([ "-i", video_filename.as_str(), @@ -268,9 +269,11 @@ impl Ffmpeg { "-y", ]) .output() - .unwrap(); + .expect("failed to execute ffmpeg"); - sleep(Duration::from_secs(1)); + println!("stat!: {:?}", output.status); + println!("out!: {:?}", String::from_utf8(output.stdout)); + println!("err!: {:?}", String::from_utf8(output.stderr)); // std::fs::remove_file(format!( // "{}.temp.audio", @@ -285,7 +288,6 @@ impl Ffmpeg { // .unwrap(); } } - // if only audio is recording then convert it to chosen format else if is_audio_record { self.progress_widget diff --git a/src/main.rs b/src/main.rs index 12bd5b9..44aafd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -402,8 +402,8 @@ pub fn build_ui(application: &Application) { follow_mouse: follow_mouse_switch, record_frames: frames_spin, command: command_entry, - video_process_id: None, - audio_process_id: None, + video_process: None, + audio_process: None, saved_filename: None, unbound: None, progress_widget: ProgressWidget::new(progress_dialog, progressbar),