Exemplo n.º 1
0
        public void Converting_Multiple_Files_Simultaneously()
        {
            var outputFiles = new List <FileInfo>();

            for (var i = 0; i < Environment.ProcessorCount; i++)
            {
                outputFiles.Add(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"MediaFiles\output{i}.mp4")));
            }

            var ffmpeg = new Engine.FFmpeg();

            ffmpeg.Complete += (sender, args) => { _output.WriteLine("Complete: [{0} => {1}]", args.Input.FileInfo.Name, args.Output.FileInfo.Name); };
            ffmpeg.Progress += (sender, args) => { _output.WriteLine("Progress: {0}", args); };
            ffmpeg.Error    += (sender, args) => { _output.WriteLine("Error: [{0} => {1}] ExitCode: {2}\n{3}", args.Input.FileInfo.Name, args.Output.FileInfo.Name, args.Exception.ExitCode, args.Exception); };
            ffmpeg.Data     += (sender, args) => { _output.WriteLine("Data: {0} => {1} | {2}", args.Input.FileInfo.Name, args.Output.FileInfo.Name, args.Data); };

            var tasks = new List <Task>();

            foreach (var outputFile in outputFiles)
            {
                tasks.Add(Task.Run(() => ffmpeg.Convert(_fixture.VideoFile, new MediaFile(outputFile))));
            }

            Task.WaitAll(tasks.ToArray());

            foreach (var outputFile in outputFiles)
            {
                Assert.True(File.Exists(outputFile.FullName));
                outputFile.Delete();
                Assert.False(File.Exists(outputFile.FullName));
            }
        }
Exemplo n.º 2
0
        public void XspfPlaylistCreator_Creates_Valid_Xml()
        {
            var ffmpeg = new Engine.FFmpeg();
            var meta1  = ffmpeg.GetMetaData(_fixture.VideoFile);
            var meta2  = ffmpeg.GetMetaData(_fixture.AudioFile);

            Assert.NotNull(meta1);
            Assert.NotNull(meta2);

            var xml = new XspfPlaylistCreator().Create(new[] { meta1, meta2 });

            Assert.NotNull(xml);
            Assert.NotEmpty(xml);

            using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("FFmpeg.NET.Tests.Resources.test.xspf"))
                using (var sr = new StreamReader(resource))
                {
                    var xspf = sr.ReadToEnd();

                    Assert.NotNull(xspf);

                    var assemblyPath = Path.GetFullPath(Assembly.GetExecutingAssembly().Location);
                    var file1        = Path.GetRelativePath(assemblyPath, _fixture.VideoFile.FileInfo.FullName);
                    var file2        = Path.GetRelativePath(assemblyPath, _fixture.AudioFile.FileInfo.FullName);

                    Assert.NotNull(file1);
                    Assert.NotNull(file2);
                    Assert.Contains($"file:///{file1.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}", xspf);
                    Assert.Contains($"file:///{file2.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}", xspf);
                }
        }
Exemplo n.º 3
0
        public void FFmpeg_Should_Throw_Exception_On_Invalid_OutputFile()
        {
            var ffmpeg = new Engine.FFmpeg();
            var output = new MediaFile("test.txt");
            var input  = _fixture.VideoFile;

            var e = Assert.Raises <ConversionErrorEventArgs>(
                x => ffmpeg.Error += x,
                x => ffmpeg.Error -= x,
                () => ffmpeg.Convert(input, output));

            Assert.NotNull(e);
            Assert.Equal(e.Sender, ffmpeg);
            Assert.Equal(input, e.Arguments.Input);
            Assert.Equal(output, e.Arguments.Output);
            Assert.Equal(1, e.Arguments.Exception.ExitCode);
        }
Exemplo n.º 4
0
        public void Multiple_FFmpeg_Instances_At_Once_Do_Not_Throw_Exception()
        {
            var task1 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });
            var task2 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });
            var task3 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });

            Task.WaitAll(task1, task2, task3);

            Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); }).Wait();

            task1 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);
            task2 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);
            task3 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);
            Task.WaitAll(task1, task2, task3);

            new Engine.FFmpeg().GetMetaData(_fixture.VideoFile);
        }
