@@ -278,15 +278,23 @@ def add_data_stream(self, codec_name=None, options: dict | None = None):
278278 :rtype: The new :class:`~av.data.stream.DataStream`.
279279 """
280280 codec : cython .pointer [cython .const [lib .AVCodec ]] = cython .NULL
281+ codec_descriptor : cython .pointer [lib .AVCodecDescriptor ] = cython .NULL
281282
282283 if codec_name is not None :
283284 codec = lib .avcodec_find_encoder_by_name (codec_name .encode ())
284285 if codec == cython .NULL :
285- raise ValueError (f"Unknown data codec: { codec_name } " )
286+ codec = lib .avcodec_find_decoder_by_name (codec_name .encode ())
287+ if codec == cython .NULL :
288+ codec_descriptor = lib .avcodec_descriptor_get_by_name (
289+ codec_name .encode ()
290+ )
291+ if codec_descriptor == cython .NULL :
292+ raise ValueError (f"Unknown data codec: { codec_name } " )
286293
287- # Assert that this format supports the requested codec
294+ # Verify format supports this codec
295+ codec_id = codec .id if codec != cython .NULL else codec_descriptor .id
288296 if not lib .avformat_query_codec (
289- self .ptr .oformat , codec . id , lib .FF_COMPLIANCE_NORMAL
297+ self .ptr .oformat , codec_id , lib .FF_COMPLIANCE_NORMAL
290298 ):
291299 raise ValueError (
292300 f"{ self .format .name !r} format does not support { codec_name !r} codec"
@@ -297,7 +305,7 @@ def add_data_stream(self, codec_name=None, options: dict | None = None):
297305 if stream == cython .NULL :
298306 raise MemoryError ("Could not allocate stream" )
299307
300- # Set up codec context if we have a codec
308+ # Set up codec context and parameters
301309 ctx : cython .pointer [lib .AVCodecContext ] = cython .NULL
302310 if codec != cython .NULL :
303311 ctx = lib .avcodec_alloc_context3 (codec )
@@ -311,8 +319,10 @@ def add_data_stream(self, codec_name=None, options: dict | None = None):
311319 # Initialize stream codec parameters
312320 err_check (lib .avcodec_parameters_from_context (stream .codecpar , ctx ))
313321 else :
314- # For raw data streams, just set the codec type
322+ # No codec available - set basic parameters for data stream
315323 stream .codecpar .codec_type = lib .AVMEDIA_TYPE_DATA
324+ if codec_descriptor != cython .NULL :
325+ stream .codecpar .codec_id = codec_descriptor .id
316326
317327 # Construct the user-land stream
318328 py_codec_context : CodecContext | None = None
0 commit comments