@@ -13,6 +13,7 @@ typedef struct StreamingParams {
1313 char copy_video ;
1414 char copy_audio ;
1515 char fragmented_mp4 ;
16+ char * video_codec ;
1617} StreamingParams ;
1718
1819typedef struct StreamingContext {
@@ -71,17 +72,23 @@ int prepare_decoder(StreamingContext *sc) {
7172 return 0 ;
7273}
7374
74- int prepare_encoder (StreamingContext * sc , AVCodecContext * decoder_ctx , AVRational input_framerate ) {
75+ int prepare_encoder (StreamingContext * sc , AVCodecContext * decoder_ctx , AVRational input_framerate , StreamingParams sp ) {
7576 sc -> video_avs = avformat_new_stream (sc -> avfc , NULL );
7677
77- sc -> video_avc = avcodec_find_encoder_by_name ("libx264" );
78+ char * codec_name = strcmp (sp .video_codec , "x264" ) == 0 ? "libx264" : "libx265" ;
79+ char * x264_opts = "keyint=60:min-keyint=60:scenecut=0:force-cfr=1" ;
80+ char * x265_opts = "keyint=60:min-keyint=60:scenecut=0" ;
81+ char * codec_priv_key = strcmp (sp .video_codec , "x264" ) == 0 ? "x264-params" : "x265-params" ;
82+ char * codec_priv_value = strcmp (sp .video_codec , "x264" ) == 0 ? x264_opts : x265_opts ;
83+
84+ sc -> video_avc = avcodec_find_encoder_by_name (codec_name );
7885 if (!sc -> video_avc ) {logging ("could not find the proper codec" ); return -1 ;}
7986
8087 sc -> video_avcc = avcodec_alloc_context3 (sc -> video_avc );
8188 if (!sc -> video_avcc ) {logging ("could not allocated memory for codec context" ); return -1 ;}
8289
8390 av_opt_set (sc -> video_avcc -> priv_data , "preset" , "fast" , 0 );
84- av_opt_set (sc -> video_avcc -> priv_data , "x264-params" , "keyint=60:min-keyint=60:scenecut=-1:force-cfr=1" , 0 );
91+ av_opt_set (sc -> video_avcc -> priv_data , codec_priv_key , codec_priv_value , 0 );
8592
8693 sc -> video_avcc -> height = decoder_ctx -> height ;
8794 sc -> video_avcc -> width = decoder_ctx -> width ;
@@ -169,6 +176,7 @@ int main(int argc, char *argv[])
169176 sp .copy_audio = 1 ;
170177 sp .copy_video = 0 ;
171178 sp .fragmented_mp4 = 0 ;
179+ sp .video_codec = "x264" ;
172180
173181 StreamingContext * decoder = (StreamingContext * ) calloc (1 , sizeof (StreamingContext ));
174182 decoder -> filename = argv [1 ];
@@ -184,7 +192,7 @@ int main(int argc, char *argv[])
184192
185193 if (!sp .copy_video ) {
186194 AVRational input_framerate = av_guess_frame_rate (decoder -> avfc , decoder -> video_avs , NULL );
187- prepare_encoder (encoder , decoder -> video_avcc , input_framerate );
195+ prepare_encoder (encoder , decoder -> video_avcc , input_framerate , sp );
188196 } else {
189197 if (prepare_copy (encoder -> avfc , & encoder -> video_avs , decoder -> video_avs -> codecpar )) {return -1 ;}
190198 }
0 commit comments