Skip to content

Commit

Permalink
Allow change of output path
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Apr 28, 2023
1 parent 9e8635a commit 285fbb0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct AppConfig {
pub srt_options: SrtOptions,
pub render_options: RenderSettings,
pub app_update: AppUpdate,
pub out_path: String,
}

impl AppConfig {
Expand Down Expand Up @@ -47,6 +48,7 @@ impl From<&mut WalksnailOsdTool> for AppConfig {
srt_options: app_state.srt_options.clone(),
render_options: app_state.render_settings.clone(),
app_update: app_state.app_update.clone(),
out_path: app_state.out_path.as_ref().unwrap().to_string_lossy().to_string(),
}
}
}
7 changes: 7 additions & 0 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct WalksnailOsdTool {
pub osd_file: Option<osd::OsdFile>,
pub font_file: Option<font::FontFile>,
pub srt_file: Option<srt::SrtFile>,
pub out_path: Option<PathBuf>,
pub ui_dimensions: UiDimensions,
pub to_ffmpeg_sender: Option<Sender<ToFfmpegMessage>>,
pub from_ffmpeg_receiver: Option<Receiver<FromFfmpegMessage>>,
Expand Down Expand Up @@ -71,6 +72,10 @@ impl WalksnailOsdTool {
let config = AppConfig::load_or_create();
let srt_options = config.srt_options;
let osd_options = config.osd_options;
let render_settings = config.render_options;

// Load last used output path
let out_path = Some(PathBuf::from(config.out_path));

// Check for app updates
let promise = if config.app_update.check_on_startup {
Expand All @@ -93,6 +98,8 @@ impl WalksnailOsdTool {
srt_font: Some(srt_font),
osd_options,
srt_options,
render_settings,
out_path,
app_update,
..Default::default()
}
Expand Down
12 changes: 10 additions & 2 deletions src/ui/bottom_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ impl WalksnailOsdTool {
{
tracing::info!("Start render button clicked");
self.render_status.start_render();
if let (Some(video_path), Some(osd_file), Some(font_file), Some(video_info), Some(srt_file)) = (
if let (
Some(video_path),
Some(osd_file),
Some(font_file),
Some(video_info),
Some(srt_file),
Some(out_path),
) = (
&self.video_file,
&self.osd_file,
&self.font_file,
&self.video_info,
&self.srt_file,
&self.out_path,
) {
self.osd_options.osd_playback_speed_factor = if self.osd_options.adjust_playback_speed {
let video_duration = video_info.duration;
Expand All @@ -49,7 +57,7 @@ impl WalksnailOsdTool {
match start_video_render(
&self.dependencies.ffmpeg_path,
video_path,
&get_output_video_path(video_path),
&get_output_video_path(out_path, video_path),
osd_file.frames.clone(),
srt_file.frames.clone(),
font_file.clone(),
Expand Down
59 changes: 56 additions & 3 deletions src/ui/side_panel.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use egui::{CollapsingHeader, RichText, Ui};
use egui_extras::{Column, TableBuilder};

use super::{
utils::{format_minutes_seconds, separator_with_space},
WalksnailOsdTool,
};
use crate::ui::utils::get_output_video_path;
use egui::{CollapsingHeader, RichText, Ui};
use egui_extras::{Column, TableBuilder};

impl WalksnailOsdTool {
pub fn render_sidepanel(&mut self, ctx: &egui::Context) {
Expand All @@ -21,6 +21,8 @@ impl WalksnailOsdTool {
self.srt_info(ui);
separator_with_space(ui, 15.0);
self.font_info(ui);
separator_with_space(ui, 15.0);
self.out_info(ui);
});
});
}
Expand Down Expand Up @@ -250,6 +252,57 @@ impl WalksnailOsdTool {
});
}

fn out_info(&self, ui: &mut Ui) {
let out_loaded = self.out_path.is_some();

CollapsingHeader::new(RichText::new("Output").heading())
.icon(move |ui, opennes, response| circle_icon(ui, opennes, response, out_loaded))
.default_open(true)
.show(ui, |ui| {
ui.push_id("out_info", |ui| {
TableBuilder::new(ui)
.column(Column::exact(self.ui_dimensions.file_info_column1_width))
.column(
Column::remainder()
.at_least(self.ui_dimensions.file_info_column2_width)
.clip(true),
)
.body(|mut body| {
let row_height = self.ui_dimensions.file_info_row_height;
body.row(row_height, |mut row| {
row.col(|ui| {
ui.label("File name:");
});
row.col(|ui| {
if let (Some(out_path), Some(input_video_path)) = (&self.out_path, &self.video_file)
{
let out = &get_output_video_path(out_path, input_video_path);
let path = out.as_path().to_string_lossy();
let name: String = out.file_name().unwrap().to_string_lossy().into();
ui.hyperlink_to(name, path);
} else {
ui.label("-");
}
});
});
body.row(row_height, |mut row| {
row.col(|ui| {
ui.label("Path:");
});
row.col(|ui| {
if let Some(out_path) = &self.out_path {
let path = out_path.as_path().to_string_lossy();
ui.label(path);
} else {
ui.label("-");
}
});
});
});
});
});
}

fn font_info(&self, ui: &mut Ui) {
let font_file = self.font_file.as_ref();
let file_loaded = font_file.is_some();
Expand Down
18 changes: 18 additions & 0 deletions src/ui/top_panel.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Instant;

use egui::{vec2, Align2, Button, Frame, Label, RichText, Sense, Ui, Visuals, Window};

use super::WalksnailOsdTool;
Expand All @@ -8,6 +10,7 @@ impl WalksnailOsdTool {
ui.add_space(5.0);
ui.horizontal(|ui| {
self.import_files(ui, ctx);
self.output_path_button(ui);
self.reset_files(ui);
ui.add_space(ui.available_width() - 55.0);
self.toggle_light_dark_theme(ui, ctx);
Expand Down Expand Up @@ -58,6 +61,20 @@ impl WalksnailOsdTool {
});
}

fn output_path_button(&mut self, ui: &mut Ui) {
if ui
.add_enabled(self.render_status.is_not_in_progress(), Button::new("Output path"))
.clicked()
{
tracing::info!("Output file button clicked");
if let Some(file_handles) = rfd::FileDialog::new().add_filter("MP4 files", &["mp4"]).pick_folder() {
tracing::info!("Output {:?}", file_handles);
self.out_path = Some(file_handles);
self.config_changed = Some(Instant::now());
}
}
}

fn reset_files(&mut self, ui: &mut Ui) {
if ui
.add_enabled(self.render_status.is_not_in_progress(), Button::new("Reset files"))
Expand All @@ -68,6 +85,7 @@ impl WalksnailOsdTool {
self.osd_file = None;
self.font_file = None;
self.srt_file = None;
self.out_path = None;
self.osd_preview.texture_handle = None;
self.osd_preview.preview_frame = 1;
self.render_status.reset();
Expand Down
9 changes: 7 additions & 2 deletions src/ui/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ impl WalksnailOsdTool {
self.video_file = Some(video_file.clone());
self.video_info = VideoInfo::get(video_file, &self.dependencies.ffprobe_path).ok();

// If the output path is not set, set it to the same directory as the video file
if self.out_path.is_none() {
self.out_path = Some(video_file.parent().unwrap().to_path_buf());
}

// Try to load the matching OSD and SRT files
self.import_osd_file(&[matching_file_with_extension(video_file, "osd")]);
self.import_srt_file(&[matching_file_with_extension(video_file, "srt")]);
Expand Down Expand Up @@ -95,10 +100,10 @@ pub fn format_minutes_seconds(duration: &Duration) -> String {
format!("{}:{:0>2}", minutes, seconds)
}

pub fn get_output_video_path(input_video_path: &Path) -> PathBuf {
pub fn get_output_video_path(out_video_path: &Path, input_video_path: &Path) -> PathBuf {
let input_video_file_name = input_video_path.file_stem().unwrap().to_string_lossy();
let output_video_file_name = format!("{}_with_osd.mp4", input_video_file_name);
let mut output_video_path = input_video_path.parent().unwrap().to_path_buf();
let mut output_video_path = out_video_path.to_path_buf();
output_video_path.push(output_video_file_name);
output_video_path
}
Expand Down

0 comments on commit 285fbb0

Please sign in to comment.