MediaStreamSource is not applying Bitrate Assigned to Media Encoding Profile

Abhishek sharma 136 Reputation points
2020-12-24T05:13:20.907+00:00

I am Using Media Composition To Preview Video and to Generate Stream I pass a MediaEncoding Profile wherein video Container I am assigning the bitrate that has to Apply when previewing but it's not working.

        MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);
        profile.Video.Bitrate = 1000000; // Any  Bitrate
        var stream = VideoComposition.GenerateMediaStreamSource(profile);
        videoPlayer.SetMediaStreamSource(stream);
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,002 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-12-25T06:12:32.18+00:00

    Hello,

    Welcome to Microsoft Q&A.

    By testing, there is no effect in the MediaStreamSource when we assign MediaEncodingProfile.Video.Bitrate in your scenario.

    You could use Windows.Media.Transcoding APIs to transcode video files from one format to another to show the effect of assigning bitrate.

    Please check the following code as a sample:

    private MediaComposition VideoComposition;  
    private MediaStreamSource mediaStreamSource;  
    private StorageFile destination;  
    ……  
    private async void button_Click(object sender, RoutedEventArgs e)  
    {  
        var picker = new Windows.Storage.Pickers.FileOpenPicker();  
        picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;  
        picker.FileTypeFilter.Add(".mp4");  
        Windows.Storage.StorageFile pickedFile = await picker.PickSingleFileAsync();  
        if (pickedFile == null)  
        {  
            return;  
        }  
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;  
        destination = await localFolder.CreateFileAsync("destination.mp4",CreationCollisionOption.ReplaceExisting);  
        MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);  
    
        MediaTranscoder transcoder = new MediaTranscoder();  
        profile.Video.Bitrate = 50000; // Any  Bitrate  
        PrepareTranscodeResult prepareOp = await  
            transcoder.PrepareFileTranscodeAsync(pickedFile, destination, profile);  
    
        if (prepareOp.CanTranscode)  
        {  
            var transcodeOp = prepareOp.TranscodeAsync();  
            transcodeOp.Completed +=  
    new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);  
        }  
        else  
        {  
            switch (prepareOp.FailureReason)  
            {  
                case TranscodeFailureReason.CodecNotFound:  
                    System.Diagnostics.Debug.WriteLine("Codec not found.");  
                    break;  
                case TranscodeFailureReason.InvalidProfile:  
                    System.Diagnostics.Debug.WriteLine("Invalid profile.");  
                    break;  
                default:  
                    System.Diagnostics.Debug.WriteLine("Unknown failure.");  
                    break;  
            }  
        }  
    }  
    
    private async void TranscodeComplete(IAsyncActionWithProgress<double> asyncInfo, AsyncStatus asyncStatus)  
    {  
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {  
            var stream = await destination.OpenAsync(Windows.Storage.FileAccessMode.Read);  
            videoPlayer.SetSource(stream, destination.ContentType);  
        });  
    }  
    

    You could delete the destination file if you do not want to save the file.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Abhishek sharma 136 Reputation points
    2020-12-25T07:16:11.313+00:00

    but it will take time to provide video since it will transcode first and then I get to play the file for the preview. my sole purpose for this question is to preview the file with user-provided bitrate input we have output already. because it's quite hard to choose the right bitrate, that's why we are thinking about providing a preview but it looks like this might be near to impossible with existing APIs. if you have something then point me in the right direction, it will be helpful I have tried most of the thing i.e. effect, bitrate in encoding profile, etc nothing works thanks for your help


  3. Abhishek sharma 136 Reputation points
    2020-12-25T09:25:26.573+00:00

    no worry, we discussed with the team, so now we are providing the app without a preview but we will notify the user by using messages.
    thanks for your help.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.