Example #1
0
        private string GenerateCommandLine()
        {
            var sb = new StringBuilder();

            var fps = _currentTask.VideoStream.Fps;
            int vidStream;

            var tempExt = Path.GetExtension(_currentTask.VideoStream.TempFile);

            if (_currentTask.VideoStream.IsRawStream)
            {
                vidStream = 0;
            }
            else if ((_currentTask.Input == InputType.InputAvi) && (!_currentTask.VideoStream.Encoded))
            {
                vidStream = 0;
            }
            else if ((_currentTask.VideoStream.Encoded) || (tempExt == ".mp4"))
            {
                vidStream = 0;
            }
            else
            {
                vidStream = _currentTask.VideoStream.StreamId;
            }

            _outputFile = !string.IsNullOrEmpty(_currentTask.TempOutput)
                        ? _currentTask.TempOutput
                        : _currentTask.OutputFile;

            var fpsStr = string.Empty;

            if (_currentTask.VideoStream.IsRawStream)
            {
                fpsStr = _currentTask.VideoStream.FrameRateEnumerator == 0 || _appConfig.LastMp4BoxVer.StartsWith("0.5")
                    ? $":fps={fps:0.000}".ToString(_appConfig.CInfo)
                    : $":fps={_currentTask.VideoStream.FrameRateEnumerator:0}/{_currentTask.VideoStream.FrameRateDenominator:0}";
            }

            sb.Append($"-add \"{_currentTask.VideoStream.TempFile}#video:trackID={vidStream:0}{fpsStr}:lang=eng\" -keep-sys ");

            _streamImportCount = 1;

            foreach (var item in _currentTask.AudioStreams)
            {
                var itemlang = item.LangCode;

                if ((itemlang == "xx") || (string.IsNullOrEmpty(itemlang)))
                {
                    itemlang = "und";
                }

                var delayString = string.Empty;

                if (item.Delay != 0)
                {
                    delayString = $":delay={item.Delay:0}";
                }

                sb.Append($"-add \"{item.TempFile}#audio:lang={itemlang}{delayString}\" -keep-sys ");
                _streamImportCount++;
            }

            foreach (var item in _currentTask.SubtitleStreams)
            {
                if (item.Format.ToLowerInvariant() != "utf-8")
                {
                    continue;
                }
                if (!File.Exists(item.TempFile))
                {
                    continue;
                }

                var itemlang = item.LangCode;

                if ((itemlang == "xx") || (string.IsNullOrEmpty(itemlang)))
                {
                    itemlang = "und";
                }

                var delayString = string.Empty;

                if (item.Delay != 0)
                {
                    delayString = $":delay={item.Delay:0}";
                }

                sb.Append($"-add \"{item.TempFile}#lang={itemlang}{delayString}:name={LanguageHelper.GetLanguage(itemlang).FullLang}\" -keep-sys ");
                _streamImportCount++;
            }

            if (_currentTask.Chapters.Count > 1)
            {
                var chapterFile = FileSystemHelper.CreateTempFile(
                    _appConfig.DemuxLocation,
                    !string.IsNullOrEmpty(_currentTask.TempOutput)
                                                            ? _currentTask.TempOutput
                                                            : _currentTask.OutputFile,
                    "chapters.ttxt");

                var xmlSettings = new XmlWriterSettings
                {
                    Indent           = true,
                    IndentChars      = "    ",
                    Encoding         = Encoding.UTF8,
                    NewLineHandling  = NewLineHandling.Entitize,
                    ConformanceLevel = ConformanceLevel.Auto,
                    CloseOutput      = true
                };
                var writer = XmlWriter.Create(chapterFile, xmlSettings);
                writer.WriteStartDocument(true);
                writer.WriteStartElement("TextStream");
                writer.WriteAttributeString("version", "1.1");

                int temp;
                var subHeight = Math.DivRem(_currentTask.VideoStream.Height, 3, out temp);
                subHeight += temp;

                writer.WriteStartElement("TextStreamHeader");
                writer.WriteAttributeString("width", _currentTask.VideoStream.Width.ToString("G"));
                writer.WriteAttributeString("height", subHeight.ToString("G"));
                writer.WriteAttributeString("layer", "0");
                writer.WriteAttributeString("translation_x", "0");
                writer.WriteAttributeString("translation_y", "0");

                writer.WriteStartElement("TextSampleDescription");
                writer.WriteAttributeString("horizontalJustification", "center");
                writer.WriteAttributeString("verticalJustification", "bottom");
                writer.WriteAttributeString("backColor", "0 0 0 0");
                writer.WriteAttributeString("verticalText", "no");
                writer.WriteAttributeString("fillTextRegion", "no");
                writer.WriteAttributeString("continousKaraoke", "no");
                writer.WriteAttributeString("scroll", "None");

                writer.WriteStartElement("FontTable");

                writer.WriteStartElement("FontTableEntry");
                writer.WriteAttributeString("fontName", "Arial");
                writer.WriteAttributeString("fontID", "1");
                writer.WriteEndElement(); // FontTableEntry

                writer.WriteEndElement(); // FontTable

                writer.WriteStartElement("TextBox");
                writer.WriteAttributeString("top", "0");
                writer.WriteAttributeString("left", "0");
                writer.WriteAttributeString("bottom", _currentTask.VideoStream.Height.ToString("G"));
                writer.WriteAttributeString("right", _currentTask.VideoStream.Width.ToString("G"));
                writer.WriteEndElement(); // TextBox

                writer.WriteStartElement("Style");
                writer.WriteAttributeString("styles", "Normal");
                writer.WriteAttributeString("fontID", "1");
                writer.WriteAttributeString("fontSize", "32");
                writer.WriteAttributeString("color", "ff ff ff ff");
                writer.WriteEndElement(); // Style

                writer.WriteEndElement(); // TextSampleDescription

                writer.WriteEndElement(); // TextStreamHeader

                for (var index = 0; index < _currentTask.Chapters.Count; index++)
                {
                    var dt = DateTime.MinValue.Add(_currentTask.Chapters[index]);
                    writer.WriteStartElement("TextSample");
                    writer.WriteAttributeString("sampleTime", dt.ToString("HH:mm:ss.fff"));
                    writer.WriteValue($"Chapter {index + 1:0}");
                    writer.WriteEndElement(); // TextSample
                }

                writer.WriteEndElement(); // TextStream
                writer.WriteEndDocument();

                writer.Flush();
                writer.Close();

                sb.Append($" -add \"{chapterFile}:chap\"");
                _currentTask.TempFiles.Add(chapterFile);
                _streamImportCount++;
            }

            var tool     = $"{AppConfigService.GetProductName()} v{AppConfigService.GetAppVersion().ToString(4)}";
            var tempPath = _appConfig.DemuxLocation;

            sb.Append($"-itags tool=\"{tool}\" -tmp \"{tempPath}\" -new \"{_outputFile}\"");

            return(sb.ToString());
        }