/// <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;

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

            SubtitleInfo sub     = _jobInfo.SubtitleStreams[_jobInfo.StreamId];
            string       inFile  = sub.TempFile;
            string       outFile = Path.ChangeExtension(inFile, "converted.srt");

            _bw.ReportProgress(0, _readingstatus);
            TextSubtitle textSub = null;

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

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

            if (textSub == null)
            {
                return;
            }

            _bw.ReportProgress(50, _writingstatus);

            if (SRTWriter.WriteFile(outFile, textSub))
            {
                sub.Format         = "UTF-8";
                sub.NeedConversion = false;
                _jobInfo.TempFiles.Add(inFile);
                sub.TempFile      = outFile;
                _jobInfo.ExitCode = 0;
            }

            _bw.ReportProgress(100);
            _jobInfo.CompletedStep = _jobInfo.NextStep;
            e.Result = _jobInfo;
        }
Ejemplo n.º 2
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;
        }