@@ -57,45 +57,54 @@ impl Encoder {
5757 } )
5858 }
5959
60- fn scale_filter ( & self ) -> Option < [ String ; 2 ] > {
61- match ( self . scaled_width , self . scaled_height ) {
62- ( Some ( width) , None ) => Some ( [ "-vf" . to_string ( ) , format ! ( "scale={}:-2" , width) ] ) ,
63- ( None , Some ( height) ) => Some ( [ "-vf" . to_string ( ) , format ! ( "scale=-2:{}" , height) ] ) ,
64- _ => None ,
65- }
66- }
67-
68- // ffmpeg -hide_banner -v error -progress pipe:2 -i input.mp4 -c:v libx265 -x265-params log-level=error:output-depth=10:crf=20 -pix_fmt -preset medium yuv420p10le -filter:v fps=24 -f mp4 -c:a copy output.mp4
60+ // ffmpeg -hide_banner -v error -progress pipe:2 -i input.mp4 -c:v libx265 -x265-params log-level=error:output-depth=10:crf=20 -pix_fmt yuv420p10le -preset medium -vf scale=1280:-2,fps=24 -f mp4 -c:a copy output.mp4
6961 pub ( crate ) fn build_ffmpeg_args ( & self ) -> EncodeResult < Vec < String > > {
7062 let mut args: Vec < String > = Vec :: new ( ) ;
63+
7164 args. extend (
72- [ "-hide_banner" , "-v" , "error" , "-progress" , "pipe:2" , "-i" ] . map ( |s| s. to_string ( ) ) ,
65+ [ "-hide_banner" , "-v" , "error" , "-progress" , "pipe:2" , "-i" ]
66+ . iter ( )
67+ . map ( |& s| s. to_string ( ) ) ,
7368 ) ;
7469
75- args. push ( self . input . to_string_lossy ( ) . to_string ( ) ) ;
76-
77- args. extend ( [ "-c:v" , "libx265" , "-x265-params" ] . map ( |s| s. to_string ( ) ) ) ;
70+ args. push ( self . input . to_string_lossy ( ) . into_owned ( ) ) ;
71+ args. extend (
72+ [ "-c:v" , "libx265" , "-x265-params" ]
73+ . iter ( )
74+ . map ( |& s| s. to_string ( ) ) ,
75+ ) ;
7876
7977 args. push ( format ! ( "log-level=error:output-depth=10:crf={}" , self . crf) ) ;
8078
8179 args. push ( "-preset" . to_string ( ) ) ;
8280
8381 args. push ( format ! ( "{}" , self . preset) ) ;
8482
85- args. extend ( [ "-pix_fmt" , "yuv420p10le" ] . map ( |s| s. to_string ( ) ) ) ;
83+ args. extend ( [ "-pix_fmt" , "yuv420p10le" ] . iter ( ) . map ( |& s| s. to_string ( ) ) ) ;
8684
87- if let Some ( filter) = self . scale_filter ( ) {
88- args. extend ( filter) ;
89- }
90-
91- if let Some ( fps) = self . fps {
92- args. push ( "-filter:v" . to_string ( ) ) ;
93- args. push ( format ! ( "fps={}" , fps) ) ;
85+ match ( self . scaled_width , self . scaled_height , self . fps ) {
86+ ( Some ( width) , None , Some ( fps) ) => {
87+ args. push ( "-vf" . to_string ( ) ) ;
88+ args. push ( format ! ( "scale={}:-2,fps={}" , width, fps) ) ;
89+ }
90+ ( Some ( width) , None , None ) => {
91+ args. push ( "-vf" . to_string ( ) ) ;
92+ args. push ( format ! ( "scale={}:-2" , width) ) ;
93+ }
94+ ( None , Some ( height) , Some ( fps) ) => {
95+ args. push ( "-vf" . to_string ( ) ) ;
96+ args. push ( format ! ( "scale=-2:{},fps={}" , height, fps) ) ;
97+ }
98+ ( None , Some ( height) , None ) => {
99+ args. push ( "-vf" . to_string ( ) ) ;
100+ args. push ( format ! ( "scale=-2:{}" , height) ) ;
101+ }
102+ _ => ( ) ,
94103 }
95104
96- args. extend ( [ "-f" , "mp4" , "-c:a" , "copy" ] . map ( |s| s. to_string ( ) ) ) ;
105+ args. extend ( [ "-f" , "mp4" , "-c:a" , "copy" ] . iter ( ) . map ( |& s| s. to_string ( ) ) ) ;
97106
98- args. push ( self . output ( ) ?. to_string_lossy ( ) . to_string ( ) ) ;
107+ args. push ( self . output ( ) ?. to_string_lossy ( ) . into_owned ( ) ) ;
99108
100109 Ok ( args)
101110 }
@@ -189,9 +198,12 @@ mod test {
189198 assert_eq ! ( encoder. scaled_width, Some ( config. resolution. width( ) ) ) ;
190199 assert_eq ! ( encoder. scaled_height, None ) ;
191200 let args = encoder. build_ffmpeg_args ( ) ?. join ( " " ) ;
192- assert ! ( args. contains( & format!( "fps={}" , config. fps) ) ) ;
193201 assert ! ( args. contains( & "crf=19" . to_string( ) ) ) ;
194- assert ! ( args. contains( & format!( "scale={}:-2" , config. resolution. width( ) ) ) ) ;
202+ assert ! ( args. contains( & format!(
203+ "-vf scale={}:-2,fps={}" ,
204+ config. resolution. width( ) ,
205+ config. fps
206+ ) ) ) ;
195207
196208 // 竖屏
197209 let config = Config {
@@ -204,7 +216,7 @@ mod test {
204216 assert_eq ! ( encoder. scaled_width, None ) ;
205217 assert_eq ! ( encoder. scaled_height, Some ( config. resolution( ) . height( ) ) ) ;
206218 let args = encoder. build_ffmpeg_args ( ) ?. join ( " " ) ;
207- assert ! ( args. contains( & format!( "scale=-2:{}" , config. resolution. height( ) ) ) ) ;
219+ assert ! ( args. contains( & format!( "-vf scale=-2:{}" , config. resolution. height( ) ) ) ) ;
208220 Ok ( ( ) )
209221 }
210222
@@ -223,7 +235,6 @@ mod test {
223235 assert_eq ! ( encoder. scaled_width, None ) ;
224236 assert_eq ! ( encoder. scaled_height, None ) ;
225237 let args = encoder. build_ffmpeg_args ( ) ?. join ( " " ) ;
226- assert ! ( !args. contains( & "fps=" . to_string( ) ) ) ;
227238 assert ! ( args. contains( & "crf=20" . to_string( ) ) ) ;
228239 assert ! ( !args. contains( & "-vf" . to_string( ) ) ) ;
229240
0 commit comments