示例#1
0
        static void ReadWriteAudio(string input, string output)
        {
            var audio = new AudioReader(input);

            audio.LoadMetadataAsync().Wait();
            audio.Load();

            using (var writer = new AudioWriter(output, audio.Metadata.Channels, audio.Metadata.SampleRate))
            {
                writer.OpenWrite(true);

                var frame = new AudioFrame(1);
                while (true)
                {
                    // read next sample
                    var f = audio.NextFrame(frame);
                    if (f == null)
                    {
                        break;
                    }

                    writer.WriteFrame(frame);
                }
            }
        }
        public async Task LoadMetadataOgg()
        {
            var audio = new AudioReader(Res.GetPath(Res.Audio_Ogg));

            await audio.LoadMetadataAsync();

            Assert.True(audio.Metadata.Codec == "vorbis");
            Assert.True(audio.Metadata.BitRate == 48000);
            Assert.True(audio.Metadata.SampleFormat == "fltp");
            Assert.True(audio.Metadata.SampleRate == 11025);
            Assert.True(audio.Metadata.Channels == 2);
            Assert.True(audio.Metadata.Streams.Length == 1);
            Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.01);
        }
        public async Task LoadMetadataMp3()
        {
            var audio = new AudioReader(Res.GetPath(Res.Audio_Mp3));

            await audio.LoadMetadataAsync();

            Assert.True(audio.Metadata.Codec == "mp3");
            Assert.True(audio.Metadata.BitRate == 128000);
            Assert.True(audio.Metadata.SampleFormat == "fltp");
            Assert.True(audio.Metadata.SampleRate == 44100);
            Assert.True(audio.Metadata.Channels == 2);
            Assert.True(audio.Metadata.Streams.Length == 1);
            Assert.True(Math.Abs(audio.Metadata.Duration - 1.549187) < 0.01);
        }
