Detecting if an audio stream is found in input file in order to skip applying the audiocodec

Most of the videos we encode using Bitmovin have an audio track. We are using the API to start an encoding using three predefined configured video codecs and one audio codec. However, some videos don’t have an audio “track”, causing the encoding to fail with this error: “Setting up encoding configuration failed! No audio stream found in input file …”.

How can we “detect” this and skip the generation of the audio stream/muxing in this case?

Hi Maarten!

When creating a Stream with an e.g. AacAudioConfiguration , you can add a condition to it that needs to be met, so any other resources that rely on it (e.g. Mp4Muxings) are processed or not. We don’t have a public sample for that in particular for dotnet, but please have a look at this code snippet we created based on our FixedBitrateLadder example for our dotnet SDK.

We’ve modified the “CreateStream” method to add a Condition to the Stream, in case an AacAudioConfiguration is provided. AUDIOSTREAMCOUNT checks the total number of audio tracks available in the input file. If it is greater than zero, the condition is met, and subsequent resources will be processed instead of being ignored based on their streamConditionMode . There are also other input file details you can use for that as well as logically group multiple conditions. All that gets explained in more detail in our documentation about Stream Conditions.

FixedBitrateLadder.cs at line 245:

private Task<Stream> CreateStream(Models.Encoding encoding, Input input, string inputPath,
            CodecConfiguration configuration)
        {
            var streamInput = new StreamInput()
            {
                InputId = input.Id,
                InputPath = inputPath,
                SelectionMode = StreamSelectionMode.AUTO
            };

            var stream = new Stream()
            {
                InputStreams = new List<StreamInput>() { streamInput },
                CodecConfigId = configuration.Id
            };

            //Adds a condition to AAC Audio Streams to process them only, if there is an audio track available in the input file
            if (configuration.GetType() == typeof(AacAudioConfiguration))
            {
                var audioTrackExistsCondition = new Condition()
                {
                    Attribute = "AUDIOSTREAMCOUNT",
                    Operator = ConditionOperator.GREATER_THAN,
                    Value = "0"
                };
                stream.Conditions = audioTrackExistsCondition;
            }
            return _bitmovinApi.Encoding.Encodings.Streams.CreateAsync(encoding.Id, stream);
        }

As you are creating MP4 files where video and audio streams get merged, you need to set an additional configuration to your MP4 Muxing which is called streamConditionsMode (API Reference - Create MP4 Muxing). It can either be set to “DROP MUXING” (default) or “DROP STREAM”. Set it to “DROP STREAM” so the MP4 Muxing takes Streams into account, that met their condition.

FixedBitrateLadder.cs at line 321:

private Task CreateMp4Muxing(Models.Encoding encoding, Output output, string outputPath,
            List<Stream> streams, string fileName)
        {
            var muxing = new Mp4Muxing()
            {
                Outputs = new List<EncodingOutput>() {BuildEncodingOutput(output, outputPath)},
                Filename = fileName,
                StreamConditionsMode = StreamConditionsMode.DROP_STREAM 
            };

            foreach (var stream in streams)
            {
                var muxingSteam = new MuxingStream()
                {
                    StreamId = stream.Id,
                };
                muxing.Streams.Add(muxingSteam);
            }
            return _bitmovinApi.Encoding.Encodings.Muxings.Mp4.CreateAsync(encoding.Id, muxing);
        }

By doing that, your encoding configuration knows how to handle input files with, and without, an audio track :slight_smile:

Some additional information on stream conditions can also be found here.

I hope this is understandable and helpful. In case you have any further questions, please let me know :+1:

Best regards,
Yuliia

Customer Success Manager,
Bitmovin, Inc

1 Like

Hi Yuliia, works like a charm!