@@ -278,15 +278,21 @@ 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 (codec_name .encode ())
289+ if codec_descriptor == cython .NULL :
290+ raise ValueError (f"Unknown data codec: { codec_name } " )
286291
287- # Assert that this format supports the requested codec
292+ # Verify format supports this codec
293+ codec_id = codec .id if codec != cython .NULL else codec_descriptor .id
288294 if not lib .avformat_query_codec (
289- self .ptr .oformat , codec . id , lib .FF_COMPLIANCE_NORMAL
295+ self .ptr .oformat , codec_id , lib .FF_COMPLIANCE_NORMAL
290296 ):
291297 raise ValueError (
292298 f"{ self .format .name !r} format does not support { codec_name !r} codec"
@@ -297,7 +303,7 @@ def add_data_stream(self, codec_name=None, options: dict | None = None):
297303 if stream == cython .NULL :
298304 raise MemoryError ("Could not allocate stream" )
299305
300- # Set up codec context if we have a codec
306+ # Set up codec context and parameters
301307 ctx : cython .pointer [lib .AVCodecContext ] = cython .NULL
302308 if codec != cython .NULL :
303309 ctx = lib .avcodec_alloc_context3 (codec )
@@ -311,8 +317,10 @@ def add_data_stream(self, codec_name=None, options: dict | None = None):
311317 # Initialize stream codec parameters
312318 err_check (lib .avcodec_parameters_from_context (stream .codecpar , ctx ))
313319 else :
314- # For raw data streams, just set the codec type
320+ # No codec available - set basic parameters for data stream
315321 stream .codecpar .codec_type = lib .AVMEDIA_TYPE_DATA
322+ if codec_descriptor != cython .NULL :
323+ stream .codecpar .codec_id = codec_descriptor .id
316324
317325 # Construct the user-land stream
318326 py_codec_context : CodecContext | None = None
0 commit comments