Esempio n. 1
0
        /// <summary>
        /// Moves specified settings file as preview files.
        /// </summary>
        /// <param name="settings">The settings to use for re-encoding.</param>
        public async Task MovePreviewFilesAsync(MediaEncoderSettings settings)
        {
            await DeletePreviewFilesAsync();

            if (settings.ConvertToAvi)
            {
                File.Move(settings.InputFile, PathManager.PreviewSourceFile);
            }
            if (settings.Deshaker && File.Exists(settings.DeshakerLog))
            {
                File.Move(settings.DeshakerLog, PathManager.PreviewDeshakerLog);
            }
            if (!string.IsNullOrEmpty(settings.CustomScript) && (settings.ConvertToAvi || settings.Deshaker))
            {
                AviSynthScriptBuilder Script = new AviSynthScriptBuilder(settings.CustomScript);
                if (settings.ConvertToAvi)
                {
                    Script.Replace(Script.GetAsciiPath(settings.InputFile), Script.GetAsciiPath(PathManager.PreviewSourceFile));
                }
                if (settings.Deshaker)
                {
                    Script.Replace(Script.GetAsciiPath(settings.DeshakerLog), Script.GetAsciiPath(PathManager.PreviewDeshakerLog));
                }
                settings.CustomScript = Script.Script;
            }
            settings.Save(PathManager.PreviewSettingsFile);
        }
