个人资料

跳过导航链接首页 > 博客列表 > 博客正文

UWP获取视频中的音频,另存为mp3

分类:

使用audiograph api 可以播放视频中的音频,同时使用文件输出节点,就可以获取视频中的音频文件了.

关于audiograph 参考:一图理解UWP提供的音频api:AudioGraph

该功能已加入爱剪辑UWP

代码示例:

    public partial class TransferPage
    {
        public async void TranscodeAudio()
        {


            try
            {
                if (VideoFile == null)
                {
                    MainPage._MainPageInAppNotification.Show(AppResources.GetString("先添加视频"), 3000);
                    return;
                }
                // check folder
                await App.CheckOutput();

                StorageFile save_file = await MainPage.OutputFolder.CreateFileAsync(VideoFile.DisplayName + ".mp3", CreationCollisionOption.ReplaceExisting);





                await CreateAudioGraph();

                await CreateFileInputNode(VideoFile);

                await CreateDeviceOutput();
                //CreateFrameOutput();
                await CreateFileOutput(save_file);


                _fileInputNode.AddOutgoingConnection(_deviceOutputNode);// not nessorary
                _fileInputNode.AddOutgoingConnection(_fileOutputNode);

                //
                _audioGraph.QuantumProcessed += async (a, b) => {



                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        progress_bar.Value = _fileInputNode.Position.TotalSeconds / _fileInputNode.Duration.TotalSeconds * 100;



                    });
                    //Debug.WriteLine(_fileInputNode.Position.TotalSeconds/ _fileInputNode.Duration.TotalSeconds);
                };

                _fileInputNode.FileCompleted += async (a, b) => {

                    _audioGraph.Stop();

                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        var MyFolderLauncherOptions = new FolderLauncherOptions();
                        //StorageFolder folder = ApplicationData.Current.LocalFolder;
                        await Launcher.LaunchFolderAsync(MainPage.OutputFolder, MyFolderLauncherOptions);


                    });




                };
                _fileInputNode.Start();
                _audioGraph.Start();
            }
            catch (Exception ex)
            {
                MainPage._MainPageInAppNotification.Show(ex.Message);

            }


           





        }

        public static AudioGraph _audioGraph;
        public static AudioFileInputNode _fileInputNode;
        public static AudioDeviceOutputNode _deviceOutputNode;
        public static AudioFileOutputNode _fileOutputNode;
        public static AudioFrameOutputNode _frameOutputNode;

        //public static EqualizerEffectDefinition eqEffectDefinition;




        public static async Task CreateAudioGraph()
        {
            if (_audioGraph != null)
            {
                Debug.WriteLine("AudioGraph already created no action");
                return;
            }

            AudioGraphSettings settings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media);
            //settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;
            // create AudioGraph
            var result = await AudioGraph.CreateAsync(settings);
            if (result.Status != AudioGraphCreationStatus.Success)
            {
                Debug.WriteLine("AudioGraph creation error: " + result.Status.ToString());
                return;
            }
            _audioGraph = result.Graph;
            //_audioGraph?.Start();
        }




        public static void CreateFrameOutput()
        {

            _frameOutputNode = _audioGraph.CreateFrameOutputNode();



        }










        public static async Task CreateDeviceOutput()
        {
            if (_deviceOutputNode != null)
            {
                return;
            }

            CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await _audioGraph.CreateDeviceOutputNodeAsync();
            if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                Debug.WriteLine("Cannot create device output node");
                return;
            }

            _deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;



        }

        public static async Task CreateFileOutput(StorageFile file)
        {
            
            var result = await _audioGraph.CreateFileOutputNodeAsync(file, MediaEncodingProfile.CreateMp3(AudioEncodingQuality.High));
            if (result.Status!= AudioFileNodeCreationStatus.Success)
            {
                Debug.WriteLine("Cannot create device output node");
                return;
            }
            
            _fileOutputNode = result.FileOutputNode;



        }

        public static async Task CreateFileInputNode(StorageFile file)
        {


            if (_fileInputNode != null)
            {
                _fileInputNode.RemoveOutgoingConnection(_frameOutputNode);
                _fileInputNode.RemoveOutgoingConnection(_deviceOutputNode);
                _fileInputNode.Stop();

                _fileInputNode.Reset();
            }


            CreateAudioFileInputNodeResult fileInputResult = await _audioGraph.CreateFileInputNodeAsync(file);
            if (AudioFileNodeCreationStatus.Success != fileInputResult.Status)
            {
                Debug.WriteLine("Cannot read input file");
                return;
            }

            _fileInputNode = fileInputResult.FileInputNode;



        }







        public static bool IsMusicTypeSupport(StorageFile file)
        {
            bool IsNeed = false;

            switch (file.FileType)
            {
                case ".flac":
                    IsNeed = true;
                    break;
                case ".mp3":
                    IsNeed = true;
                    break;

                case ".wav":
                    IsNeed = true;
                    break;
                case ".wma":
                    IsNeed = true;
                    break;
                case ".m4a":
                    IsNeed = true;
                    break;
                case ".aac":
                    IsNeed = true;
                    break;
                case ".ac3":
                    IsNeed = true;
                    break;
                default:
                    break;
            }

            return IsNeed;

        }






    }

songshizhao
最初发表2022/3/1 23:11:09 最近更新2022/3/1 23:21:18 173
为此篇作品打分
10
   评论