コード例 #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);
                }
            }
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
0
        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);
                }
            }
        }
コード例 #4
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;
                    }
                }
            }
        }
コード例 #5
0
        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);
                }
            }
        }