Beispiel #1
0
        private string GenerateCommandLine()
        {
            string[] mbdArray = { "simple", "bits", "rd" };
            string[] cmpArray = { "sad", "sse", "satd", "dct", "psnr", "bit", "rd" };

            var sb = new StringBuilder();

            _encProfile = _currentTask.VideoProfile as Mpeg2VideoProfile;

            if (_encProfile == null)
            {
                return(string.Empty);
            }

            _inputFile  = _currentTask.VideoStream.TempFile;
            _outputFile = FileSystemHelper.CreateTempFile(_appConfig.DemuxLocation,
                                                          _inputFile,
                                                          "encoded.m2v");

            _frameCount = _currentTask.VideoStream.FrameCount;

            #region AviSynth script generation

            var targetSys = _currentTask.EncodingProfile.SystemType;
            int targetHeight;

            var sourceFps = (float)Math.Round(_currentTask.VideoStream.Fps, 3);
            var targetFps = 0f;
            var changeFps = false;

            var sourceAspect = (float)Math.Round(_currentTask.VideoStream.AspectRatio, 3);

            var targetWidth = sourceAspect >= 1.4f ? 1024 : 720;

            if (_currentTask.Input == InputType.InputDvd)
            {
                _currentTask.VideoStream.Width =
                    (int)Math.Round(_currentTask.VideoStream.Height * sourceAspect, 0);
            }

            if (_currentTask.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                if (targetSys == 0)
                {
                    targetHeight = 576;
                    if (Math.Abs(sourceFps - 25f) > 0)
                    {
                        changeFps = true;
                    }
                    targetFps = 25f;
                }
                else
                {
                    targetHeight = 480;
                    if (Math.Abs(sourceFps - 29.970f) > 0 && Math.Abs(sourceFps - 23.976f) > 0)
                    {
                        changeFps = true;
                    }
                    targetFps = (float)Math.Round(30000f / 1001f, 3);
                }
            }
            else
            {
                targetWidth  = _currentTask.EncodingProfile.TargetWidth;
                targetHeight = (int)Math.Floor(targetWidth / sourceAspect);
            }

            var resizeTo = new Size(targetWidth, targetHeight);

            var sub            = _currentTask.SubtitleStreams.FirstOrDefault(item => item.HardSubIntoVideo);
            var subFile        = string.Empty;
            var keepOnlyForced = false;
            if (sub != null)
            {
                subFile        = sub.TempFile;
                keepOnlyForced = sub.KeepOnlyForcedCaptions;
            }

            if (string.IsNullOrEmpty(_currentTask.AviSynthScript))
            {
                var avs = new AviSynthGenerator(_appConfig);
                _currentTask.AviSynthScript = avs.Generate(_currentTask.VideoStream,
                                                           changeFps,
                                                           targetFps,
                                                           resizeTo,
                                                           StereoEncoding.None,
                                                           new StereoVideoInfo(),
                                                           true,
                                                           subFile,
                                                           keepOnlyForced,
                                                           false);
            }

            #endregion

            #region bitrate calculation

            var bitrate = _currentTask.EncodingProfile.TargetFileSize > 1
                ? VideoHelper.CalculateVideoBitrate(_currentTask)
                : _encProfile.Bitrate;

            var audBitrate = _currentTask.AudioStreams.Sum(stream => (int)stream.Bitrate / 1000);
            var maxRate    = 9800 - audBitrate;

            #endregion


            _encodingPass = _encProfile.EncodingMode == 0 ? 0 : _currentTask.StreamId;

            var targetSysStr = targetSys == 0 ? "pal" : "ntsc";

            sb.Append($" -i \"{_currentTask.AviSynthScript}\" -map 0:v");
            sb.Append($" -target {targetSysStr}-dvd");
            sb.Append($" -b:v {bitrate:0}k -maxrate {maxRate:0}k -qmin 1");

            sb.Append($" -aspect {sourceAspect:0.000}".ToString(_appConfig.CInfo));

            if (_encodingPass > 0)
            {
                sb.Append($" -pass {_encodingPass:0}");
            }

            if (_encProfile.MbDecision > 0)
            {
                sb.Append($" -mbd {mbdArray[_encProfile.MbDecision]}");
            }

            if (_encProfile.Trellis > 0)
            {
                sb.Append($" -trellis {_encProfile.Trellis:0}");
            }

            if (_encProfile.Cmp > 0)
            {
                sb.Append($" -cmp {cmpArray[_encProfile.Cmp]}");
            }

            if (_encProfile.SubCmp > 0)
            {
                sb.Append($" -subcmp {cmpArray[_encProfile.SubCmp]}");
            }

            if (_encProfile.DcPrecision > 0)
            {
                sb.Append($" -dc {_encProfile.DcPrecision}");
            }

            if (_encProfile.ClosedGops)
            {
                sb.Append(" -flags cgop -mpv_flags strict_gop -sc_threshold 1000000000");
            }

            if (!_encProfile.AutoGop)
            {
                sb.Append($" -g {_encProfile.GopLength:0}");
            }

            sb.Append($" -f rawvideo -y \"{_outputFile}\"");
            return(sb.ToString());
        }