Exemplo n.º 5
0
        public void Multiple_FFmpeg_Instances_At_Once_Do_Not_Throw_Exception()
        {
            // TODO Verify why following commented code throws exception
            //var task1 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });
            //var task2 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });
            //var task3 = Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); });
            //Task.WaitAll(task1, task2, task3);

            Task.Run(() => { new Engine.FFmpeg().GetMetaData(_fixture.VideoFile); }).Wait();

            var task1 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);
            var task2 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);
            var task3 = new Engine.FFmpeg().GetMetaDataAsync(_fixture.VideoFile);

            Task.WaitAll(task1, task2, task3);

            new Engine.FFmpeg().GetMetaData(_fixture.VideoFile);
        }
Exemplo n.º 6
0
        public void FFmpeg_Can_Read_Audio_Metadata()
        {
            var ffmpeg = new Engine.FFmpeg();

            var audioFile = _fixture.AudioFile;
            var metaData  = ffmpeg.GetMetaData(audioFile);

            Assert.NotNull(metaData);

            Assert.Equal(metaData.FileInfo, audioFile.FileInfo);

            Assert.NotNull(metaData.AudioData);
            Assert.Equal("mp3", metaData.AudioData.Format);
            Assert.Equal("44100 Hz", metaData.AudioData.SampleRate);
            Assert.Equal("stereo", metaData.AudioData.ChannelOutput);
            Assert.Equal(128, metaData.AudioData.BitRateKbs);

            Assert.Null(metaData.VideoData);
        }
Exemplo n.º 7
0
        public void FFmpeg_Invokes_ConversionCompleteEvent()
        {
            var output = new MediaFile(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"MediaFiles\conversionTest.mp4")));

            ConversionCompleteEventArgs completeEventArgs = null;

            var ffmpeg = new Engine.FFmpeg();

            ffmpeg.Complete += (sender, args) => {
                completeEventArgs = args;
                _outputHelper.WriteLine("ConversionCompletedEvent: {0}", args);
            };
            ffmpeg.Convert(_fixture.VideoFile, output);

            Assert.True(File.Exists(output.FileInfo.FullName));
            output.FileInfo.Delete();
            Assert.False(File.Exists(output.FileInfo.FullName));

            Assert.NotNull(completeEventArgs);
            Assert.NotNull(completeEventArgs.Output);
            Assert.Equal(output, completeEventArgs.Output);
        }
Exemplo n.º 8
0
        public void FFmpeg_Can_Read_Video_Metadata()
        {
            var ffmpeg = new Engine.FFmpeg();

            var videoFile = _fixture.VideoFile;
            var metaData  = ffmpeg.GetMetaData(videoFile);

            Assert.NotNull(metaData);
            Assert.Equal(metaData.FileInfo, videoFile.FileInfo);
            Assert.NotNull(metaData.VideoData);
            Assert.Equal("h264 (Main) (avc1 / 0x31637661)", metaData.VideoData.Format);
            Assert.Equal("yuv420p,", metaData.VideoData.ColorModel);
            Assert.Equal("1280x720", metaData.VideoData.FrameSize);
            Assert.Equal(1205, metaData.VideoData.BitRateKbs);
            Assert.Equal(25, metaData.VideoData.Fps);

            Assert.NotNull(metaData.AudioData);
            Assert.Equal("aac (LC) (mp4a / 0x6134706D)", metaData.AudioData.Format);
            Assert.Equal("48000 Hz", metaData.AudioData.SampleRate);
            Assert.Equal("5.1", metaData.AudioData.ChannelOutput);
            Assert.Equal(384, metaData.AudioData.BitRateKbs);
        }
Exemplo n.º 9
0
        public void M3uPlaylistCreator_Creates_Valid_m3u8_Content()
        {
            var ffmpeg = new Engine.FFmpeg();
            var meta1  = ffmpeg.GetMetaData(_fixture.VideoFile);
            var meta2  = ffmpeg.GetMetaData(_fixture.AudioFile);

            Assert.NotNull(meta1);
            Assert.NotNull(meta2);

            var m3u8 = new M3uPlaylistCreator().Create(new[] { meta1, meta2 });

            Assert.NotNull(m3u8);

            var lines = m3u8.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);

            Assert.True(lines.Length == 5);

            Assert.Equal("#EXTM3U", lines[0]);
            Assert.Equal("#EXTINF:5,SampleVideo_1280x720_1mb.mp4", lines[1]);
            Assert.Equal($"file:///{_fixture.VideoFile.FileInfo.FullName.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}", lines[2]);
            Assert.Equal("#EXTINF:27,SampleAudio_0.4mb.mp3", lines[3]);
            Assert.Equal($"file:///{_fixture.AudioFile.FileInfo.FullName.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}", lines[4]);
        }