Example #1
0
        private void ExecuteCopy(string fromFile, string toFile, bool isDirectory)
        {
            string progressFormat = Processing.GetResourceString("fileworker_copy_progress");

            string outDir = isDirectory ? toFile : Path.GetDirectoryName(toFile);

            if (outDir != null && ((!Directory.Exists(outDir)) && (isDirectory)))
            {
                Directory.CreateDirectory(outDir, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
            }

            if (File.GetAttributes(fromFile) == FileAttributes.Directory)
            {
                return;
            }
            using (FileStream fromStream = new FileStream(fromFile, FileMode.Open),
                   toStream = new FileStream(toFile, FileMode.CreateNew))
            {
                long   total        = fromStream.Length;
                long   current      = 0;
                Byte[] buffer       = new Byte[1048576]; // 1 mbyte buffer
                int    secRemaining = 0;

                DateTime startTime  = DateTime.Now;
                DateTime reportTime = DateTime.Now;
                do
                {
                    int read = fromStream.Read(buffer, 0, buffer.Length);
                    toStream.Write(buffer, 0, read);
                    current += read;

                    long     prozent     = current * 100 / total;
                    TimeSpan eta         = startTime.Subtract(DateTime.Now);
                    int      kbRemaining = ((int)current - (int)total) / 1024;
                    if (eta.Seconds != 0)
                    {
                        double kbs = Math.Round(current / 1024d / (eta.Seconds), 2);
                        secRemaining = kbRemaining / (int)kbs;
                    }

                    if (reportTime.AddSeconds(1) <= DateTime.Now)
                    {
                        string progressText = string.Format(progressFormat,
                                                            Path.GetFileName(fromFile), (int)(current / 1024),
                                                            (int)(total / 1024), secRemaining);

                        _bw.ReportProgress((int)prozent, progressText);
                        reportTime = DateTime.Now;
                    }
                } while (total != current);
            }
        }
Example #2
0
        public static void InitTvDBCache()
        {
            if (!String.IsNullOrEmpty(TvDBCachePath))
            {
                return;
            }

            TvDBCachePath = Path.Combine(CommonAppSettingsPath, "TvDBCache");
            if (!Directory.Exists(TvDBCachePath))
            {
                Directory.CreateDirectory(TvDBCachePath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
            }
        }
        private void InitialiseProfilesFile()
        {
            _profilesFile = new XmlDocument();
            try
            {
                _profilesFile.Load(_profFileName);
            }
            catch (Exception)
            {
                var dir = Path.GetDirectoryName(_profFileName);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
                }
                var decl = _profilesFile.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                var xn   = _profilesFile.CreateElement("VideoConvert");
                _profilesFile.AppendChild(decl);
                _profilesFile.AppendChild(xn);

                _profilesFile.Save(_profFileName);
                _profilesFile.Load(_profFileName);
            }
        }
Example #4
0
        void UpdaterDoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = (BackgroundWorker)sender;

            string status          = Processing.GetResourceString("update_downloading_status");
            string progressFmt     = Processing.GetResourceString("update_downloading_progress");
            string unzippingStatus = Processing.GetResourceString("update_unzipping");
            string reloadStatus    = Processing.GetResourceString("update_reload_versions");
            string finishedStatus  = Processing.GetResourceString("update_finished");
            string importProfiles  = Processing.GetResourceString("update_import_profiles");

            string tempPath = AppSettings.TempPath;

            foreach (ToolVersions item in _tempToolCollection)
            {
                string showName = item.ToolName;
                bw.ReportProgress(-1, string.Format(status, showName, item.ServerVersion));

                string outFPath = Path.GetDirectoryName(item.FileName);
                if (!string.IsNullOrEmpty(outFPath) && !Directory.Exists(outFPath))
                {
                    Directory.CreateDirectory(outFPath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
                }
                tempPath = outFPath;

                WebClient downloader = new WebClient {
                    UseDefaultCredentials = true
                };
                downloader.DownloadFileAsync(new Uri(item.DownloadUri), item.FileName);

                ToolVersions lItem = item;

                downloader.DownloadProgressChanged += (s, ev) =>
                {
                    string progress = string.Format(progressFmt, showName, lItem.ServerVersion, ev.ProgressPercentage);
                    bw.ReportProgress(ev.ProgressPercentage, progress);
                };

                while (downloader.IsBusy)
                {
                    Thread.Sleep(200);
                }

                switch (lItem.DownloadType)
                {
                case AppType.Updater:
                    if (!string.IsNullOrEmpty(AppSettings.UpdaterPath) && !Directory.Exists(AppSettings.UpdaterPath))
                    {
                        Directory.CreateDirectory(AppSettings.UpdaterPath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
                    }
                    bw.ReportProgress(-1, unzippingStatus);
                    try
                    {
                        using (SevenZipExtractor eFile = new SevenZipExtractor(lItem.FileName))
                        {
                            eFile.PreserveDirectoryStructure = true;
                            eFile.FileExtractionStarted     += (o, args) =>
                            {
                                if (args.FileInfo.IsDirectory)
                                {
                                    string outPath = Path.Combine(AppSettings.UpdaterPath, args.FileInfo.FileName);
                                    Directory.CreateDirectory(outPath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));
                                }
                            };
                            eFile.ExtractArchive(AppSettings.UpdaterPath);
                        }
                        File.Delete(item.FileName);
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorFormat("Error reading \"{0:s}\" -> {1:s}", item.FileName, ex.Message);
                    }
                    break;

                case AppType.Profiles:
                    try
                    {
                        bw.ReportProgress(-1, importProfiles);
                        List <EncoderProfile> importedProfiles = ProfilesHandler.ImportProfiles(lItem.FileName);
                        ProfilesHandler       profiles         = new ProfilesHandler();
                        foreach (EncoderProfile profile in profiles.ProfileList)
                        {
                            try
                            {
                                EncoderProfile selProfile =
                                    importedProfiles.Single(
                                        encoderProfile =>
                                        encoderProfile.Name == profile.Name && encoderProfile.Type == profile.Type);
                                importedProfiles.Remove(selProfile);
                            }
                            catch (Exception importException)
                            {
                                Log.Error(importException);
                            }
                        }
                        foreach (EncoderProfile encoderProfile in importedProfiles)
                        {
                            profiles.AddProfile(encoderProfile);
                        }

                        profiles.Destroy();
                        AppSettings.LastProfilesVer = lItem.ServerVersion;
                    }
                    catch (Exception exProfiles)
                    {
                        Log.Error(exProfiles);
                    }

                    break;

                default:
                {
                    _mainAppUpdate = true;
                    PackageInfo package = new PackageInfo
                    {
                        PackageName             = item.ToolName,
                        PackageLocation         = item.FileName,
                        Version                 = item.ServerVersion,
                        Destination             = item.Destination,
                        WriteVersion            = item.DownloadType == AppType.AviSynthPlugins,
                        ClearDirectory          = item.DownloadType == AppType.MainApp,
                        RecursiveClearDirectory = item.DownloadType == AppType.AviSynthPlugins
                    };
                    _packages.Add(package);
                }
                break;
                }
                bw.ReportProgress(-20, item);
            }

            if (_packages.Count > 0)
            {
                if (tempPath != null)
                {
                    MainAppUpdateFile = Path.Combine(tempPath, "update.xml");
                }
                Updater.SaveUpdateList(MainAppUpdateFile, _packages);
            }
            else
            {
                bw.ReportProgress(-1, reloadStatus);
                Processing.GetAppVersions();
            }

            bw.ReportProgress(0, finishedStatus);
        }
        private string GenerateCommandLine()
        {
            var sb = new StringBuilder();
            var localExecutable = Path.Combine(_appConfig.ToolsPath, Executable);

            sb.Append($"-jar \"{localExecutable}\" ");

            _subtitle = _currentTask.SubtitleStreams[_currentTask.StreamId];

            var targetRes = -1;

            if (_currentTask.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                targetRes = _currentTask.EncodingProfile.SystemType == 0 ? 576 : 480;
            }

            _inputFile = _subtitle.TempFile;

            TextSubtitle textSub = null;

            switch (_subtitle.Format)
            {
            case "SSA":
            case "ASS":
                textSub = SsaReader.ReadFile(_inputFile);
                break;

            case "UTF-8":
                textSub = SrtReader.ReadFile(_inputFile);
                break;
            }

            var inFileDir = Path.GetDirectoryName(_inputFile);

            if (string.IsNullOrEmpty(inFileDir))
            {
                inFileDir = string.Empty;
            }

            var inFileName = Path.GetFileNameWithoutExtension(_inputFile);

            if (string.IsNullOrEmpty(inFileName))
            {
                inFileName = string.Empty;
            }

            var outPath = Path.Combine(inFileDir, inFileName);

            if (Directory.Exists(outPath))
            {
                Directory.Delete(outPath, true);
            }
            Directory.CreateDirectory(outPath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));

            var inFileFullName = Path.GetFileName(_inputFile);

            if (string.IsNullOrEmpty(inFileFullName))
            {
                inFileFullName = string.Empty;
            }

            _outputFile = Path.Combine(outPath, inFileFullName);

            if (textSub != null)
            {
                var xmlFile = Path.ChangeExtension(_outputFile, "xml");
                if (BdnExport.WriteBdnXmlFile(textSub,
                                              xmlFile,
                                              _currentTask.VideoStream.Width,
                                              _currentTask.VideoStream.Height,
                                              _currentTask.VideoStream.Fps))
                {
                    _subtitle.Format = "XML";
                    _currentTask.TempFiles.Add(_inputFile);
                    _subtitle.TempFile = xmlFile;
                    _inputFile         = xmlFile;
                }
            }

            if (_currentTask.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                _outputFile = Path.ChangeExtension(_outputFile, "processed.xml");
            }
            else if (_subtitle.KeepOnlyForcedCaptions)
            {
                _outputFile = Path.ChangeExtension(_outputFile, "forced.sup");
            }
            else if (_subtitle.Format == "XML" || _subtitle.Format == "VobSub")
            {
                _outputFile = Path.ChangeExtension(_outputFile, "sup");
            }

            var targetFps = _currentTask.VideoStream.FrameMode.Trim().ToLowerInvariant() == "frame doubling"
                              ? _currentTask.VideoStream.Fps * 2
                              : _currentTask.VideoStream.Fps;
            var fpsMode = "keep";

            if (Math.Abs(targetFps - _currentTask.VideoStream.Fps) > 0)
            {
                fpsMode = targetFps.ToString("0.000", _appConfig.CInfo);
            }

            sb.Append($"\"{_inputFile}\" --output \"{_outputFile}\" --fps-target {fpsMode} --palette-mode keep ");

            if (_subtitle.KeepOnlyForcedCaptions)
            {
                sb.Append("--forced-only ");
            }

            if (_currentTask.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                sb.Append($" --resolution {targetRes:0} ");
            }

            return(sb.ToString());
        }