Esempio n. 2
0
        public void PrepareJobFiles(MediaEncoderSettings settings)
        {
            settings.JobIndex = ++JobIndex;
            // Files must be prepared before adding to queue so that user can replace preview files.
            PathManager.DeleteJobFiles(settings.JobIndex);
            File.Delete(PathManager.PreviewScriptFile);
            File.Delete(PathManager.PreviewSettingsFile);
            if (settings.ConvertToAvi)
            {
                PathManager.SafeMove(PathManager.PreviewSourceFile, settings.InputFile);
            }
            if (settings.Deshaker)
            {
                PathManager.SafeMove(PathManager.PreviewDeshakerLog, settings.DeshakerLog);
            }
            settings.Save(settings.SettingsFile);
            AviSynthScriptBuilder Script = new AviSynthScriptBuilder(settings.CustomScript);

            if (Script.IsEmpty)
            {
                Script = MediaEncoderScript.GenerateVideoScript(settings, settings.InputFile, false, true);
            }
            else
            {
                Script.Replace(Script.GetAsciiPath(PathManager.PreviewSourceFile), Script.GetAsciiPath(settings.InputFile));
                Script.Replace(Script.GetAsciiPath(PathManager.PreviewDeshakerLog), Script.GetAsciiPath(settings.DeshakerLog));
            }
            Script.WriteToFile(settings.ScriptFile);
            // if (settings.DeshakerSettings.PrescanAction == PrescanType.Full)
        }
        /// <summary>
        /// Saves the audio output of specified script into a WAV file.
        /// </summary>
        /// <param name="settings">An object containing the encoding settings.</param>
        /// <param name="destination">The WAV file to write.</param>
        /// <param name="options">The options that control the behaviors of the process.</param>
        public static void SaveAudioToWav(MediaEncoderSettings settings, string destination, ProcessStartOptions options)
        {
            string TempFile = settings.TempFile + ".avs";
            AviSynthScriptBuilder Script = new AviSynthScriptBuilder();

            if (settings.VideoAction != VideoAction.Copy)
            {
                // Read source script.
                Script.Script = File.ReadAllText(settings.ScriptFile);
                // Remote MT code.
                Script.RemoveMT();
                Script.AppendLine("Trim(0,0)");
            }
            else
            {
                // Read full video file.
                Script.AddPluginPath();
                if (settings.ConvertToAvi || settings.InputFile.ToLower().EndsWith(".avi"))
                {
                    Script.OpenAvi(settings.InputFile, !string.IsNullOrEmpty(settings.SourceAudioFormat));
                }
                else
                {
                    Script.OpenDirect(settings.InputFile, !string.IsNullOrEmpty(settings.SourceAudioFormat));
                }
                Script.AppendLine("KillVideo()");
            }
            Script.AppendLine();
            // Add audio gain.
            if (settings.AudioGain.HasValue && settings.AudioGain != 0)
            {
                Script.AppendLine("AmplifydB({0})", settings.AudioGain.Value);
            }
            if (settings.ChangeAudioPitch)
            {
                // Change pitch to 432hz.
                Script.LoadPluginDll("TimeStretch.dll");
                Script.AppendLine("ResampleAudio(48000)");
                Script.AppendLine("TimeStretchPlugin(pitch = 100.0 * 0.98181819915771484)");
            }
            // Add TWriteWAV.
            Script.AppendLine();
            Script.LoadPluginDll("TWriteAVI.dll");
            Script.AppendLine(@"TWriteWAV(""{0}"", true)", Script.GetAsciiPath(destination));
            Script.AppendLine("ForceProcessWAV()");

            // Write temp script.
            Script.WriteToFile(TempFile);
            // Execute. It aways returns an error but the file is generated.
            FFmpegProcess Worker = new FFmpegProcess(options);

            Worker.RunAvisynth(TempFile);
            File.Delete(TempFile);
        }
        private static void ApplyDeshaker(AviSynthScriptBuilder Script, MediaEncoderSettings settings, ref bool isLsb, ref bool isYV24, bool multiProcess)
        {
            settings.DeshakerSettings.LogFile = Script.GetAsciiPath(settings.DeshakerLog);
            settings.DeshakerSettings.Pass    = 2;
            Script.LoadPluginDll(@"VDubFilter.dll");

            Script.AppendLine(@"LoadVirtualDubPlugin(P+""deshaker.vdf"", ""deshaker"", preroll=0)");
            Script.AppendLine(@"ConvertToRGB32(matrix=""Rec709"")");
            if (multiProcess)
            {
                Script.AppendLine("### prefetch: 3, 3"); // Keep 3 frames before and after for temporal filters.
                Script.AppendLine("### ###");            // MP_Pipeline starts new process here
            }
            Script.AppendLine(@"deshaker(""{0}"")", settings.DeshakerSettings.ToString());
            Script.AppendLine(@"ConvertToYV12(matrix=""Rec709"")");
            if (multiProcess)
            {
                Script.AppendLine("### prefetch: {0}, {1}", settings.DeshakerSettings.FillBordersWithFutureFrames ? settings.DeshakerSettings.FillBordersWithFutureFramesCount : 0, settings.DeshakerSettings.FillBordersWithPreviousFrames ? settings.DeshakerSettings.FillBordersWithPreviousFramesCount : 0);
                Script.AppendLine("### ###"); // MP_Pipeline starts another here
            }
            isLsb  = false;
            isYV24 = false;
        }
        /// <summary>
        /// Calculates auto-crop coordinates for specified video script.
        /// </summary>
        /// <param name="filePath">The script to analyze.</param>
        /// <param name="sourceWidth">The width of the source video.</param>
        /// <param name="sourceHeight">The height of the source video.</param>
        /// <param name="options">The options that control the behaviors of the process.</param>
        /// <returns>The auto-crop coordinates.</returns>
        public static Rect GetAutoCropRect(string filePath, int sourceWidth, int sourceHeight, ProcessStartOptions options)
        {
            string TempScript = PathManager.GetTempFile(".avs");
            string TempResult = Path.ChangeExtension(TempScript, ".txt");

            // Create script to get auto-crop coordinates
            AviSynthScriptBuilder Script = new AviSynthScriptBuilder();

            Script.AddPluginPath();
            Script.OpenDirect(filePath, false);
            Script.LoadPluginDll("RoboCrop26.dll");
            Script.AppendLine(@"RoboCrop(LogFn=""{0}"")", Script.GetAsciiPath(TempResult));
            Script.AppendLine("Trim(0,-1)");
            Script.WriteToFile(TempScript);

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

            Worker.RunAvisynth(TempScript);

            // Read auto crop coordinates
            Rect Result = new Rect();

            if (File.Exists(TempResult))
            {
                string[] Values = File.ReadAllText(TempResult).Split(' ');
                if (Values.Length >= 13)
                {
                    Result.Left   = Math.Max(int.Parse(Values[10]), 0);
                    Result.Top    = Math.Max(int.Parse(Values[11]), 0);
                    Result.Right  = Math.Max(sourceWidth - int.Parse(Values[12]), 0);
                    Result.Bottom = Math.Max(sourceHeight - int.Parse(Values[13]), 0);
                    // Make result conservative, we have to visually see a line of black border to know the right dimensions.
                    if (Result.Left > 0)
                    {
                        Result.Left--;
                    }
                    if (Result.Top > 0)
                    {
                        Result.Top--;
                    }
                    if (Result.Right > 0)
                    {
                        Result.Right--;
                    }
                    if (Result.Bottom > 0)
                    {
                        Result.Bottom--;
                    }
                }
            }

            // Delete temp files.
            Exception LastError = null;

            for (int i = 0; i < 10; i++)
            {
                try {
                    File.Delete(TempScript);
                    File.Delete(TempResult);
                    break;
                } catch (Exception e) {
                    LastError = e;
                    System.Threading.Thread.Sleep(500);
                }
            }
            if (LastError != null)
            {
                throw LastError;
            }

            return(Result);
        }
