示例#1
0
        /// <summary>
        /// Read stream info
        /// </summary>
        /// <param name="mi"><see cref="MediaInfoContainer"/></param>
        /// <param name="vStream">Video stream</param>
        /// <param name="bluRayTarget"></param>
        /// <returns></returns>
        public static VideoInfo GetStreamInfo(MediaInfoContainer mi, VideoInfo vStream, bool bluRayTarget)
        {
            if (mi.Video.Count <= 0)
            {
                return(vStream);
            }

            float.TryParse(mi.Video[0].DisplayAspectRatio, NumberStyles.Number, CInfo,
                           out vStream.AspectRatio);
            vStream.Bitrate       = mi.Video[0].BitRate;
            vStream.Format        = mi.Video[0].Format;
            vStream.FormatProfile = mi.Video[0].FormatProfile;
            if (mi.Video[0].FrameRateEnumerator < vStream.FrameRateEnumerator * 2 || !bluRayTarget)
            {
                vStream.Fps                  = mi.Video[0].FrameRate;
                vStream.FrameCount           = mi.Video[0].FrameCount;
                vStream.FrameRateDenominator = mi.Video[0].FrameRateDenominator;
                vStream.FrameRateEnumerator  = mi.Video[0].FrameRateEnumerator;
            }
            vStream.Height     = mi.Video[0].Height;
            vStream.Width      = mi.Video[0].Width;
            vStream.Interlaced = mi.Video[0].ScanType != "Progressive";
            vStream.Length     = mi.Video[0].DurationTime.TimeOfDay.TotalSeconds;
            vStream.PicSize    = mi.Video[0].VideoSize;
            vStream.StreamSize = GenHelper.GetFileSize(vStream.TempFile);
            vStream.FrameMode  = mi.Video[0].FormatFrameMode;
            return(vStream);
        }
示例#2
0
        private static MediaInfoContainer DoWorkHandler(string fileName)
        {
            MediaInfoContainer mi = new MediaInfoContainer();

            mi.GetMediaInfo(fileName);
            return(mi);
        }
示例#3
0
        public static AudioInfo GetStreamInfo(AudioInfo aStream)
        {
            MediaInfoContainer mi = new MediaInfoContainer();

            try
            {
                mi = Processing.GetMediaInfo(aStream.TempFile);
            }
            catch (TimeoutException ex)
            {
                Log.Error(ex);
                mi = new MediaInfoContainer();
            }
            finally
            {
                if (mi.Audio.Count > 0)
                {
                    aStream.Bitrate       = mi.Audio[0].BitRate;
                    aStream.BitDepth      = mi.Audio[0].BitDepth;
                    aStream.ChannelCount  = mi.Audio[0].Channels;
                    aStream.SampleRate    = mi.Audio[0].SamplingRate;
                    aStream.Format        = mi.Audio[0].Format;
                    aStream.FormatProfile = mi.Audio[0].FormatProfile;
                    aStream.StreamSize    = Processing.GetFileSize(aStream.TempFile);
                    if (aStream.Format == "PCM")
                    {
                        aStream.Length = GetRuntimePCM(aStream);
                    }
                    else
                    {
                        aStream.Length = mi.Audio[0].Duration / 1000d; // convert from ms to seconds
                    }
                }
            }
            return(aStream);
        }