示例#4
0
        public async Task ConversionStreamTest()
        {
            var path  = Res.GetPath(Res.Audio_Mp3);
            var opath = "out-test-v-2.aac";

            try
            {
                using var reader = new AudioReader(path);
                await reader.LoadMetadataAsync();

                var encoder = new AACEncoder
                {
                    Format = "flv"
                };

                using (var filestream = File.Create(opath))
                {
                    using (var writer = new AudioWriter(filestream,
                                                        reader.Metadata.Channels,
                                                        reader.Metadata.SampleRate, 16,
                                                        encoder.Create()))
                    {
                        writer.OpenWrite();

                        reader.Load();

                        await reader.CopyToAsync(writer);
                    }
                }

                using var audio = new AudioReader(opath);
                await audio.LoadMetadataAsync();

                Assert.True(audio.Metadata.Format.FormatName == "flv");
                Assert.True(audio.Metadata.Channels == 2);
                Assert.True(audio.Metadata.Streams.Length == 1);
                Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.2);
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }
        public async Task FFmpegWrapperProgressTest()
        {
            var path  = Res.GetPath(Res.Audio_Ogg);
            var opath = "out-test.mp3";

            double lastval = -1;

            try
            {
                var audio = new AudioReader(path);

                await audio.LoadMetadataAsync();

                var dur = audio.Metadata.Duration;
                audio.Dispose();

                Assert.True(Math.Abs(dur - 1.515102) < 0.01);

                var p        = FFmpegWrapper.ExecuteCommand("ffmpeg", $"-i \"{path}\" \"{opath}\"");
                var progress = FFmpegWrapper.RegisterProgressTracker(p, dur);
                progress.ProgressChanged += (s, prg) => lastval = prg;
                p.WaitForExit();

                await Task.Delay(300);

                Assert.True(lastval > 50 && lastval <= 100);

                audio = new AudioReader(opath);

                await audio.LoadMetadataAsync();

                Assert.True(audio.Metadata.Channels == 2);
                Assert.True(audio.Metadata.Streams.Length == 1);
                Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.2);

                audio.Dispose();
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }
        public async Task ConversionTest()
        {
            var path  = Res.GetPath(Res.Audio_Ogg);
            var opath = "out-test-2.mp3";

            try
            {
                using var reader = new AudioReader(path);
                await reader.LoadMetadataAsync();

                using (var writer = new AudioWriter(opath,
                                                    reader.Metadata.Channels,
                                                    reader.Metadata.SampleRate, 16,
                                                    new MP3Encoder().Create()))
                {
                    writer.OpenWrite();

                    reader.Load();

                    await reader.CopyToAsync(writer);
                }

                using var audio = new AudioReader(opath);
                await audio.LoadMetadataAsync();

                Assert.True(audio.Metadata.Format.FormatName == "mp3");
                Assert.True(audio.Metadata.Channels == 2);
                Assert.True(audio.Metadata.Streams.Length == 1);
                Assert.True(Math.Abs(audio.Metadata.Duration - 1.515102) < 0.2);
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }
示例#7
0
        static void ReadPlayAudio(string input, string output)
        {
            var audio = new AudioReader(input);

            audio.LoadMetadataAsync().Wait();
            audio.Load();

            using (var player = new AudioPlayer())
            {
                player.OpenWrite(audio.Metadata.SampleRate, audio.Metadata.Channels, showWindow: false);

                // For simple playing, can just use "CopyTo"
                // audio.CopyTo(player);

                var frame = new AudioFrame(audio.Metadata.Channels);
                while (true)
                {
                    // read next frame
                    var f = audio.NextFrame(frame);
                    if (f == null)
                    {
                        break;
                    }

                    try
                    {
                        player.WriteFrame(frame);
                    }
                    catch (IOException) { break; }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
        public async Task ConversionStreamTest()
        {
            var vpath = Res.GetPath(Res.Video_Mp4);
            var apath = Res.GetPath(Res.Audio_Mp3);
            var opath = "out-test-av-2.mp4";

            try
            {
                using var vreader = new VideoReader(vpath);
                await vreader.LoadMetadataAsync();

                vreader.Load();

                using var areader = new AudioReader(apath);
                await areader.LoadMetadataAsync();

                areader.Load();

                // Get video and audio stream metadata
                var vstream = vreader.Metadata.GetFirstVideoStream();
                var astream = areader.Metadata.GetFirstAudioStream();

                var encoder = new H264Encoder
                {
                    Format = "flv"
                };

                using (var filestream = File.Create(opath))
                {
                    // Prepare writer (Converting to H.264 + AAC video)
                    using (var writer = new AudioVideoWriter(filestream,
                                                             vstream.Width.Value,
                                                             vstream.Height.Value,
                                                             vstream.AvgFrameRateNumber,
                                                             astream.Channels.Value,
                                                             astream.SampleRateNumber, 16,
                                                             encoder.Create(),
                                                             new AACEncoder().Create()))
                    {
                        // Open for writing (this starts the FFmpeg process)
                        writer.OpenWrite();

                        // Copy raw data directly from stream to stream
                        var t2 = areader.DataStream.CopyToAsync(writer.InputDataStreamAudio);
                        var t1 = vreader.DataStream.CopyToAsync(writer.InputDataStreamVideo);

                        await t1;
                        await t2;
                    }
                }

                using var video = new VideoReader(opath);
                await video.LoadMetadataAsync();

                Assert.True(video.Metadata.Streams.Length == 2);

                vstream = video.Metadata.GetFirstVideoStream();
                astream = video.Metadata.GetFirstAudioStream();

                Assert.True(Math.Abs(vstream.AvgFrameRateNumber - vreader.Metadata.AvgFramerate) < 0.1);
                Assert.True(Math.Abs(video.Metadata.Duration - vreader.Metadata.Duration) < 0.2);
                Assert.True(vstream.Width.Value == vreader.Metadata.Width);
                Assert.True(vstream.Height.Value == vreader.Metadata.Height);
                Assert.True(astream.SampleRateNumber == areader.Metadata.SampleRate);
            }
            finally
            {
                if (File.Exists(opath))
                {
                    File.Delete(opath);
                }
            }
        }