Example #6
0
        /// <summary>
        /// Main processing function, called by BackgroundWorker thread
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DoProcess(object sender, DoWorkEventArgs e)
        {
            _bw = (BackgroundWorker)sender;

            string createSubtitle = Processing.GetResourceString("bdsup2sub_convert_subtitle_create");

            _bw.ReportProgress(-10, _status);
            _bw.ReportProgress(0, _status);

            string javaExecutable  = AppSettings.JavaInstallPath;
            string localExecutable = Path.Combine(AppSettings.ToolsPath, Executable);

            SubtitleInfo sub = _jobInfo.SubtitleStreams[_jobInfo.StreamId];

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("-jar \"{0}\" ", localExecutable);

            int targetRes = -1;

            if (_jobInfo.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                targetRes = _jobInfo.EncodingProfile.SystemType == 0 ? 576 : 480;
            }

            string inFile = sub.TempFile;

            TextSubtitle textSub = null;

            switch (sub.Format)
            {
            case "SSA":
            case "ASS":
                textSub = SSAReader.ReadFile(inFile);
                break;

            case "UTF-8":
                textSub = SRTReader.ReadFile(inFile);
                break;
            }

            string inFileDir = Path.GetDirectoryName(inFile);

            if (string.IsNullOrEmpty(inFileDir))
            {
                inFileDir = string.Empty;
            }

            string inFileName = Path.GetFileNameWithoutExtension(inFile);

            if (string.IsNullOrEmpty(inFileName))
            {
                inFileName = string.Empty;
            }

            string outPath = Path.Combine(inFileDir, inFileName);

            if (Directory.Exists(outPath))
            {
                Directory.Delete(outPath, true);
            }
            Directory.CreateDirectory(outPath, DirSecurity.CreateDirSecurity(SecurityClass.Everybody));

            string inFileFullName = Path.GetFileName(inFile);

            if (string.IsNullOrEmpty(inFileFullName))
            {
                inFileFullName = string.Empty;
            }

            string outFile = Path.Combine(outPath, inFileFullName);

            if (textSub != null)
            {
                string xmlFile = Path.ChangeExtension(outFile, "xml");
                if (BDNExport.WriteBDNXmlFile(textSub, xmlFile, _jobInfo.VideoStream.Width, _jobInfo.VideoStream.Height,
                                              _jobInfo.VideoStream.FPS))
                {
                    sub.Format = "XML";
                    _jobInfo.TempFiles.Add(inFile);
                    sub.TempFile = xmlFile;
                    inFile       = xmlFile;
                }
            }

            if (_jobInfo.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                outFile = Path.ChangeExtension(outFile, "processed.xml");
            }
            else if (sub.KeepOnlyForcedCaptions)
            {
                outFile = Path.ChangeExtension(outFile, "forced.sup");
            }
            else if (sub.Format == "XML" || sub.Format == "VobSub")
            {
                outFile = Path.ChangeExtension(outFile, "sup");
            }

            float targetFPS = _jobInfo.VideoStream.FrameMode.Trim().ToLowerInvariant() == "frame doubling"
                        ? _jobInfo.VideoStream.FPS * 2
                        : _jobInfo.VideoStream.FPS;
            string fpsMode = "keep";

            if (Math.Abs(targetFPS - _jobInfo.VideoStream.FPS) > 0)
            {
                fpsMode = targetFPS.ToString("0.000", AppSettings.CInfo);
            }

            sb.AppendFormat(AppSettings.CInfo, "\"{0:s}\" --output \"{1:s}\" --fps-target {2} --palette-mode keep ", inFile, outFile, fpsMode);

            if (sub.KeepOnlyForcedCaptions)
            {
                sb.AppendFormat("--forced-only ");
            }

            if (_jobInfo.EncodingProfile.OutFormat == OutputType.OutputDvd)
            {
                sb.AppendFormat(AppSettings.CInfo, " --resolution {0:0} ", targetRes);
            }

            using (Process encoder = new Process())
            {
                ProcessStartInfo parameter = new ProcessStartInfo(javaExecutable)
                {
                    WorkingDirectory       = AppSettings.DemuxLocation,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    Arguments = sb.ToString()
                };

                encoder.StartInfo           = parameter;
                encoder.OutputDataReceived += EncoderOnOutputDataReceived;

                Log.InfoFormat("BDSup2Sub: {0:s}", parameter.Arguments);

                bool started;
                try
                {
                    started = encoder.Start();
                }
                catch (Exception ex)
                {
                    started = false;
                    Log.ErrorFormat("BDSup2Sub exception: {0}", ex);
                    _jobInfo.ExitCode = -1;
                }

                if (started)
                {
                    encoder.PriorityClass = AppSettings.GetProcessPriority();
                    encoder.BeginOutputReadLine();

                    while (!encoder.HasExited)
                    {
                        if (_bw.CancellationPending)
                        {
                            encoder.Kill();
                        }

                        Thread.Sleep(200);
                    }
                    _jobInfo.ExitCode = encoder.ExitCode;
                    Log.InfoFormat("Exit Code: {0:g}", _jobInfo.ExitCode);
                }

                if (_jobInfo.ExitCode == 0)
                {
                    _jobInfo.TempFiles.Add(inFile);
                    if (sub.Format == "XML")
                    {
                        GetTempImages(inFile);
                    }
                    if (sub.Format == "VobSub")
                    {
                        _jobInfo.TempFiles.Add(Path.ChangeExtension(inFile, "sub"));
                    }

                    if (_jobInfo.EncodingProfile.OutFormat == OutputType.OutputDvd)
                    {
                        _jobInfo.TempFiles.Add(outFile);
                        _bw.ReportProgress(-1, createSubtitle);

                        sub.TempFile = GenerateSpuMuxSubtitle(outFile);
                        sub.Format   = "SpuMux";
                    }
                    else
                    {
                        sub.TempFile = outFile;
                        sub.Format   = "PGS";
                    }
                    sub.NeedConversion = false;
                }
            }

            _bw.ReportProgress(100);
            _jobInfo.CompletedStep = _jobInfo.NextStep;

            e.Result = _jobInfo;
        }