private static async Task convertAudio(IStream audioStream, string outputFilePath)
        {
            var conv = new Conversion();

            conv.AddStream(audioStream);
            conv.SetOutput(outputFilePath);

            //constant bitrate needed to stop song length from doubling incorrectly - FFMpeg bug
            //see here https://superuser.com/questions/892996/ffmpeg-is-doubling-audio-length-when-extracting-from-video/893044
            //128k seems to be typical youtube audio quality - won't increase file size
            conv.SetAudioBitrate("128k");
            await conv.Start(); //wait for conversion to finish;
        }
Example #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);
            }
        }