示例#4
0
        public void GetFileInfo()
        {
            var mi = new MediaInfoContainer();

            try
            {
                mi = GenHelper.GetMediaInfo(JobInfo.InputFile);
            }
            catch (TimeoutException ex)
            {
                Log.Error(ex);
            }

            JobInfo.MediaInfo = mi;

            const string strChapters  = "Chapters";
            const string strVideo     = "Video";
            const string strAudio     = "Audio";
            const string strSubtitles = "Subtitles";

            var containerFormat = mi.General.Format;
            var duration        = mi.General.DurationTime.ToString("H:mm:ss.fff");
            var shortFileName   = mi.General.FileName + "." + mi.General.FileExtension;

            var treeRoot = $"{shortFileName} / {containerFormat} / Length: {duration}";

            var root = new StreamTreeNode
            {
                ID         = _treeNodeID++,
                Name       = treeRoot,
                Data       = JobInfo.InputFile,
                IsChecked  = true,
                IsExpanded = true,
                Children   = new List <StreamTreeNode>()
            };

            root.PropertyChanged += TreeNodePropertyChanged;
            _tree.Add(root);

            var chaptersStreamTree = CreateNode(root, strChapters, null);
            var videoStreamTree    = CreateNode(root, strVideo, null);
            var audioStreamTree    = CreateNode(root, strAudio, null);
            var subStreamTree      = CreateNode(root, strSubtitles, null);

            if (mi.Chapters.Count > 0)
            {
                var chaptersTitle = $"{mi.Chapters.Count:0} {strChapters}";

                CreateNode(chaptersStreamTree, chaptersTitle, mi.Chapters);
            }
            else
            {
                chaptersStreamTree.IsChecked = false;
            }

            var streamIndex = 0;

            foreach (var clip in mi.Video)
            {
                streamIndex++;
                var videoPid        = clip.ID;
                var videoCodec      = clip.FormatInfo;
                var videoCodecShort = clip.Format;

                var videoDesc        = $"{clip.Width:0}x{clip.Height:0} {clip.ScanType} / Profile: {clip.FormatProfile} / {clip.FrameRate:0.000}fps";
                var videoStreamTitle = $"{streamIndex:0}: {videoCodec} ({videoCodecShort}), {videoDesc}";

                var vid = new VideoInfo();
                if (JobInfo.Input == InputType.InputAvi)
                {
                    vid.StreamId = 0;
                }
                else
                {
                    vid.StreamId = videoPid == 0 ? streamIndex : videoPid;
                }
                vid.StreamKindID  = clip.StreamKindID;
                vid.Fps           = clip.FrameRate;
                vid.PicSize       = clip.VideoSize;
                vid.Interlaced    = clip.ScanType == "Interlaced";
                vid.Format        = clip.Format;
                vid.FormatProfile = clip.FormatProfile;
                vid.Height        = clip.Height;
                vid.Width         = clip.Width;
                vid.FrameCount    = clip.FrameCount;
                vid.StreamSize    = clip.StreamSize;
                vid.Length        = mi.General.DurationTime.TimeOfDay.TotalSeconds;
                float.TryParse(clip.DisplayAspectRatio, NumberStyles.Number, _configService.CInfo, out vid.AspectRatio);
                vid.FrameRateEnumerator  = clip.FrameRateEnumerator;
                vid.FrameRateDenominator = clip.FrameRateDenominator;
                vid.FrameMode            = clip.FormatFrameMode;

                CreateNode(videoStreamTree, videoStreamTitle, vid);
            }

            videoStreamTree.IsChecked = videoStreamTree.Children.Count > 0;

            foreach (var audio in mi.Audio)
            {
                streamIndex++;
                var audioPid          = audio.ID;
                var audioCodec        = audio.FormatInfo;
                var audioCodecShort   = audio.Format;
                var audioLangCode     = audio.LanguageIso6392;
                var audioLanguage     = audio.LanguageFull;
                var audioStreamKindID = audio.StreamKindID;

                var audioDesc = $"{audio.Channels:0} Channels ({audio.ChannelPositions}) / {audio.SamplingRate:0}Hz / ";
                audioDesc += $"{audio.BitDepth:0} bit / {audio.BitRate/1000:0} kbit/s";

                var audioStreamTitle = $"{streamIndex:0}: {audioCodec} ({audioCodecShort}) / {audioLangCode} ({audioLanguage}) / {audioDesc}";

                if (JobInfo.Input == InputType.InputAvi)
                {
                    audioPid += 1;
                }
                else
                {
                    audioPid = audioPid == 0 ? streamIndex : audioPid;
                }

                var aud = new AudioInfo
                {
                    Id            = audioPid,
                    Format        = audioCodecShort,
                    FormatProfile = audio.FormatProfile,
                    StreamId      = streamIndex,
                    LangCode      = audioLangCode,
                    OriginalId    = audioPid,
                    StreamKindId  = audioStreamKindID,
                    Delay         = audio.Delay,
                    Bitrate       = audio.BitRate,
                    SampleRate    = audio.SamplingRate,
                    ChannelCount  = audio.Channels,
                    BitDepth      = audio.BitDepth,
                    ShortLang     = audio.LanguageIso6391,
                    StreamSize    = audio.StreamSize,
                    Length        = mi.General.DurationTime.TimeOfDay.TotalSeconds,
                    IsHdStream    = audio.CompressionMode == "Lossless"
                };

                CreateNode(audioStreamTree, audioStreamTitle, aud);
            }

            audioStreamTree.IsChecked = audioStreamTree.Children.Count > 0;

            foreach (var sub in mi.Text)
            {
                streamIndex++;
                var subCodec        = sub.CodecIDInfo;
                var subCodecShort   = sub.Format;
                var subLangCode     = sub.LanguageIso6392;
                var subLanguage     = sub.LanguageFull;
                var subStreamKindID = sub.StreamKindID;

                var subStreamTitle = $"{streamIndex:0}: {subCodec} ({subCodecShort}) / {subLangCode} ({subLanguage})";

                var subInfo = new SubtitleInfo
                {
                    Id           = sub.ID,
                    StreamId     = streamIndex,
                    LangCode     = subLangCode,
                    Format       = subCodecShort,
                    StreamKindId = subStreamKindID,
                    Delay        = sub.Delay,
                    StreamSize   = sub.StreamSize
                };

                CreateNode(subStreamTree, subStreamTitle, subInfo);
            }

            foreach (var sub in mi.Image)
            {
                streamIndex++;
                var subCodec        = sub.CodecIDInfo;
                var subCodecShort   = sub.Format;
                var subLangCode     = sub.LanguageIso6392;
                var subLanguage     = sub.LanguageFull;
                var subStreamKindID = sub.StreamKindID;

                var subStreamTitle = $"{streamIndex:0}: {subCodec} ({subCodecShort}) / {subLangCode} ({subLanguage})";
                var subInfo        = new SubtitleInfo
                {
                    Id           = sub.ID,
                    StreamId     = streamIndex,
                    LangCode     = subLangCode,
                    Format       = subCodecShort,
                    StreamKindId = subStreamKindID,
                    Delay        = 0,
                    StreamSize   = sub.StreamSize
                };

                CreateNode(subStreamTree, subStreamTitle, subInfo);
            }

            subStreamTree.IsChecked = subStreamTree.Children.Count > 0;

            NotifyOfPropertyChange(() => Tree);
        }
示例#5
0
 private static MediaInfoContainer DoWorkHandler(string fileName)
 {
     return(MediaInfoContainer.GetMediaInfo(fileName));
 }
