1

I try to render a MediaComposition with the RenderToFileAsync method. The file is created but the file is empty (0Ko) and the "TranscodeFailureReason" is "Unknown". I'm looking for explanation online for hours and i can't find a solution. I started to think that it is an issue but i'm not sure. Thanks in advance,

EDIT: I try on another computer (i7-3610QM) and it works. My actual computer (i7-10510U) Any ideas?

MainPage.xaml.cs


namespace MediaCompositionTest
{

    public sealed partial class MainPage : Page
    {
        MediaComposition composition = new MediaComposition();
        public MainPage()
        {
            this.InitializeComponent();
            
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("click");
             FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.FileTypeFilter.Add(".mp4");
            openPicker.SuggestedStartLocation = PickerLocationId.Desktop;

            var files = await openPicker.PickMultipleFilesAsync();

            foreach (StorageFile item in files)
            {
                MediaClip clip = await MediaClip.CreateFromFileAsync(item);
                composition.Clips.Add(clip);
            }
            MediaStreamSource mediaStream = composition.GeneratePreviewMediaStreamSource((int)mainPlayer.Width, (int)mainPlayer.Height);
            mainPlayer.Source = MediaSource.CreateFromMediaStreamSource(mediaStream);
            mainPlayer.MediaPlayer.Play();

        }

        private async Task RenderCompositionToFile()
        {
            var picker = new Windows.Storage.Pickers.FileSavePicker();
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
            picker.FileTypeChoices.Add("MP4 files", new List<string>() { ".mp4" });
            picker.SuggestedFileName = "RenderedComposition.mp4";

            Windows.Storage.StorageFile file = await picker.PickSaveFileAsync();
            if (file != null)
            {
                // Call RenderToFileAsync
                var saveOperation = composition.RenderToFileAsync(file, MediaTrimmingPreference.Precise);

                saveOperation.Progress = new AsyncOperationProgressHandler<TranscodeFailureReason, double>(async (info, progress) =>
                {
                    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
                    {
                        ShowErrorMessage(string.Format("Saving file... Progress: {0:F0}%", progress));
                    }));
                });
                saveOperation.Completed = new AsyncOperationWithProgressCompletedHandler<TranscodeFailureReason, double>(async (info, status) =>
                {
                    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
                    {
                        try
                        {
                            var results = info.GetResults();
                            Debug.WriteLine(results);
                            if (results != TranscodeFailureReason.None || status != AsyncStatus.Completed)
                            {
                                ShowErrorMessage("Saving was unsuccessful");
                            }
                            else
                            {
                                ShowErrorMessage("Trimmed clip saved to file");
                            }
                        }
                        finally
                        {
                            // Update UI whether the operation succeeded or not
                        }

                    }));
                });
            }
            else
            {
                ShowErrorMessage("User cancelled the file selection");
            }
        }

        private void ShowErrorMessage(string v)
        {
            Debug.WriteLine(v);
        }

        private async void Save_Button_Click(object sender, RoutedEventArgs e)
        {
           await  RenderCompositionToFile();
        }

    }
}

  • I found the problem. On my laptop, there are 2 graphic cards. One intel UHD Graphics and the other is a GeForce MX250. MediaComposition.RenderToFileAsync use the video encode features of the graphic card. The GeForce MX250 doesn't support h264 encoding. I solved it by deactivate the Geforce and force to use the Intel UHD that support h264 encoding. Hope it helps. – Vincent ACHILLE Jun 26 '20 at 07:42

0 Answers0