@@ -182,31 +182,52 @@ ov::Tensor extract_audio_from_video(const std::filesystem::path& video_path) {
182182
183183 // Build SwrContext: source format → 16 kHz mono f32
184184 SwrContext* swr = nullptr ;
185+ int swr_ret = -1 ;
186+
187+ #if LIBAVUTIL_VERSION_MAJOR >= 57
185188 AVChannelLayout in_layout{};
186189 AVChannelLayout out_layout{};
187190 av_channel_layout_default (&out_layout, 1 );
188191
189- #if LIBAVCODEC_VERSION_MAJOR >= 61
190- // FFmpeg 7 +: AVCodecContext exposes ch_layout instead of channel_layout/channels .
192+ #if LIBAVCODEC_VERSION_MAJOR >= 59
193+ // FFmpeg 5 +: AVCodecContext exposes ch_layout.
191194 if (av_channel_layout_copy (&in_layout, &dec_ctx->ch_layout ) < 0 ) {
192195 av_channel_layout_default (&in_layout, dec_ctx->ch_layout .nb_channels > 0 ? dec_ctx->ch_layout .nb_channels : 1 );
193196 }
194197#else
195- // FFmpeg <= 6: legacy channel layout stored as a bitmask + channels .
198+ // Compatibility path for older codec API with new channel layout type .
196199 uint64_t in_mask = dec_ctx->channel_layout
197- ? ( uint64_t ) dec_ctx->channel_layout
198- : ( uint64_t ) av_get_default_channel_layout (dec_ctx->channels );
200+ ? static_cast < uint64_t >( dec_ctx->channel_layout )
201+ : static_cast < uint64_t >( av_get_default_channel_layout (dec_ctx->channels ) );
199202 if (av_channel_layout_from_mask (&in_layout, in_mask) < 0 ) {
200203 av_channel_layout_default (&in_layout, dec_ctx->channels > 0 ? dec_ctx->channels : 1 );
201204 }
202205#endif
203206
204- int swr_ret = swr_alloc_set_opts2 (&swr,
205- &out_layout, AV_SAMPLE_FMT_FLT, 16000 ,
206- &in_layout, dec_ctx->sample_fmt , dec_ctx->sample_rate ,
207- 0 , nullptr );
207+ #if LIBSWRESAMPLE_VERSION_MAJOR >= 4
208+ swr_ret = swr_alloc_set_opts2 (&swr,
209+ &out_layout, AV_SAMPLE_FMT_FLT, 16000 ,
210+ &in_layout, dec_ctx->sample_fmt , dec_ctx->sample_rate ,
211+ 0 , nullptr );
212+ #else
213+ swr_ret = -1 ;
214+ #endif
215+
208216 av_channel_layout_uninit (&in_layout);
209217 av_channel_layout_uninit (&out_layout);
218+ #else
219+ // FFmpeg 4.x: legacy channel_layout/channel API + swr_alloc_set_opts.
220+ const int64_t out_layout = static_cast <int64_t >(av_get_default_channel_layout (1 ));
221+ const int64_t in_layout = dec_ctx->channel_layout
222+ ? static_cast <int64_t >(dec_ctx->channel_layout )
223+ : static_cast <int64_t >(av_get_default_channel_layout (dec_ctx->channels ));
224+ swr = swr_alloc_set_opts (nullptr ,
225+ out_layout, AV_SAMPLE_FMT_FLT, 16000 ,
226+ in_layout, dec_ctx->sample_fmt , dec_ctx->sample_rate ,
227+ 0 , nullptr );
228+ swr_ret = swr ? 0 : -1 ;
229+ #endif
230+
210231 if (swr_ret < 0 || !swr) {
211232 avcodec_free_context (&dec_ctx);
212233 avformat_close_input (&fmt_ctx);
0 commit comments