示例#6
0
        /// <summary>
        /// Determines Media Type of the input file
        /// </summary>
        /// <param name="pathToFile">Path to input file</param>
        /// <returns><see cref="InputType"/></returns>
        public InputType CheckFileType(string pathToFile)
        {
            MediaInfoContainer mi;

            try
            {
                mi = MediaInfoContainer.GetMediaInfo(pathToFile);
            }
            catch (TimeoutException ex)
            {
                Log.Error(ex);
                mi = new MediaInfoContainer();
            }

            var containerFormat = mi.General.Format;

            Log.Info($"General.FileName:                {mi.General.CompleteName}");
            Log.Info($"General.FileExtension:           {mi.General.FileExtension}");
            Log.Info($"General.Format:                  {mi.General.Format}");
            Log.Info($"General.FormatExtensions:        {mi.General.FormatExtensions}");
            Log.Info($"General.InternetMediaType:       {mi.General.InternetMediaType}");
            Log.Info($"General.EncodedApplication:      {mi.General.EncodedApplication}");
            Log.Info($"General.EncodedApplicationUrl:   {mi.General.EncodedApplicationUrl}");
            Log.Info($"General.EncodedLibrary:          {mi.General.EncodedLibrary}");
            Log.Info($"General.EncodedLibraryDate:      {mi.General.EncodedLibraryDate}");
            Log.Info($"General.EncodedLibraryName:      {mi.General.EncodedLibraryName}");
            Log.Info($"General.EncodedLibrarySettings:  {mi.General.EncodedLibrarySettings}");
            Log.Info($"General.EncodedLibraryVersion:   {mi.General.EncodedLibraryVersion}");
            Log.Info(string.Empty);

            foreach (var item in mi.Video)
            {
                Log.Info($"Video.ID:                        {item.ID:0}");
                Log.Info($"Video.StreamKindID:              {item.StreamKindID:0}");
                Log.Info($"Video.StreamKindPos:             {item.StreamKindPos:0}");
                Log.Info($"Video.CodecID:                   {item.CodecID}");
                Log.Info($"Video.CodecIDInfo:               {item.CodecIDInfo}");
                Log.Info($"Video.CodecIDURL:                {item.CodecIDUrl}");
                Log.Info($"Video.CodecIDDescription:        {item.CodecIDDescription}");
                Log.Info($"Video.InternetMediaType:         {item.InternetMediaType}");
                Log.Info($"Video.Format:                    {item.Format}");
                Log.Info($"Video.FormatProfile:             {item.FormatProfile}");
                Log.Info($"Video.FormatInfo:                {item.FormatInfo}");
                Log.Info($"Video.FormatVersion:             {item.FormatVersion}");
                Log.Info($"Video.MultiViewBaseProfile:      {item.MultiViewBaseProfile}");
                Log.Info($"Video.MultiViewCount:            {item.MultiViewCount}");
                Log.Info($"Video.DisplayAspectRatio:        {item.DisplayAspectRatio}");
                Log.Info($"Video.PixelAspectRatio:          {item.PixelAspectRatio}");
                Log.Info($"Video.BitrateMode:               {item.BitRateMode}");
                Log.Info($"Video.Bitrate:                   {item.BitRate:0}");
                Log.Info($"Video.BitrateNom:                {item.BitRateNom:0}");
                Log.Info($"Video.BitrateMin:                {item.BitRateMin:0}");
                Log.Info($"Video.BitrateMax:                {item.BitRateMax:0}");
                Log.Info($"Video.BitDepth:                  {item.BitDepth:0}");
                Log.Info($"Video.FrameRate:                 {item.FrameRate:0.###}".ToString(CultureInfo.InvariantCulture));
                Log.Info($"Video.FrameRateMax:              {item.FrameRateMax:0.###}".ToString(CultureInfo.InvariantCulture));
                Log.Info($"Video.FrameRateMin:              {item.FrameRateMin:0.###}".ToString(CultureInfo.InvariantCulture));
                Log.Info($"Video.FrameRateNom:              {item.FrameRateNom:0.###}".ToString(CultureInfo.InvariantCulture));
                Log.Info($"Video.FrameRateMode:             {item.FrameRateMode}");
                Log.Info($"Video.Height:                    {item.Height:0}");
                Log.Info($"Video.Width:                     {item.Width:0}");
                Log.Info($"Video.VideoSize:                 {item.VideoSize}");
                Log.Info($"Video.ScanType:                  {item.ScanType}");
                Log.Info($"Video.ScanOrder:                 {item.ScanOrder}");
                Log.Info($"Video.EncodedApplication:        {item.EncodedApplication}");
                Log.Info($"Video.EncodedApplicationUrl:     {item.EncodedApplicationUrl}");
                Log.Info($"Video.EncodedLibrary:            {item.EncodedLibrary}");
                Log.Info($"Video.EncodedLibraryDate:        {item.EncodedLibraryDate}");
                Log.Info($"Video.EncodedLibraryName:        {item.EncodedLibraryName}");
                Log.Info($"Video.EncodedLibrarySettings:    {item.EncodedLibrarySettings}");
                Log.Info($"Video.EncodedLibraryVersion:     {item.EncodedLibraryVersion}");
            }
            Log.Info(string.Empty);

            foreach (var item in mi.Audio)
            {
                Log.Info($"Audio.ID:                        {item.ID:0}");
                Log.Info($"Audio.StreamKindID:              {item.StreamKindID:0}");
                Log.Info($"Audio.StreamKindPos:             {item.StreamKindPos:0}");
                Log.Info($"Audio.CodecID:                   {item.CodecID}");
                Log.Info($"Audio.CodecIDInfo:               {item.CodecIDInfo}");
                Log.Info($"Audio.CodecIDURL:                {item.CodecIDUrl}");
                Log.Info($"Audio.CodecIDDescription:        {item.CodecIDDescription}");
                Log.Info($"Audio.Format:                    {item.Format}");
                Log.Info($"Audio.FormatProfile:             {item.FormatProfile}");
                Log.Info($"Audio.FormatInfo:                {item.FormatInfo}");
                Log.Info($"Audio.FormatVersion:             {item.FormatVersion}");
                Log.Info($"Audio.Channels:                  {item.Channels:0}");
                Log.Info($"Audio.ChannelsString:            {item.ChannelsString}");
                Log.Info($"Audio.BitrateMode:               {item.BitRateMode}");
                Log.Info($"Audio.Bitrate:                   {item.BitRate:0}");
                Log.Info($"Audio.BitrateNom:                {item.BitRateNom:0}");
                Log.Info($"Audio.BitrateMin:                {item.BitRateMin:0}");
                Log.Info($"Audio.BitrateMax:                {item.BitRateMax:0}");
                Log.Info($"Audio.BitDepth:                  {item.BitDepth:0}");
                Log.Info($"Audio.SamplingRate:              {item.SamplingRate:0}");
                Log.Info($"Audio.EncodedLibrary:            {item.EncodedLibrary}");
                Log.Info($"Audio.EncodedLibraryDate:        {item.EncodedLibraryDate}");
                Log.Info($"Audio.EncodedLibraryName:        {item.EncodedLibraryName}");
                Log.Info($"Audio.EncodedLibrarySettings:    {item.EncodedLibrarySettings}");
                Log.Info($"Audio.EncodedLibraryVersion:     {item.EncodedLibraryVersion}");
            }
            Log.Info(string.Empty);

            switch (containerFormat)
            {
            case "Matroska":
                return(InputType.InputMatroska);

            case "AVI":
                return(InputType.InputAvi);

            case "MPEG-4":
                return(InputType.InputMp4);

            case "BDAV":
            case "MPEG-TS":
                return(InputType.InputTs);

            case "Windows Media":
                return(InputType.InputWm);

            case "Flash Video":
                return(InputType.InputFlash);

            case "MPEG-PS":
                return(InputType.InputMpegps);

            case "WebM":
                return(InputType.InputWebM);

            case "OGG":
                return(InputType.InputOgg);
            }

            return(Path.GetExtension(pathToFile) == ".avs" ? InputType.InputAviSynth : InputType.InputUndefined);
        }
        /// <summary>
        /// Generates AviSynth script used for video encoding
        /// </summary>
        /// <param name="videoInfo">All video properties</param>
        /// <param name="changeFps">Defines whether framerate should be changed</param>
        /// <param name="targetFps">Sets target framerate</param>
        /// <param name="resizeTo">Sets target video resolution</param>
        /// <param name="stereoEncoding">Defines, which stereo encoding mode should be used</param>
        /// <param name="stereoVideoInfo">Sets all parameters for stereo encoding</param>
        /// <param name="isDvdResolution">Defines whether target resolution is used for DVD encoding</param>
        /// <param name="subtitleFile">Sets subtitle file for hardcoding into video</param>
        /// <param name="subtitleOnlyForced">Defines whether only forced captions should be hardcoded</param>
        /// <param name="skipScaling"></param>
        /// <returns>Path to AviSynth script</returns>
        public string Generate(VideoInfo videoInfo, bool changeFps, float targetFps, Size resizeTo,
                               StereoEncoding stereoEncoding, StereoVideoInfo stereoVideoInfo, bool isDvdResolution,
                               string subtitleFile, bool subtitleOnlyForced, bool skipScaling)
        {
            var sb = new StringBuilder();

            var mtUseful = (videoInfo.Interlaced && _appConfig.UseHQDeinterlace) || changeFps;

            var useStereo = stereoEncoding != StereoEncoding.None && stereoVideoInfo.RightStreamId > -1;

            // support for multithreaded AviSynth
            if (_appConfig.UseAviSynthMT && mtUseful)
            {
                sb.AppendLine("SetMTMode(2,0)");
                sb.AppendLine("SetMemoryMax(512)");
            }

            var pluginList = new List <string> {
                "ffms2.dll"
            };
            var scriptList = new List <string>();

            //loading plugins

            if (changeFps || (videoInfo.Interlaced && _appConfig.UseHQDeinterlace))
            {
                pluginList.Add("mvtools2.dll");
            }

            if (videoInfo.Interlaced && _appConfig.UseHQDeinterlace)
            {
                pluginList.Add(_appConfig.LastAviSynthVer.StartsWith("2.5")
                                    ? "mt_masktools-25.dll"
                                    : "mt_masktools-26.dll");
                pluginList.Add("nnedi3.dll");
                pluginList.Add("RemoveGrainSSE2.dll");
                pluginList.Add("RepairSSE2.dll");

                scriptList.Add("QTGMC-3.32.avsi");
            }
            else if (videoInfo.Interlaced)
            {
                pluginList.Add("Decomb.dll");
            }

            if (useStereo)
            {
                pluginList.Add("H264StereoSource.dll");
            }

            if (!string.IsNullOrEmpty(subtitleFile) && File.Exists(subtitleFile))
            {
                switch (Path.GetExtension(subtitleFile))
                {
                case "sup":
                    pluginList.Add("SupTitle.dll");
                    break;

                case "ass":
                case "ssa":
                case "srt":
                    pluginList.Add("VSFilter.dll");
                    break;
                }
            }

            // generate plugin and script loading
            foreach (var plugin in pluginList)
            {
                sb.AppendLine($"LoadPlugin(\"{Path.Combine(_appConfig.AvsPluginsPath, plugin)}\")");
            }
            foreach (var script in scriptList)
            {
                sb.AppendLine($"Import(\"{Path.Combine(_appConfig.AvsPluginsPath, script)}\")");
            }

            //generate rest of the script

            // calculate framerate numerator & denominator
            if (videoInfo.Fps <= 0)
            {
                var mi = new MediaInfoContainer();
                try
                {
                    mi = GenHelper.GetMediaInfo(videoInfo.TempFile);
                }
                catch (TimeoutException ex)
                {
                    Log.Error(ex);
                    mi = new MediaInfoContainer();
                }
                finally
                {
                    if (mi.Video.Count > 0)
                    {
                        videoInfo.Fps = mi.Video[0].FrameRate;
                        VideoHelper.GetFpsNumDenom(videoInfo.Fps, out videoInfo.FrameRateEnumerator,
                                                   out videoInfo.FrameRateDenominator);

                        if (videoInfo.FrameRateEnumerator == 0)
                        {
                            videoInfo.FrameRateEnumerator  = (int)Math.Round(videoInfo.Fps) * 1000;
                            videoInfo.FrameRateDenominator =
                                (int)(Math.Round(Math.Ceiling(videoInfo.Fps) - Math.Floor(videoInfo.Fps)) + 1000);
                        }
                    }
                }
            }
            sb.Append($"FFVideoSource(\"{videoInfo.TempFile}\",");

            if (videoInfo.FrameRateEnumerator > 0 && videoInfo.FrameRateDenominator > 0)
            {
                sb.Append($"fpsnum={videoInfo.FrameRateEnumerator:0},fpsden={videoInfo.FrameRateDenominator:0},");
            }

            var threadCount = _appConfig.LimitDecoderThreads ? 1 : 0;

            sb.Append($"threads={threadCount:0})");
            sb.AppendLine();

            var stereoVar = string.Empty;

            if (useStereo)
            {
                var configFile = GenerateStereoSourceConfig(stereoVideoInfo);
                sb.AppendLine($"VideoRight = H264StereoSource(\"{configFile}\",{videoInfo.FrameCount - 50:0})");
                StereoConfigFile = configFile;
                stereoVar        = "VideoRight";
            }

            // deinterlace video source
            if (videoInfo.Interlaced)
            {
                if (_appConfig.UseHQDeinterlace)
                {
                    sb.AppendLine("QTGMC(Preset=\"Slower\")");
                }
                else
                {
                    sb.AppendLine("ConvertToYUY2(interlaced=true)");
                    sb.AppendLine("Telecide(post=4)");
                    sb.AppendLine("Crop(4, 0, -4, 0)");
                    sb.AppendLine("AddBorders(4, 0, 4, 0)");
                    sb.AppendLine("ConvertToYV12()");
                }
            }

            // hardcode subtitles
            if (!string.IsNullOrEmpty(subtitleFile) && File.Exists(subtitleFile))
            {
                switch (Path.GetExtension(subtitleFile))
                {
                case "sup":
                    var subForced = subtitleOnlyForced ? "true" : "false";
                    sb.Append($"SupTitle(\"{subtitleFile}\", forcedOnly={subForced})");
                    break;

                case "ass":
                case "ssa":
                case "srt":
                    sb.Append($"TextSub(\"{subtitleFile}\")");
                    break;
                }

                sb.AppendLine();
            }

            // video cropping
            if (!videoInfo.CropRect.IsEmpty && !skipScaling)
            {
                int temp;

                Math.DivRem(videoInfo.CropRect.X, 2, out temp);
                videoInfo.CropRect.X += temp;
                Math.DivRem(videoInfo.CropRect.Y, 2, out temp);
                videoInfo.CropRect.Y += temp;
                Math.DivRem(videoInfo.CropRect.Width, 2, out temp);
                videoInfo.CropRect.Width += temp;
                Math.DivRem(videoInfo.CropRect.Height, 2, out temp);
                videoInfo.CropRect.Height += temp;

                videoInfo.Height = videoInfo.CropRect.Height;
                videoInfo.Width  = videoInfo.CropRect.Width;

                if ((videoInfo.CropRect.X > 0) || (videoInfo.CropRect.Y > 0) || (videoInfo.CropRect.Width < videoInfo.Width) ||
                    (videoInfo.CropRect.Height < videoInfo.Height))
                {
                    sb.Append(useStereo ? "CroppedVideoRight = Crop(VideoRight," : "Crop(");

                    sb.Append($"{videoInfo.CropRect.Left:0},{videoInfo.CropRect.Top:0},");
                    sb.Append($"{videoInfo.CropRect.Width:0},{videoInfo.CropRect.Height:0})");
                    sb.AppendLine();

                    if (useStereo)
                    {
                        stereoVar = "CroppedVideoRight";
                    }
                }
            }

            // Side-By-Side stereo encoding
            if (!string.IsNullOrEmpty(stereoVar))
            {
                switch (stereoEncoding)
                {
                case StereoEncoding.FullSideBySideLeft:
                case StereoEncoding.HalfSideBySideLeft:
                    sb.AppendLine($"StackHorizontal(last,{stereoVar})");
                    break;

                case StereoEncoding.FullSideBySideRight:
                case StereoEncoding.HalfSideBySideRight:
                    sb.AppendLine($"StackHorizontal({stereoVar},last)");
                    break;
                }
                sb.AppendLine("ConvertToYV12()");
            }

            var calculatedHeight = videoInfo.Height;
            var calculatedWidth  = videoInfo.Width;

            var borderRight  = 0;
            var borderLeft   = 0;
            var borderBottom = 0;
            var borderTop    = 0;
            var addBorders   = false;

            // video resizing
            if (!resizeTo.IsEmpty && (resizeTo.Height != videoInfo.Height || resizeTo.Width != videoInfo.Width) && !skipScaling)
            {
                // aspect ratios

                var toAr   = (float)Math.Round(resizeTo.Width / (float)resizeTo.Height, 3);
                var fromAr = videoInfo.AspectRatio;
                var mod    = 1f;

                calculatedWidth = resizeTo.Width;

                if (fromAr > toAr) // source aspectratio higher than target aspectratio
                {
                    if (isDvdResolution)
                    {
                        calculatedHeight = (int)(calculatedWidth / fromAr);
                        if (calculatedHeight > resizeTo.Height)
                        {
                            calculatedHeight = resizeTo.Height;
                        }
                        calculatedWidth = 720;
                    }
                    else
                    {
                        calculatedWidth  = resizeTo.Width;
                        calculatedHeight = (int)(calculatedWidth / fromAr);
                    }

                    int temp;

                    Math.DivRem(calculatedWidth, 2, out temp);
                    calculatedWidth += temp;
                    Math.DivRem(calculatedHeight, 2, out temp);
                    calculatedHeight += temp;

                    if (calculatedHeight != resizeTo.Height)
                    {
                        addBorders = true;
                        var borderHeight = resizeTo.Height - calculatedHeight;
                        borderTop = borderHeight / 2;
                        Math.DivRem(borderTop, 2, out temp);
                        borderTop   += temp;
                        borderBottom = borderHeight - borderTop;
                    }
                }
                else if (Math.Abs(fromAr - toAr) <= 0)  // source and target aspectratio equals
                {
                    if (isDvdResolution)
                    {
                        calculatedHeight = (int)(calculatedWidth / fromAr);
                        calculatedWidth  = 720;
                        if (calculatedHeight > resizeTo.Height)
                        {
                            calculatedHeight = resizeTo.Height;
                        }
                    }
                    else
                    {
                        calculatedWidth  = resizeTo.Width;
                        calculatedHeight = (int)(calculatedWidth / toAr);
                    }

                    int temp;

                    Math.DivRem(calculatedWidth, 2, out temp);
                    calculatedWidth += temp;
                    Math.DivRem(calculatedHeight, 2, out temp);
                    calculatedHeight += temp;

                    if (calculatedHeight != resizeTo.Height)
                    {
                        addBorders = true;
                        var borderHeight = resizeTo.Height - calculatedHeight;
                        borderTop = borderHeight / 2;
                        Math.DivRem(borderTop, 2, out temp);
                        borderTop   += temp;
                        borderBottom = borderHeight - borderTop;
                    }
                }
                else
                {
                    if (fromAr > 1.4f && isDvdResolution)  // source aspectratio not 4:3, encoding for dvd resolution
                    {
                        mod = 720f / resizeTo.Width;

                        calculatedHeight = (int)(calculatedWidth / fromAr);
                        if (calculatedHeight > resizeTo.Height)
                        {
                            calculatedHeight = resizeTo.Height;
                            calculatedWidth  = (int)(calculatedHeight * fromAr * mod);
                        }
                        else
                        {
                            calculatedWidth = 720;
                        }
                    }
                    else if (isDvdResolution)
                    {
                        calculatedHeight = resizeTo.Height;
                        calculatedWidth  = (int)(calculatedHeight * fromAr);
                    }
                    else
                    {
                        calculatedHeight = resizeTo.Height;
                    }

                    int temp;
                    Math.DivRem(calculatedWidth, 2, out temp);
                    calculatedWidth += temp;
                    Math.DivRem(calculatedHeight, 2, out temp);
                    calculatedHeight += temp;

                    if (Math.Abs(toAr - 1.778f) <= 0)     // aspectratio 16:9
                    {
                        addBorders = true;
                        var borderHeight = resizeTo.Height - calculatedHeight;
                        borderTop = borderHeight / 2;
                        Math.DivRem(borderTop, 2, out temp);
                        borderTop   += temp;
                        borderBottom = borderHeight - borderTop;

                        var borderWidth = (int)((resizeTo.Width * mod) - calculatedWidth);
                        borderLeft = borderWidth / 2;
                        Math.DivRem(borderLeft, 2, out temp);
                        borderLeft += temp;
                        borderRight = borderWidth - borderLeft;
                    }
                    else if (calculatedWidth != resizeTo.Width)
                    {
                        addBorders = true;
                        var borderWidth = resizeTo.Width - calculatedWidth;
                        borderLeft = borderWidth / 2;
                        Math.DivRem(borderLeft, 2, out temp);
                        borderLeft += temp;
                        borderRight = borderWidth - borderLeft;

                        var borderHeight = resizeTo.Height - calculatedHeight;
                        borderTop = borderHeight / 2;
                        Math.DivRem(borderTop, 2, out temp);
                        borderTop   += temp;
                        borderBottom = borderHeight - borderTop;
                    }
                }
            }

            // apply resize filter
            if (calculatedHeight != videoInfo.Height || calculatedWidth != videoInfo.Width ||
                (stereoEncoding == StereoEncoding.HalfSideBySideLeft ||
                 stereoEncoding == StereoEncoding.HalfSideBySideRight &&
                 useStereo) && !skipScaling)
            {
                if (calculatedHeight < videoInfo.Height || calculatedWidth < videoInfo.Width ||
                    (stereoEncoding == StereoEncoding.HalfSideBySideLeft ||
                     stereoEncoding == StereoEncoding.HalfSideBySideRight &&
                     useStereo))
                {
                    sb.Append("BicubicResize");
                }
                else
                {
                    sb.Append("Lanczos4Resize");
                }

                sb.Append($"({calculatedWidth:0},{calculatedHeight:0})");
                sb.AppendLine();
            }

            // add borders if needed
            if (addBorders && (borderLeft > 0 || borderRight > 0 || borderTop > 0 || borderBottom > 0) && !skipScaling)
            {
                sb.AppendLine($"AddBorders({borderLeft:0},{borderTop:0},{borderRight:0},{borderBottom:0})");
            }

            // change framerate
            if (changeFps)
            {
                int fpsnum;
                int fpsden;

                // get framerate numerator & denominator for target framerate
                VideoHelper.GetFpsNumDenom(targetFps, out fpsnum, out fpsden);

                // source is 23.976 or 24 fps
                if (videoInfo.FrameRateEnumerator == 24000 && (videoInfo.FrameRateDenominator == 1001 || videoInfo.FrameRateDenominator == 1000))
                {
                    if (fpsnum == 30000 && fpsden == 1001)
                    {
                        // 3:2 pulldown / telecine
                        sb.AppendLine("AssumeFrameBased()");
                        sb.AppendLine("SeparateFields()");
                        sb.AppendLine("SelectEvery(8, 0, 1, 2, 3, 2, 5, 4, 7, 6, 7)");
                        sb.AppendLine("Weave()");
                    }
                    else if (fpsnum == 25000 && fpsden == 1000)
                    {
                        // convert to 25 fps
                        sb.AppendLine("ConvertToYUY2()");
                        sb.AppendLine("ConvertFPS(50)");
                        sb.AppendLine("AssumeTFF()");
                        sb.AppendLine("SeparateFields()");
                        sb.AppendLine("SelectEvery(4,0,3)");
                        sb.AppendLine("Weave()");
                        sb.AppendLine("ConvertToYV12()");
                    }
                }
                // source is 30fps
                else if (videoInfo.FrameRateEnumerator == 30000)
                {
                    sb.AppendLine("ConvertToYUY2()");
                    sb.AppendLine("DoubleWeave()");
                    sb.AppendLine($"ConvertFPS(numerator={fpsnum * 2:0},denominator={fpsden:0})");
                    sb.AppendLine("SelectEven()");
                    sb.AppendLine("ConvertToYV12()");
                }
                // source is 25fps
                else if (videoInfo.FrameRateEnumerator == 25000 && videoInfo.FrameRateDenominator == 1000)
                {
                    if ((fpsnum == 30000 || fpsnum == 24000) && fpsden == 1001)
                    {
                        sb.AppendLine("ConvertToYUY2()");
                        sb.AppendLine("ConvertFPS(numerator=48000,denominator=1001");
                        if (fpsnum == 30000)
                        {
                            sb.AppendLine("AssumeFrameBased()");
                            sb.AppendLine("SeparateFields()");
                            sb.AppendLine("SelectEvery(8, 0, 1, 2, 3, 2, 5, 4, 7, 6, 7)");
                        }
                        else
                        {
                            sb.AppendLine("AssumeTFF()");
                            sb.AppendLine("SeparateFields()");
                            sb.AppendLine("SelectEven()");
                        }
                        sb.AppendLine("Weave()");
                        sb.AppendLine("ConvertToYV12()");
                    }
                }
                // every other framerate
                else
                {
                    // very slow framerate interpolation
                    sb.AppendLine("super = MSuper(pel=2)");
                    sb.AppendLine("backward_vec = MAnalyse(super, overlap=4, isb = true, search=3)");
                    sb.AppendLine("forward_vec = MAnalyse(super, overlap=4, isb = false, search=3)");
                    sb.Append($"MFlowFps(super, backward_vec, forward_vec, num={fpsnum:0}, den={fpsden:0})");
                }

                sb.AppendLine();
            }

            // multithreaded avisynth
            if (!_appConfig.UseAviSynthMT || !mtUseful)
            {
                return(WriteScript(sb.ToString()));
            }

            sb.AppendLine("SetMTMode(1)");
            sb.AppendLine("GetMTMode(false) > 0 ? distributor() : last");

            return(WriteScript(sb.ToString()));
        }
