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);
        }
Esempio n. 2
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);
        }