@@ -78,6 +78,19 @@ fn generate_concat_list(dir: &str, slides: &Vec<Slide>) -> String {
7878 lines. join ( "\n " )
7979}
8080
81+ fn set_default_ffmpeg_video_args ( cmd : & mut std:: process:: Command ) {
82+ cmd. arg ( "-c:v" )
83+ . arg ( "libx264" )
84+ . arg ( "-crf" )
85+ . arg ( "23" )
86+ . arg ( "-preset" )
87+ . arg ( "fast" )
88+ . arg ( "-vf" )
89+ . arg ( format ! ( "scale=-1:{HEIGHT},format=yuv420p" ) )
90+ . arg ( "-pix_fmt" )
91+ . arg ( "yuv420p" ) ;
92+ }
93+
8194fn write_concat_list ( dir : & str , path : & str , slides : & Vec < Slide > ) {
8295 let concat_list = generate_concat_list ( dir, slides) ;
8396 std:: fs:: write ( path, concat_list) . expect ( "couldn't write concat list" ) ;
@@ -101,34 +114,24 @@ fn create_video_clip(dir: &str, slide: &Slide, cache: bool, config: &TTSConfig,
101114 return ;
102115 }
103116 let output_video = crate :: path:: video_path ( dir, slide) ;
104- let output = std:: process:: Command :: new ( "ffmpeg" )
105- . arg ( "-y" )
117+ let mut cmd = std:: process:: Command :: new ( "ffmpeg" ) ;
118+ cmd . arg ( "-y" )
106119 . arg ( "-loop" )
107120 . arg ( "1" )
108121 . arg ( "-i" )
109122 . arg ( input_image)
110123 . arg ( "-i" )
111- . arg ( input_audio)
112- . arg ( "-c:v" )
113- . arg ( "libx264" )
114- . arg ( "-crf" )
115- . arg ( "23" )
116- . arg ( "-preset" )
117- . arg ( "fast" )
118- . arg ( "-vf" )
119- . arg ( format ! ( "scale=-1:{HEIGHT},format=yuv420p" ) )
120- . arg ( "-pix_fmt" )
121- . arg ( "yuv420p" )
122- . arg ( "-c:a" )
124+ . arg ( input_audio) ;
125+ set_default_ffmpeg_video_args ( & mut cmd) ;
126+ cmd. arg ( "-c:a" )
123127 . arg ( "opus" )
124128 . arg ( "-strict" )
125129 . arg ( "experimental" )
126130 . arg ( "-shortest" )
127131 . arg ( "-tune" )
128132 . arg ( "stillimage" )
129- . arg ( output_video. clone ( ) )
130- . output ( )
131- . expect ( "Failed to run ffmpeg command" ) ;
133+ . arg ( output_video. clone ( ) ) ;
134+ let output = cmd. output ( ) . expect ( "Failed to run ffmpeg command" ) ;
132135 if !output. status . success ( ) {
133136 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
134137 tracing:: error!( "Failed to create video clip: {stderr}" ) ;
@@ -139,7 +142,7 @@ fn create_video_clip(dir: &str, slide: &Slide, cache: bool, config: &TTSConfig,
139142 }
140143}
141144
142- fn create_video_clips (
145+ pub ( crate ) fn create_video_clips (
143146 dir : & str ,
144147 slides : & Vec < Slide > ,
145148 cache : bool ,
@@ -159,7 +162,13 @@ fn create_video_clips(
159162 }
160163}
161164
162- fn concat_video_clips ( concat_list : & str , output_path : & str ) {
165+ pub ( crate ) fn combine_video ( dir : & str , slides : & Vec < Slide > , output : & str , audio_codec : & str ) {
166+ let output = Path :: new ( dir) . join ( output) ;
167+ let output_path = output. to_str ( ) . unwrap ( ) ;
168+ let concat_list = Path :: new ( dir) . join ( "concat_list.txt" ) ;
169+ let concat_list = concat_list. to_str ( ) . unwrap ( ) ;
170+ write_concat_list ( dir, concat_list, slides) ;
171+
163172 let output = std:: process:: Command :: new ( "ffmpeg" )
164173 . arg ( "-y" )
165174 . arg ( "-f" )
@@ -168,65 +177,18 @@ fn concat_video_clips(concat_list: &str, output_path: &str) {
168177 . arg ( concat_list)
169178 . arg ( "-c" )
170179 . arg ( "copy" )
171- . arg ( output_path)
172- . output ( )
173- . expect ( "Failed to run ffmpeg command" ) ;
174- if !output. status . success ( ) {
175- let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
176- tracing:: error!( "Failed to concat video clips: {stderr}" ) ;
177- std:: process:: exit ( 1 ) ;
178- } else {
179- tracing:: info!( "Concatenated video clips into {output_path}" ) ;
180- }
181- }
182-
183- pub fn generate_video (
184- dir : & str ,
185- slides : & Vec < Slide > ,
186- cache : bool ,
187- config : & TTSConfig ,
188- output : & str ,
189- audio_ext : & str ,
190- ) {
191- create_video_clips ( dir, slides, cache, config, audio_ext) ;
192- let output = Path :: new ( dir) . join ( output) ;
193- let output = output. to_str ( ) . unwrap ( ) ;
194- let concat_list = Path :: new ( dir) . join ( "concat_list.txt" ) ;
195- let concat_list = concat_list. to_str ( ) . unwrap ( ) ;
196- write_concat_list ( dir, concat_list, slides) ;
197- concat_video_clips ( concat_list, output) ;
198- }
199-
200- pub fn generate_release_video ( dir : & str , input : & str , output : & str , audio_codec : & str ) {
201- let input_path = Path :: new ( dir) . join ( input) ;
202- let output_path = Path :: new ( dir) . join ( output) ;
203- let output_path = output_path. to_str ( ) . unwrap ( ) ;
204- let mut cmd = std:: process:: Command :: new ( "ffmpeg" ) ;
205- let output = cmd
206- . arg ( "-y" )
207- . arg ( "-i" )
208- . arg ( input_path)
209- . arg ( "-c:v" )
210- . arg ( "libx264" )
211- . arg ( "-crf" )
212- . arg ( "23" )
213- . arg ( "-preset" )
214- . arg ( "fast" )
215- . arg ( "-vf" )
216- . arg ( format ! ( "scale=-1:{HEIGHT},format=yuv420p" ) )
217180 . arg ( "-c:a" )
218181 . arg ( audio_codec)
219182 . arg ( "-strict" )
220183 . arg ( "experimental" )
221184 . arg ( output_path)
222185 . output ( )
223186 . expect ( "Failed to run ffmpeg command" ) ;
224-
225187 if !output. status . success ( ) {
226188 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
227- tracing:: error!( "Failed to create release video: {stderr}" ) ;
189+ tracing:: error!( "Failed to concat video clips : {stderr}" ) ;
228190 std:: process:: exit ( 1 ) ;
229191 } else {
230- tracing:: info!( "Created release video {}" , output_path ) ;
192+ tracing:: info!( "Combined video clips into {output_path}" ) ;
231193 }
232194}
0 commit comments