示例#8
0
        private static InputType CheckFileType(string pathToFile)
        {
            MediaInfoContainer mi;

            try
            {
                mi = GetMediaInfo(pathToFile);
            }
            catch (TimeoutException ex)
            {
                Log.Error(ex);
                mi = new MediaInfoContainer();
            }


            string containerFormat = mi.General.Format;

            Log.InfoFormat(AppSettings.CInfo, "General.FileName:            {0:s}", mi.General.CompleteName);
            Log.InfoFormat(AppSettings.CInfo, "General.FileExtension:       {0:s}", mi.General.FileExtension);
            Log.InfoFormat(AppSettings.CInfo, "General.Format:              {0:s}", mi.General.Format);
            Log.InfoFormat(AppSettings.CInfo, "General.FormatExtensions:    {0:s}", mi.General.FormatExtensions);
            Log.InfoFormat(AppSettings.CInfo, "General.InternetMediaType:   {0:s}", mi.General.InternetMediaType);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedApplication:  {0:s}", mi.General.EncodedApplication);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedApplicationUrl:{0:s}", mi.General.EncodedApplicationUrl);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedLibrary:      {0:s}", mi.General.EncodedLibrary);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedLibraryDate:  {0:s}", mi.General.EncodedLibraryDate);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedLibraryName:  {0:s}", mi.General.EncodedLibraryName);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedLibrarySettings: {0:s}", mi.General.EncodedLibrarySettings);
            Log.InfoFormat(AppSettings.CInfo, "General.EncodedLibraryVersion: {0:s}", mi.General.EncodedLibraryVersion);
            Log.Info(String.Empty);

            foreach (MediaInfoContainer.VideoStreamInfo item in mi.Video)
            {
                Log.InfoFormat(AppSettings.CInfo, "Video.ID:                 {0:g}", item.ID);
                Log.InfoFormat(AppSettings.CInfo, "Video.StreamKindID:       {0:g}", item.StreamKindID);
                Log.InfoFormat(AppSettings.CInfo, "Video.StreamKindPos:      {0:g}", item.StreamKindPos);
                Log.InfoFormat(AppSettings.CInfo, "Video.CodecID:            {0:s}", item.CodecID);
                Log.InfoFormat(AppSettings.CInfo, "Video.CodecIDInfo:        {0:s}", item.CodecIDInfo);
                Log.InfoFormat(AppSettings.CInfo, "Video.CodecIDURL:         {0:s}", item.CodecIDUrl);
                Log.InfoFormat(AppSettings.CInfo, "Video.CodecIDDescription: {0:s}", item.CodecIDDescription);
                Log.InfoFormat(AppSettings.CInfo, "Video.InternetMediaType:  {0:s}", item.InternetMediaType);
                Log.InfoFormat(AppSettings.CInfo, "Video.Format:             {0:s}", item.Format);
                Log.InfoFormat(AppSettings.CInfo, "Video.FormatProfile:      {0:s}", item.FormatProfile);
                Log.InfoFormat(AppSettings.CInfo, "Video.FormatInfo:         {0:s}", item.FormatInfo);
                Log.InfoFormat(AppSettings.CInfo, "Video.FormatVersion:      {0:s}", item.FormatVersion);
                Log.InfoFormat(AppSettings.CInfo, "Video.MultiViewBaseProfile: {0:s}", item.MultiViewBaseProfile);
                Log.InfoFormat(AppSettings.CInfo, "Video.MultiViewCount:     {0:s}", item.MultiViewCount);
                Log.InfoFormat(AppSettings.CInfo, "Video.DisplayAspectRatio: {0:s}", item.DisplayAspectRatio);
                Log.InfoFormat(AppSettings.CInfo, "Video.PixelAspectRatio:   {0:g}", item.PixelAspectRatio);
                Log.InfoFormat(AppSettings.CInfo, "Video.BitrateMode:        {0:s}", item.BitRateMode);
                Log.InfoFormat(AppSettings.CInfo, "Video.Bitrate:            {0:g}", item.BitRate);
                Log.InfoFormat(AppSettings.CInfo, "Video.BitrateNom:         {0:g}", item.BitRateNom);
                Log.InfoFormat(AppSettings.CInfo, "Video.BitrateMin:         {0:g}", item.BitRateMin);
                Log.InfoFormat(AppSettings.CInfo, "Video.BitrateMax:         {0:g}", item.BitRateMax);
                Log.InfoFormat(AppSettings.CInfo, "Video.BitDepth:           {0:g}", item.BitDepth);
                Log.InfoFormat(AppSettings.CInfo, "Video.FrameRate:          {0:g}", item.FrameRate);
                Log.InfoFormat(AppSettings.CInfo, "Video.FrameRateMax:       {0:g}", item.FrameRateMax);
                Log.InfoFormat(AppSettings.CInfo, "Video.FrameRateMin:       {0:g}", item.FrameRateMin);
                Log.InfoFormat(AppSettings.CInfo, "Video.FrameRateNom:       {0:g}", item.FrameRateNom);
                Log.InfoFormat(AppSettings.CInfo, "Video.FrameRateMode:      {0:s}", item.FrameRateMode);
                Log.InfoFormat(AppSettings.CInfo, "Video.Height:             {0:g}", item.Height);
                Log.InfoFormat(AppSettings.CInfo, "Video.Width:              {0:g}", item.Width);
                Log.InfoFormat(AppSettings.CInfo, "Video.VideoSize:          {0:s}", item.VideoSize.ToString());
                Log.InfoFormat(AppSettings.CInfo, "Video.ScanType:           {0:s}", item.ScanType);
                Log.InfoFormat(AppSettings.CInfo, "Video.ScanOrder:          {0:g}", item.ScanOrder);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedApplication: {0:s}", item.EncodedApplication);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedApplicationUrl: {0:s}", item.EncodedApplicationUrl);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedLibrary:     {0:s}", item.EncodedLibrary);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedLibraryDate: {0:s}", item.EncodedLibraryDate);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedLibraryName: {0:s}", item.EncodedLibraryName);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedLibrarySettings: {0:s}", item.EncodedLibrarySettings);
                Log.InfoFormat(AppSettings.CInfo, "Video.EncodedLibraryVersion: {0:s}", item.EncodedLibraryVersion);
            }
            Log.Info(String.Empty);

            foreach (MediaInfoContainer.AudioStreamInfo item in mi.Audio)
            {
                Log.InfoFormat(AppSettings.CInfo, "Audio.ID:                 {0:g}", item.ID);
                Log.InfoFormat(AppSettings.CInfo, "Audio.StreamKindID:       {0:g}", item.StreamKindID);
                Log.InfoFormat(AppSettings.CInfo, "Audio.StreamKindPos:      {0:g}", item.StreamKindPos);
                Log.InfoFormat(AppSettings.CInfo, "Audio.CodecID:            {0:s}", item.CodecID);
                Log.InfoFormat(AppSettings.CInfo, "Audio.CodecIDInfo:        {0:s}", item.CodecIDInfo);
                Log.InfoFormat(AppSettings.CInfo, "Audio.CodecIDURL:         {0:s}", item.CodecIDUrl);
                Log.InfoFormat(AppSettings.CInfo, "Audio.CodecIDDescription: {0:s}", item.CodecIDDescription);
                Log.InfoFormat(AppSettings.CInfo, "Audio.Format:             {0:s}", item.Format);
                Log.InfoFormat(AppSettings.CInfo, "Audio.FormatProfile:      {0:s}", item.FormatProfile);
                Log.InfoFormat(AppSettings.CInfo, "Audio.FormatInfo:         {0:s}", item.FormatInfo);
                Log.InfoFormat(AppSettings.CInfo, "Audio.FormatVersion:      {0:s}", item.FormatVersion);
                Log.InfoFormat(AppSettings.CInfo, "Audio.Channels:           {0:g}", item.Channels);
                Log.InfoFormat(AppSettings.CInfo, "Audio.ChannelsString:     {0:s}", item.ChannelsString);
                Log.InfoFormat(AppSettings.CInfo, "Audio.BitrateMode:        {0:s}", item.BitRateMode);
                Log.InfoFormat(AppSettings.CInfo, "Audio.Bitrate:            {0:g}", item.BitRate);
                Log.InfoFormat(AppSettings.CInfo, "Audio.BitrateNom:         {0:g}", item.BitRateNom);
                Log.InfoFormat(AppSettings.CInfo, "Audio.BitrateMin:         {0:g}", item.BitRateMin);
                Log.InfoFormat(AppSettings.CInfo, "Audio.BitrateMax:         {0:g}", item.BitRateMax);
                Log.InfoFormat(AppSettings.CInfo, "Audio.BitDepth:           {0:g}", item.BitDepth);
                Log.InfoFormat(AppSettings.CInfo, "Audio.SamplingRate:       {0:g}", item.SamplingRate);
                Log.InfoFormat(AppSettings.CInfo, "Audio.EncodedLibrary:     {0:s}", item.EncodedLibrary);
                Log.InfoFormat(AppSettings.CInfo, "Audio.EncodedLibraryDate: {0:s}", item.EncodedLibraryDate);
                Log.InfoFormat(AppSettings.CInfo, "Audio.EncodedLibraryName: {0:s}", item.EncodedLibraryName);
                Log.InfoFormat(AppSettings.CInfo, "Audio.EncodedLibrarySettings: {0:s}", item.EncodedLibrarySettings);
                Log.InfoFormat(AppSettings.CInfo, "Audio.EncodedLibraryVersion: {0:s}", item.EncodedLibraryVersion);
            }
            Log.Info(String.Empty);

            switch (containerFormat)
            {
            case "Matroska":
                return(InputType.InputMatroska);

            case "AVI":
                return(InputType.InputAvi);

            case "MPEG-4":
                return(InputType.InputMp4);

            case "BDAV":
            case "MPEG-TS":
                return(InputType.InputTs);

            case "Windows Media":
                return(InputType.InputWm);

            case "Flash Video":
                return(InputType.InputFlash);

            case "MPEG-PS":
                return(InputType.InputMpegps);

            case "WebM":
                return(InputType.InputWebM);

            case "OGG":
                return(InputType.InputOgg);
            }

            return(Path.GetExtension(pathToFile) == ".avs" ? InputType.InputAviSynth : InputType.InputUndefined);
        }