@@ -81,18 +81,6 @@ pub fn is_ffmpeg_installed() -> bool {
8181/// Err(e) => eprintln!("Error: {}", e),
8282/// }
8383/// ```
84- // ffmpeg \
85- // -i input.h264 \
86- // -i input.wav \
87- // -i speaker.wav \
88- // -c:v copy \
89- // -c:a aac \
90- // -b:a:0 128k \
91- // -b:a:1 128k \
92- // -map 0:v \
93- // -map 1:a \
94- // -map 2:a \
95- // -y output.mp4
9684pub fn merge_tracks (
9785 config : MergeTracksConfig ,
9886 mut progress_cb : impl FnMut ( f32 ) ,
@@ -131,31 +119,49 @@ pub fn merge_tracks(
131119
132120 cmd. args ( & [ "-c:v" , "copy" ] ) ;
133121
134- if config. input_wav_path . is_some ( ) || config. speaker_wav_path . is_some ( ) {
135- cmd. args ( & [ "-c:a" , "aac" ] ) ;
136- }
137-
138- if config. input_wav_path . is_some ( ) {
139- cmd. args ( & [ "-b:a:0" , "128k" ] ) ;
140- }
141-
142- if config. speaker_wav_path . is_some ( ) {
143- cmd. args ( & [ "-b:a:1" , "128k" ] ) ;
144- }
145-
146- cmd. args ( & [ "-map" , "0:v" ] ) ;
147-
148- if config. input_wav_path . is_some ( ) {
149- cmd. args ( & [ "-map" , "1:a" ] ) ;
150- }
151-
152- if config. speaker_wav_path . is_some ( ) {
153- cmd. args ( & [ "-map" , "2:a" ] ) ;
122+ if config. input_wav_path . is_some ( ) && config. speaker_wav_path . is_some ( ) {
123+ cmd. args ( & [
124+ "-filter_complex" ,
125+ if config. convert_input_wav_to_mono {
126+ "[1:a]pan=mono|c0=0.5*FL+0.5*FR[a1];[a1][2:a]amix=inputs=2:duration=longest[a]"
127+ } else {
128+ "[1:a][2:a]amix=inputs=2:duration=longest[a]"
129+ } ,
130+ "-map" ,
131+ "0:v" ,
132+ "-map" ,
133+ "[a]" ,
134+ "-c:a" ,
135+ "aac" ,
136+ "-b:a" ,
137+ "128k" ,
138+ ] ) ;
139+ } else if config. input_wav_path . is_some ( ) {
140+ if config. convert_input_wav_to_mono {
141+ cmd. args ( & [
142+ "-filter_complex" ,
143+ "[1:a]pan=mono|c0=0.5*FL+0.5*FR[a]" ,
144+ "-map" ,
145+ "0:v" ,
146+ "-map" ,
147+ "[a]" ,
148+ "-c:a" ,
149+ "aac" ,
150+ "-b:a" ,
151+ "128k" ,
152+ ] ) ;
153+ } else {
154+ cmd. args ( & [ "-map" , "0:v" , "-map" , "1:a" , "-c:a" , "aac" , "-b:a" , "128k" ] ) ;
155+ }
156+ } else if config. speaker_wav_path . is_some ( ) {
157+ cmd. args ( & [ "-map" , "0:v" , "-map" , "1:a" , "-c:a" , "aac" , "-b:a" , "128k" ] ) ;
158+ } else {
159+ unimplemented ! ( ) ;
154160 }
155161
156162 let mut child_process = cmd
157- . output ( config. output_path . display ( ) . to_string ( ) )
158163 . overwrite ( )
164+ . output ( config. output_path . display ( ) . to_string ( ) )
159165 . print_command ( )
160166 . spawn ( )
161167 . map_err ( |e| RecorderError :: Ffmpeg ( format ! ( "ffmpeg spawn child process failed. {e}" ) ) ) ?;
0 commit comments