Esempio n. 6
0
        public static Rect GetAutoCropRect(MediaEncoderSettings settings, bool silent)
        {
            string TempScript = settings.TempFile + ".avs";
            string TempResult = settings.TempFile + ".txt";
            string TempOut    = settings.TempFile + ".y4m";

            // Create script to get auto-crop coordinates
            AviSynthScriptBuilder Script = new AviSynthScriptBuilder();

            Script.AddPluginPath();
            Script.OpenDirect(settings.FilePath, false);
            Script.LoadPluginDll("RoboCrop26.dll");
            Script.AppendLine(@"RoboCrop(LogFn=""{0}"")", Script.GetAsciiPath(TempResult));
            Script.AppendLine("Trim(0,-1)");
            Script.WriteToFile(TempScript);

            // Run script.
            Run(@"Encoder\avs2yuv.exe", String.Format(@"""{0}"" -o ""{1}""", TempScript, TempOut), true);

            // Read auto crop coordinates
            Rect Result = new Rect();

            if (File.Exists(TempResult))
            {
                string[] Values = File.ReadAllText(TempResult).Split(' ');
                if (Values.Length >= 13)
                {
                    Result.Left   = int.Parse(Values[10]);
                    Result.Top    = int.Parse(Values[11]);
                    Result.Right  = settings.SourceWidth.Value - int.Parse(Values[12]);
                    Result.Bottom = settings.SourceHeight.Value - int.Parse(Values[13]);
                    // Make result conservative, we have to visually see a line of black border to know the right dimensions.
                    if (Result.Left > 0)
                    {
                        Result.Left--;
                    }
                    if (Result.Top > 0)
                    {
                        Result.Top--;
                    }
                    if (Result.Right > 0)
                    {
                        Result.Right--;
                    }
                    if (Result.Bottom > 0)
                    {
                        Result.Bottom--;
                    }
                }
            }

            // Delete temp files.
            Exception LastError = null;

            for (int i = 0; i < 10; i++)
            {
                try {
                    File.Delete(TempScript);
                    File.Delete(TempResult);
                    File.Delete(TempOut); // Dummy file that received avs2yuv output.
                    break;
                }
                catch (Exception e) {
                    LastError = e;
                    System.Threading.Thread.Sleep(500);
                }
            }
            if (LastError != null)
            {
                throw LastError;
            }

            return(Result);
        }