Esempio n. 1
0
        /// <summary>
        /// Gets an AviSynth clip information by running a script that outputs the frame count to a file.
        /// </summary>
        /// <param name="source">The AviSynth script to get information about.</param>
        /// <param name="options">The options that control the behaviors of the process.</param>
        /// <returns>The frame count.</returns>
        public static long GetFrameCount(string source, ProcessStartOptions options)
        {
            if (!File.Exists(source))
            {
                return(0);
            }
            string TempScriptBase = Path.ChangeExtension(source, null);
            string TempScript     = PathManager.GetTempFile("avs");
            string TempResult     = Path.ChangeExtension(TempScript, "txt");

            AviSynthScriptBuilder Script;

            if (source.ToLower().EndsWith(".avs"))
            {
                // Read source script and remove MT. Also remove Deshaker if present.
                string FileContent = File.ReadAllText(source);
                FileContent.Replace(Environment.NewLine + "Deshaker", Environment.NewLine + "#Deshaker");
                Script = new AviSynthScriptBuilder(FileContent);
                Script.RemoveMT();
            }
            else
            {
                // Generate script to read media file.
                Script = new AviSynthScriptBuilder();
                Script.AddPluginPath();
                Script.OpenDirect(source, false);
            }
            // Get frame count.
            Script.AppendLine();
            Script.AppendLine(@"WriteFileStart(""{0}"", ""FrameRate""{1}""Framecount"")", TempResult, @", """""" "","" """""", ");
            Script.AppendLine("Trim(0,-1)");
            Script.WriteToFile(TempScript);

            // Run script.
            FFmpegProcess Worker = new FFmpegProcess(options);

            Worker.RunAvisynth(TempScript);

            // Read frame count
            long Result = 0;

            if (File.Exists(TempResult))
            {
                string   FileString = File.ReadAllText(TempResult);
                string[] FileValues = FileString.Split(',');
                try {
                    //Result.FrameRate = float.Parse(FileValues[0], CultureInfo.InvariantCulture);
                    Result = int.Parse(FileValues[1]);
                }
                catch {
                }
            }

            // Delete temp files.
            File.Delete(TempScript);
            File.Delete(TempResult);

            return(Result);
        }
        /// <summary>
        /// Returns whether the GPU supports the latest version of KNLMeans with OpenCL 1.2
        /// </summary>
        /// <param name="supports11">If true, it will instead test whether the GPU supports OpenCL 1.1</param>
        /// <returns>True if OpenCL is supported.</returns>
        private static bool GpuSupportsOpenCL(bool version12)
        {
            string TempScript = Path.Combine(Path.GetTempPath(), "GpuSupportsOpenCL.avs");
            string TempResult = Path.Combine(Path.GetTempPath(), "GpuSupportsOpenCL.txt");

            AviSynthScriptBuilder Script = new AviSynthScriptBuilder();

            Script.AddPluginPath();
            Script.LoadPluginDll(string.Format("KNLMeansCL{0}.dll", version12 ? "" : "-6.11"));
            Script.AppendLine(@"colorbars(pixel_type = ""yv12"").killaudio().trim(1, 1)");
            Script.AppendLine("Result = true");
            Script.AppendLine("try {");
            Script.AppendLine(@"KNLMeansCL(device_type=""GPU"")");
            Script.AppendLine("} catch(error_msg) {");
            Script.AppendLine("Result = false");
            Script.AppendLine("}");
            Script.AppendLine(@"WriteFileStart(""{0}"", string(Result))", TempResult);
            Script.WriteToFile(TempScript);

            // Run script.
            FFmpegProcess Worker = new FFmpegProcess();

            Worker.Options.Timeout     = TimeSpan.FromSeconds(10);
            Worker.Options.DisplayMode = FFmpegDisplayMode.ErrorOnly;
            Worker.RunAvisynth(TempScript);

            // Read frame count
            bool Result = false;

            if (File.Exists(TempResult))
            {
                string FileString = File.ReadAllText(TempResult);
                try {
                    Result = bool.Parse(FileString.TrimEnd());
                } catch {
                    Result = false;
                }
            }

            // Delete temp files.
            File.Delete(TempScript);
            File.Delete(TempResult);

            return(Result);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates an AviSynth script that will auto-pitch the audio to 432hz. You then open this script file in the video player instead of directly opening the file.
        /// </summary>
        /// <param name="inputFile">The video to play.</param>
        /// <param name="infoReader">An object to read media information.</param>
        public static void CreateScript(string inputFile, FFmpegProcess infoReader, string scriptLocation)
        {
            bool AviSynthPlus            = AvisynthEnv.GetAvisynthVersion() == AvisynthVersion.AviSynthPlus;
            AviSynthScriptBuilder Script = new AviSynthScriptBuilder();

            Script.AddPluginPath();
            if (AviSynthPlus)
            {
                //Script.AppendLine(@"SetFilterMTMode(""DEFAULT_MT_MODE"",2)");
                //Script.AppendLine(@"SetFilterMTMode(""LWLibavVideoSource"",3)");
                //Script.AppendLine(@"SetFilterMTMode(""LWLibavAudioSource"",3)");
                bool IsAudio = PathManager.AudioExtensions.Contains(Path.GetExtension(inputFile).ToLower());
                Script.OpenDirect(inputFile, infoReader.AudioStream != null, !IsAudio && infoReader.VideoStream != null);
                if (IsAudio)
                {
                    Script.AppendLine("AudioDub(BlankClip(Last, width=8, height=8), Last)");
                }
                Script.AppendLine("Preroll(int(FrameRate*3))");
                // This causes a slight audio delay in AviSynth 2.6
                Script.LoadPluginDll("TimeStretch.dll");
                Script.AppendLine("ResampleAudio(48000)");
                Script.AppendLine("TimeStretchPlugin(pitch = 100.0 * 0.98181819915771484)");
                //Script.AppendLine("Prefetch({0})", CPU);
            }
            else
            {
                int CPU = Environment.ProcessorCount / 2;
                Script.AppendLine("SetMTMode(3,{0})", CPU);
                Script.OpenDirect(inputFile, infoReader.AudioStream != null, infoReader.VideoStream != null);
                Script.AppendLine("SetMTMode(2)");
                Script.AppendLine("Preroll(int(FrameRate*3))");
                //Script.AppendLine("Loop(int(FrameRate/2), 0, 0)");
                //Script.LoadPluginAvsi("UUSize4.avsi");
                //Script.AppendLine("UUSize4(mod=4)");
                // This slightly slows down playback speed but audio stays in sync
                Script.AppendLine("V = AssumeFPS(432.0 / 440.0 * FrameRate)");
                Script.AppendLine("A = AssumeSampleRate(int(432.0 / 440.0 * AudioRate))");
                Script.AppendLine("AudioDub(V, A)");
            }

            Script.Cleanup();
            Script.WriteToFile(scriptLocation);
        }