Beispiel #1
0
        public async Task BurnSubtitlesWithParametersTest()
        {
            string outputPath = Path.ChangeExtension(Path.GetTempFileName(), Extensions.Mp4);
            string subtitle   = Resources.SubtitleSrt.Replace("\\", "\\\\")
                                .Replace(":", "\\:");

            IConversion conversion = new Conversion()
                                     .SetInput(Resources.MkvWithAudio)
                                     .SetSubtitle(Resources.SubtitleSrt, "UTF-8", "Fontsize=20,PrimaryColour=&H00ffff&,MarginV=30", new Size(1024, 768))
                                     .SetOutput(outputPath);
            bool conversionResult = await conversion.Start();

            Assert.True(conversionResult);
            var mediaInfo = new MediaInfo(outputPath);

            Assert.Equal(TimeSpan.FromSeconds(9), mediaInfo.Properties.Duration);
            Assert.Equal("h264", mediaInfo.Properties.VideoFormat);
            Assert.Equal("aac", mediaInfo.Properties.AudioFormat);
            Assert.Equal($"-i \"{Resources.MkvWithAudio}\" -n -filter:v \"subtitles='{subtitle}':charenc=UTF-8:force_style='Fontsize=20,PrimaryColour=&H00ffff&,MarginV=30':original_size=1024x768\" \"{outputPath}\"", conversion.Build());
        }
Beispiel #2
0
        async private void CutGif(int from, int to, String filePath, String[] additionalFilter = null, bool subtitles = false, bool fullResolution = false, bool keepAudio = false)
        {
            if (from > to)
            {
                int t = from;
                from = to;
                to   = t;
            }
            if (from == to)
            {
                ShowMessage(MessageType.Warning, "Start and end markers have same position");
                return;
            }
            if (TimeSpan.FromMilliseconds(to - from) > TimeSpan.FromMinutes(kWarningTresholdMin))
            {
                if (MessageBox.Show("You selected timespan longer than 5 minutes. Do you want proceed?", "Gifer", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    return;
                }
            }
            var subpath     = Path.ChangeExtension(Path.GetTempFileName(), "ass");
            var escapedPath = subpath.Replace("\\", "\\\\").Replace(":", "\\:").Replace("[", "\\[").Replace("]", "\\]");
            var mediaInfo   = await FFmpeg.GetMediaInfo(filePath);

            bool isDVDsubs = false;

            if (subtitles)
            {
                if (selectedSubtitle == null)
                {
                    selectedSubtitle = mediaInfo.SubtitleStreams.First();
                }
                if (selectedSubtitle.Codec == "dvd_subtitle")
                {
                    isDVDsubs = true;
                }
                else
                {
                    var subconv = FFmpeg.Conversions.New().AddStream(selectedSubtitle).SetOutput(subpath);
                    WriteToLog(subconv.Build());
                    await subconv.Start();
                }
            }
            var videoStream = mediaInfo.VideoStreams.First();
            var fileName    = Path.GetFileNameWithoutExtension(filePath);
            var resultName  = fileName + "_" + from + "_" + to + ".mp4";
            var videoPath   = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
            var resultPath  = Path.Combine(videoPath, resultName);
            var resizeVf    = "scale=iw*sar:ih, scale='min(800,ceil(iw/2)*2)':-2";
            var roundVf     = "pad=ceil(iw/2)*2:ceil(ih/2)*2";
            var subtitlesVf = FormattableString.Invariant($"subtitles='{escapedPath}':original_size={videoStream.Width}x{videoStream.Height}:force_style='FontName=Open Sans Semibold,PrimaryColour=&H00FFFFFF,Bold=1,ScaleX={Configuration.SubtitlesScale:F1},ScaleY={Configuration.SubtitlesScale:F1}'");
            var vfs         = new List <String>();

            if (additionalFilter != null)
            {
                vfs.AddRange(additionalFilter);
            }
            if (!fullResolution)
            {
                vfs.Add(resizeVf);
            }
            else
            {
                vfs.Add(roundVf);
            }
            if (subtitles)
            {
                if (isDVDsubs)
                {
                    var dvdSubsVf = FormattableString.Invariant($"scale[vidi];[sub][vidi]scale2ref[subrs][vidio];[vidio][subrs]overlay;[0:{selectedSubtitle.Index}]fps=fps={videoStream.Framerate}[sub]");
                    vfs.Add(dvdSubsVf);
                }
                else
                {
                    vfs.Add(subtitlesVf);
                }
            }
            var crf  = Configuration.CRF;
            var conv = new Conversion().AddStream(videoStream).AddParameter($"-ss {from}ms -to {to}ms -copyts", ParameterPosition.PreInput)
                       .SetPixelFormat(PixelFormat.yuv420p)
                       .AddParameter($"-filter_complex \"[0:v]{BuildVF(vfs)}\" -map -0:s -c:v libx264 -crf {crf} -profile:v baseline -ss {from}ms")
                       .SetOutput(resultPath).SetOverwriteOutput(true);

            if (keepAudio)
            {
                if (selectedAudio == null)
                {
                    selectedAudio = mediaInfo.AudioStreams.First();
                }
                conv = conv.AddStream(selectedAudio);
            }
            if (isDVDsubs)
            {
                conv = conv.AddStream(selectedSubtitle);
            }
            WriteToLog(conv.Build());
            try {
                var convProcess = await conv.Start();
            }
            catch (Exception ex) {
                WriteToLog(ex.ToString());
                ShowMessage(MessageType.Error, "Building gif failed");
                return;
            }
            ShowMessage(MessageType.Success, "Finished building gif");
            StringCollection resultList = new StringCollection {
                resultPath
            };

            Clipboard.SetFileDropList(resultList);
            if (subtitles)
            {
                File.Delete(subpath);
            }
        }