Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically rename existing mp4 files under register_output.mp4.path location to [path].old.[index] #684

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### ✨ New features

- Add `overwrite` option for MP4 output ([#684](https://github.com/software-mansion/live-compositor/pull/684) by [@WojciechBarczynski](https://github.com/WojciechBarczynski))

### 🐛 Bug fixes

## [v0.3.0](https://github.com/software-mansion/live-compositor/releases/tag/v0.3.0)
Expand Down
8 changes: 7 additions & 1 deletion compositor_api/src/types/from_register_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ impl TryFrom<Mp4Output> for pipeline::RegisterOutputOptions<output::OutputOption
type Error = TypeError;

fn try_from(request: Mp4Output) -> Result<Self, Self::Error> {
let Mp4Output { path, video, audio } = request;
let Mp4Output {
path,
video,
audio,
overwrite,
} = request;

if video.is_none() && audio.is_none() {
return Err(TypeError::new(
Expand Down Expand Up @@ -158,6 +163,7 @@ impl TryFrom<Mp4Output> for pipeline::RegisterOutputOptions<output::OutputOption
let output_options = output::OutputOptions {
output_protocol: output::OutputProtocolOptions::Mp4(Mp4OutputOptions {
output_path: path.into(),
overwrite: overwrite.unwrap_or(false),
video: mp4_video,
audio: mp4_audio,
}),
Expand Down
2 changes: 2 additions & 0 deletions compositor_api/src/types/register_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Mp4Output {
pub video: Option<OutputVideoOptions>,
/// Audio track configuration.
pub audio: Option<OutputMp4AudioOptions>,
/// (**default=`false`**) Specifies file under `path` should be overwritten if present.
pub overwrite: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
Expand Down
5 changes: 5 additions & 0 deletions compositor_pipeline/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io;

use compositor_render::{
error::{
InitRendererEngineError, RegisterError, RegisterRendererError, RequestKeyframeError,
Expand Down Expand Up @@ -75,6 +77,9 @@ pub enum OutputInitError {
#[error("Failed to register output. All ports in range {lower_bound} to {upper_bound} are already used or not available.")]
AllPortsAlreadyInUse { lower_bound: u16, upper_bound: u16 },

#[error("Failed to overwrite existing output file. Error: {0}.")]
Mp4OverwriteError(io::Error),

#[error("Path {path} already exist. Can't create a new mp4 file under that path.")]
Mp4PathExist { path: String },

Expand Down
15 changes: 10 additions & 5 deletions compositor_pipeline/src/pipeline/output/mp4.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, ptr};
use std::{fs, path::PathBuf, ptr};

use compositor_render::{event_handler::emit_event, OutputId};
use crossbeam_channel::Receiver;
Expand All @@ -16,6 +16,7 @@ use crate::{
#[derive(Debug, Clone)]
pub struct Mp4OutputOptions {
pub output_path: PathBuf,
pub overwrite: bool,
pub video: Option<Mp4VideoTrack>,
pub audio: Option<Mp4AudioTrack>,
}
Expand Down Expand Up @@ -50,10 +51,14 @@ impl Mp4FileWriter {
packets_receiver: Receiver<EncoderOutputEvent>,
sample_rate: u32,
) -> Result<Self, OutputInitError> {
if options.output_path.exists() {
return Err(OutputInitError::Mp4PathExist {
path: options.output_path.to_string_lossy().into_owned(),
});
if options.output_path.exists() && !options.overwrite {
match options.overwrite {
true => fs::remove_file(options.output_path.clone())
.map_err(OutputInitError::Mp4OverwriteError),
false => Err(OutputInitError::Mp4PathExist {
path: options.output_path.to_string_lossy().into_owned(),
}),
}?
}

let (output_ctx, video_stream, audio_stream) = init_ffmpeg_output(options, sample_rate)?;
Expand Down
1 change: 1 addition & 0 deletions integration_tests/examples/mp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn client_code() -> Result<()> {
&json!({
"type": "mp4",
"path": "output.mp4",
"overwrite": true,
"video": {
"resolution": {
"width": VIDEO_RESOLUTION.width,
Expand Down