コード例 #1
0
 public static bool IsM2TransportStream(string fileName)
 {
     using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
     {
         return(TransportStreamParser.IsM2TransportStream(fs));
     }
 }
コード例 #2
0
 public static bool IsM2TransportStream(string fileName)
 {
     using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
     {
         var tsp = new TransportStreamParser();
         tsp.DetectFormat(fs);
         return(tsp.IsM2TransportStream);
     }
 }
コード例 #3
0
        public static bool IsM2TransportStream(string fileName)
        {
            using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var transportStreamParser = new TransportStreamParser();
                transportStreamParser.DetectFormat(fileStream);

                return transportStreamParser.IsM2TransportStream;
            }
        }
コード例 #4
0
        private void backgroundWorkerMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TransportStreamParser tsParser = (TransportStreamParser)e.Result;

            PopulateTreeView(tsParser);
            this.splitContainerMaIN.Visible = true;
            this.progressBarLoading.Value   = this.progressBarLoading.Minimum;
            this.panelAbout.Visible         = false;

            this.Text = TITLEBAR + " - " + _currentFile;
        }
コード例 #5
0
        internal void Initialize(TransportStreamParser tsParser, string fileName)
        {
            _tsParser = tsParser;
            Text      = string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.Title, fileName);

            foreach (int id in tsParser.SubtitlePacketIds)
            {
                listBoxTracks.Items.Add(string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.PidLine, id, tsParser.GetDvbSubtitles(id).Count));
            }
            listBoxTracks.SelectedIndex = 0;
        }
コード例 #6
0
        internal void Initialize(TransportStreamParser tsParser, string fileName)
        {
            _programMapTableParser = new ProgramMapTableParser();
            _programMapTableParser.Parse(fileName); // get languages
            _tsParser = tsParser;
            _fileName = fileName;
            Text      = string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.Title, fileName);

            foreach (int id in tsParser.SubtitlePacketIds)
            {
                var language = _programMapTableParser.GetSubtitleLanguage(id);
                if (string.IsNullOrEmpty(language))
                {
                    language = "unknown";
                }

                listBoxTracks.Items.Add(new StreamTrackItem
                {
                    Text       = string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.PidLineImage, id, language, tsParser.GetDvbSubtitles(id).Count),
                    IsTeletext = false,
                    Pid        = id,
                    Language   = language
                });
            }

            foreach (var program in tsParser.TeletextSubtitlesLookup)
            {
                var language = _programMapTableParser.GetSubtitleLanguage(program.Key);
                if (string.IsNullOrEmpty(language))
                {
                    language = "unknown";
                }

                foreach (var kvp in program.Value)
                {
                    var subtitle = new Subtitle(kvp.Value);
                    subtitle.Renumber();
                    listBoxTracks.Items.Add(new StreamTrackItem
                    {
                        Text       = string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.PidLineTeletext, kvp.Key, program.Key, language, kvp.Value.Count),
                        IsTeletext = true,
                        Pid        = program.Key,
                        PageNumber = kvp.Key,
                        Srt        = new SubRip().ToText(subtitle, null)
                    });
                }
            }

            listBoxTracks.SelectedIndex = 0;
        }
コード例 #7
0
        public void TransportStreamTestImage()
        {
            string fileName = Path.Combine(Directory.GetCurrentDirectory(), "sample_TS_with_graphics.ts");
            var    parser   = new TransportStreamParser();

            parser.Parse(fileName, null);
            var subtitles = parser.GetDvbSubtitles(41);

            Assert.IsTrue(subtitles.Count == 10);
            using (var bmp = subtitles[0].Pes.GetImageFull())
            {
                Assert.IsTrue(bmp.Width == 719);
                Assert.IsTrue(bmp.Height == 575);
            }
        }
コード例 #8
0
        public void TransportStreamTestTeletext()
        {
            string fileName = Path.Combine(Directory.GetCurrentDirectory(), "sample_TS_with_teletext.ts");
            var    parser   = new TransportStreamParser();

            parser.Parse(fileName, null);
            Assert.AreEqual(1, parser.TeletextSubtitlesLookup.Count);
            Assert.AreEqual(5104, parser.TeletextSubtitlesLookup.First().Key); // package Id
            var packagePages = parser.TeletextSubtitlesLookup[parser.TeletextSubtitlesLookup.First().Key];

            Assert.AreEqual(2, packagePages.Count);
            Assert.AreEqual(1, packagePages[150].Count); // first page number
            Assert.AreEqual(1, packagePages[799].Count); // second page number

            Assert.AreEqual("Für diese Klassenstufe ist er nicht" + Environment.NewLine +
                            "geeignet.  <font color=\"#00ffff\">  Stufen Sie ihn zurück!</font>", packagePages[150][0].Text);

            Assert.AreEqual("Han er ikke egnet" + Environment.NewLine +
                            "til dette klassetrin.", packagePages[799][0].Text);
        }
コード例 #9
0
        internal void Initialize(TransportStreamParser tsParser, string fileName)
        {
            _programMapTableParser = new ProgramMapTableParser();
            _programMapTableParser.Parse(fileName); // get languages
            _tsParser = tsParser;
            _fileName = fileName;
            Text      = string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.Title, fileName);

            foreach (int id in tsParser.SubtitlePacketIds)
            {
                var language = _programMapTableParser.GetSubtitleLanguage(id);
                if (!string.IsNullOrEmpty(language))
                {
                    listBoxTracks.Items.Add(string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.PidLine, id + ", " + language, tsParser.GetDvbSubtitles(id).Count));
                }
                else
                {
                    listBoxTracks.Items.Add(string.Format(Configuration.Settings.Language.TransportStreamSubtitleChooser.PidLine, id, tsParser.GetDvbSubtitles(id).Count));
                }
            }
            listBoxTracks.SelectedIndex = 0;
        }
コード例 #10
0
        private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e)
        {
            BackGroundWorkerInitInfo initInfo = (BackGroundWorkerInitInfo)e.Argument;

            TransportStreamParser tsParser = new TransportStreamParser();

            tsParser.Parse(initInfo.SourceFile, (pos, total) => UpdateProgressBackground(pos, total, "Parsing Transport Stream file. Please wait..."));
            if (initInfo.CreateHtmlFiles)
            {
                foreach (KeyValuePair <int, List <DvbSubPes> > item in tsParser.SubtitlesLookup)
                {
                    string res = "<html><body>";
                    res += GetHtmlPid(item);
                    res += "</body></html>";
                    //TODO change to "next to video file"
                    string outFile = Path.GetDirectoryName(initInfo.SourceFile) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(initInfo.SourceFile) + "_(PID" + item.Key.ToString() + ").html";
                    File.WriteAllText(outFile, res);
                }
            }

            e.Result = tsParser;
        }
コード例 #11
0
ファイル: TsToBluRaySup.cs プロジェクト: zzfeed/subtitleedit
        internal static void WriteTrack(string fileName, string outputFolder, bool overwrite, int count, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point?resolution, ProgramMapTableParser programMapTableParser, int pid, TransportStreamParser tsParser)
        {
            var overrideScreenSize = Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0 &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 ||
                                     resolution.HasValue;

            using (var form = new ExportPngXml())
            {
                form.Initialize(new Subtitle(), new SubRip(), BatchConvert.BluRaySubtitle, fileName, null, fileName);


                var language       = GetFileNameEnding(programMapTableParser, pid);
                var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + language + Path.GetExtension(fileName), ".sup", outputFolder, overwrite);
                stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> PID {pid} to {outputFileName}...");
                var sub = tsParser.GetDvbSubtitles(pid);
                progressCallback?.Invoke($"Save PID {pid}");
                var subtitleScreenSize = GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
                using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
                {
                    for (int index = 0; index < sub.Count; index++)
                    {
                        var p        = sub[index];
                        var pos      = p.GetPosition();
                        var bmp      = sub[index].GetBitmap();
                        var tsWidth  = bmp.Width;
                        var tsHeight = bmp.Height;
                        var nBmp     = new NikseBitmap(bmp);
                        pos.Top  += nBmp.CropTopTransparent(0);
                        pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
                        bmp.Dispose();
                        bmp = nBmp.GetBitmap();
                        var mp = form.MakeMakeBitmapParameter(index, subtitleScreenSize.X, subtitleScreenSize.Y);

                        if (overrideScreenSize)
                        {
                            var widthFactor  = (double)subtitleScreenSize.X / tsWidth;
                            var heightFactor = (double)subtitleScreenSize.Y / tsHeight;
                            var resizeBmp    = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
                            bmp.Dispose();
                            bmp      = resizeBmp;
                            pos.Left = (int)Math.Round(pos.Left * widthFactor);
                            pos.Top  = (int)Math.Round(pos.Top * heightFactor);
                            progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
                        }

                        mp.Bitmap       = bmp;
                        mp.P            = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
                        mp.ScreenWidth  = subtitleScreenSize.X;
                        mp.ScreenHeight = subtitleScreenSize.Y;
                        if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                        {
                            var overrideMarginX = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideHMargin * subtitleScreenSize.X / 100.0);
                            var overrideMarginY = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin * subtitleScreenSize.Y / 100.0);
                            if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                            {
                                var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
                                if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                                {
                                    x = overrideMarginX;
                                }
                                else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                                {
                                    x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
                                }

                                var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
                                mp.OverridePosition = new Point(x, y);
                            }
                            else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
                            {
                                var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
                                if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                                {
                                    x = overrideMarginX;
                                }
                                else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                                {
                                    x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
                                }

                                mp.OverridePosition = new Point(x, pos.Top);
                            }
                            else
                            {
                                var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
                                mp.OverridePosition = new Point(pos.Left, y);
                            }
                        }
                        else
                        {
                            mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position (can be scaled)
                        }

                        ExportPngXml.MakeBluRaySupImage(mp);
                        binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
                        mp.Bitmap?.Dispose();
                        mp.Bitmap = null;
                    }
                }
            }
            stdOutWriter?.WriteLine(" done.");
        }
コード例 #12
0
        public static bool ConvertFromTs(string targetFormat, string fileName, string outputFolder, bool overwrite, ref int count, ref int converted, ref int errors, List <SubtitleFormat> formats, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point?resolution, TextEncoding targetEncoding, List <CommandLineConverter.BatchAction> actions, TimeSpan offset, int pacCodePage, double?targetFrameRate, HashSet <string> multipleReplaceImportFiles, string ocrEngine, bool teletextOnly)
        {
            var success = false;
            var programMapTableParser = new ProgramMapTableParser();

            programMapTableParser.Parse(fileName); // get languages
            var tsParser = new TransportStreamParser();

            tsParser.Parse(fileName, (position, total) =>
            {
                var percent = (int)Math.Round(position * 100.0 / total);
                stdOutWriter?.Write("\rParsing transport stream {0}: {1}%", fileName, percent);
                progressCallback?.Invoke($"{percent}%");
            });
            stdOutWriter?.WriteLine();

            // images
            if (!teletextOnly)
            {
                foreach (int id in tsParser.SubtitlePacketIds)
                {
                    if (BatchConvert.BluRaySubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
                    {
                        TsToBluRaySup.WriteTrack(fileName, outputFolder, overwrite, count, stdOutWriter, progressCallback, resolution, programMapTableParser, id, tsParser);
                        success = true;
                    }
                    else if (BatchConvert.BdnXmlSubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
                    {
                        TsToBdnXml.WriteTrack(fileName, outputFolder, overwrite, stdOutWriter, progressCallback, resolution, programMapTableParser, id, tsParser);
                        success = true;
                    }
                    else
                    {
                        var preExt           = TsToBluRaySup.GetFileNameEnding(programMapTableParser, id);
                        var binaryParagraphs = new List <IBinaryParagraph>();
                        var subtitle         = new Subtitle();
                        foreach (var transportStreamSubtitle in tsParser.GetDvbSubtitles(id))
                        {
                            binaryParagraphs.Add(transportStreamSubtitle);
                            subtitle.Paragraphs.Add(new Paragraph(string.Empty, transportStreamSubtitle.StartMilliseconds, transportStreamSubtitle.EndMilliseconds));
                        }

                        success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), binaryParagraphs, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true, null, null, ocrEngine, preExt);
                        if (success)
                        {
                            converted--;
                        }
                    }
                }
            }

            // teletext
            foreach (var program in tsParser.TeletextSubtitlesLookup)
            {
                foreach (var kvp in program.Value)
                {
                    var subtitle = new Subtitle(kvp.Value);
                    subtitle.Renumber();
                    var preExt = TsToBluRaySup.GetFileNameEnding(programMapTableParser, kvp.Key);
                    success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), null, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true, null, null, null, preExt);
                    if (success)
                    {
                        converted--;
                    }
                }
            }

            return(success);
        }
コード例 #13
0
ファイル: TsToBdnXml.cs プロジェクト: zhiiker/subtitleedit
        internal static void WriteTrack(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point?resolution, ProgramMapTableParser programMapTableParser, int pid, TransportStreamParser tsParser)
        {
            var overrideScreenSize = Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0 &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 ||
                                     resolution.HasValue;

            using (var form = new ExportPngXml())
            {
                var language  = TsToBluRaySup.GetFileNameEnding(programMapTableParser, pid);
                var nameNoExt = Utilities.GetFileNameWithoutExtension(fileName) + "." + language;
                var folder    = Path.Combine(outputFolder, nameNoExt);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(nameNoExt + Path.GetExtension(fileName), ".xml", folder, overwrite);
                stdOutWriter?.WriteLine($"Saving PID {pid} to {outputFileName}...");
                progressCallback?.Invoke($"Save PID {pid}");
                var sub      = tsParser.GetDvbSubtitles(pid);
                var subtitle = new Subtitle();
                foreach (var p in sub)
                {
                    subtitle.Paragraphs.Add(new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds));
                }

                var res       = TsToBluRaySup.GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
                var videoInfo = new VideoInfo {
                    Success = true, Width = res.X, Height = res.Y
                };
                form.Initialize(subtitle, new SubRip(), BatchConvert.BdnXmlSubtitle, fileName, videoInfo, fileName);
                var sb = new StringBuilder();
                var imagesSavedCount = 0;
                for (int index = 0; index < sub.Count; index++)
                {
                    var p        = sub[index];
                    var pos      = p.GetPosition();
                    var bmp      = sub[index].GetBitmap();
                    var tsWidth  = bmp.Width;
                    var tsHeight = bmp.Height;
                    var nBmp     = new NikseBitmap(bmp);
                    pos.Top  += nBmp.CropTopTransparent(0);
                    pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
                    bmp.Dispose();
                    bmp = nBmp.GetBitmap();
                    var mp = form.MakeMakeBitmapParameter(index, videoInfo.Width, videoInfo.Height);

                    if (overrideScreenSize)
                    {
                        var widthFactor  = (double)videoInfo.Width / tsWidth;
                        var heightFactor = (double)videoInfo.Height / tsHeight;
                        var resizeBmp    = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
                        bmp.Dispose();
                        bmp      = resizeBmp;
                        pos.Left = (int)Math.Round(pos.Left * widthFactor);
                        pos.Top  = (int)Math.Round(pos.Top * heightFactor);
                        progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
                    }

                    mp.Bitmap       = bmp;
                    mp.P            = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
                    mp.ScreenWidth  = videoInfo.Width;
                    mp.ScreenHeight = videoInfo.Height;
                    int bottomMarginInPixels;
                    if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                    {
                        if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                        {
                            var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
                            if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                            {
                                x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
                            }
                            else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                            {
                                x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
                            }

                            var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
                            mp.OverridePosition = new Point(x, y);
                        }
                        else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
                        {
                            var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
                            if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                            {
                                x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
                            }
                            else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                            {
                                x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
                            }

                            mp.OverridePosition = new Point(x, pos.Top);
                        }
                        else
                        {
                            var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
                            mp.OverridePosition = new Point(pos.Left, y);
                        }

                        bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
                    }
                    else
                    {
                        mp.OverridePosition  = new Point(pos.Left, pos.Top); // use original position
                        bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
                    }

                    imagesSavedCount = form.WriteBdnXmlParagraph(videoInfo.Width, sb, bottomMarginInPixels, videoInfo.Height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
                }

                form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
            }
        }
コード例 #14
0
        public static bool ConvertFromTsToBluRaySup(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback)
        {
            var tsParser = new TransportStreamParser();

            tsParser.Parse(fileName, (position, total) =>
            {
                var percent = (int)Math.Round(position * 100.0 / total);
                stdOutWriter?.Write("\rParsing transport stream: {0}%", percent);
                progressCallback?.Invoke($"{percent}%");
            });
            stdOutWriter?.Write("\r".PadRight(32, ' '));
            stdOutWriter?.Write("\r");
            var videoInfo = UiUtil.GetVideoInfo(fileName);
            int width     = 720;
            int height    = 576;

            if (videoInfo.Success && videoInfo.Width > 0 && videoInfo.Height > 0)
            {
                width  = videoInfo.Width;
                height = videoInfo.Height;
            }

            var overrideScreenSize = Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 &&
                                     Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0;

            if (overrideScreenSize)
            {
                width  = Configuration.Settings.Tools.BatchConvertTsScreenWidth;
                height = Configuration.Settings.Tools.BatchConvertTsScreenHeight;
            }
            using (var form = new ExportPngXml())
            {
                if (tsParser.SubtitlePacketIds.Count == 0)
                {
                    stdOutWriter?.WriteLine($"No subtitles found");
                    progressCallback?.Invoke($"No subtitles found");
                    return(false);
                }
                form.Initialize(new Subtitle(), new SubRip(), BatchConvert.BluRaySubtitle, fileName, videoInfo, fileName);
                foreach (int pid in tsParser.SubtitlePacketIds)
                {
                    var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + "-" + pid + Path.GetExtension(fileName), ".sup", outputFolder, overwrite);
                    stdOutWriter?.WriteLine($"Saving PID {pid} to {outputFileName}...");
                    var sub = tsParser.GetDvbSubtitles(pid);
                    progressCallback?.Invoke($"Save PID {pid}");
                    using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
                    {
                        for (int index = 0; index < sub.Count; index++)
                        {
                            var p   = sub[index];
                            var pos = p.GetPosition();
                            var bmp = sub[index].GetBitmap();
                            if (!overrideScreenSize)
                            {
                                width            = bmp.Width;
                                height           = bmp.Height;
                                videoInfo.Width  = bmp.Width;
                                videoInfo.Height = bmp.Height;
                            }
                            var tsWidth  = bmp.Width;
                            var tsHeight = bmp.Height;
                            var nbmp     = new NikseBitmap(bmp);
                            pos.Top  += nbmp.CropTopTransparent(0);
                            pos.Left += nbmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
                            bmp.Dispose();
                            bmp = nbmp.GetBitmap();
                            var mp = form.MakeMakeBitmapParameter(index, width, height);

                            if (overrideScreenSize)
                            {
                                var widthFactor  = (double)width / tsWidth;
                                var heightFactor = (double)height / tsHeight;
                                var resizeBmp    = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
                                bmp.Dispose();
                                bmp      = resizeBmp;
                                pos.Left = (int)Math.Round(pos.Left * widthFactor);
                                pos.Top  = (int)Math.Round(pos.Top * heightFactor);
                                progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
                            }

                            mp.Bitmap       = bmp;
                            mp.P            = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
                            mp.ScreenWidth  = width;
                            mp.ScreenHeight = height;
                            if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                            {
                                var overrideMarginX = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideHMargin * width / 100.0);
                                var overrideMarginY = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin * width / 100.0);
                                if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
                                {
                                    var x = (int)Math.Round((width / 2.0) - mp.Bitmap.Width / 2.0);
                                    if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                                    {
                                        x = overrideMarginX;
                                    }
                                    else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                                    {
                                        x = width - overrideMarginX - mp.Bitmap.Width;
                                    }
                                    var y = height - overrideMarginY - mp.Bitmap.Height;
                                    mp.OverridePosition = new Point(x, y);
                                }
                                else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
                                {
                                    var x = (int)Math.Round((width / 2.0) - mp.Bitmap.Width / 2.0);
                                    if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
                                    {
                                        x = overrideMarginX;
                                    }
                                    else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
                                    {
                                        x = width - overrideMarginX - mp.Bitmap.Width;
                                    }
                                    mp.OverridePosition = new Point(x, pos.Top);
                                }
                                else
                                {
                                    var y = height - overrideMarginY - mp.Bitmap.Height;
                                    mp.OverridePosition = new Point(pos.Left, y);
                                }
                            }
                            else
                            {
                                mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position (can be scaled)
                            }
                            ExportPngXml.MakeBluRaySupImage(mp);
                            binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
                            if (mp.Bitmap != null)
                            {
                                mp.Bitmap.Dispose();
                                mp.Bitmap = null;
                            }
                        }
                    }
                }
            }
            return(true);
        }
コード例 #15
0
ファイル: TsToBdnXml.cs プロジェクト: wavous/subtitleedit
        public static bool ConvertFromTsToBdnXml(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback)
        {
            var tsParser = new TransportStreamParser();

            tsParser.Parse(fileName, (position, total) =>
            {
                var percent = (int)Math.Round(position * 100.0 / total);
                stdOutWriter?.Write("\rParsing transport stream: {0}%", percent);
                progressCallback?.Invoke($"{percent}%");
            });
            stdOutWriter?.Write("\r".PadRight(32, ' '));
            stdOutWriter?.Write("\r");
            var videoInfo = UiUtil.GetVideoInfo(fileName);
            int width     = 1920;
            int height    = 1080;

            if (videoInfo.Success && videoInfo.Width > 0 && videoInfo.Height > 0)
            {
                width  = videoInfo.Width;
                height = videoInfo.Height;
            }

            if (Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
                Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 &&
                Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0)
            {
                width  = Configuration.Settings.Tools.BatchConvertTsScreenWidth;
                height = Configuration.Settings.Tools.BatchConvertTsScreenHeight;
            }
            using (var form = new ExportPngXml())
            {
                if (tsParser.SubtitlePacketIds.Count == 0)
                {
                    stdOutWriter?.WriteLine($"No subtitles found");
                    progressCallback?.Invoke($"No subtitles found");
                    return(false);
                }
                foreach (int pid in tsParser.SubtitlePacketIds)
                {
                    var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + "-" + pid + Path.GetExtension(fileName), ".xml", outputFolder, overwrite);
                    stdOutWriter?.WriteLine($"Saving PID {pid} to {outputFileName}...");
                    progressCallback?.Invoke($"Save PID {pid}");
                    var sub      = tsParser.GetDvbSubtitles(pid);
                    var subtitle = new Subtitle();
                    foreach (var p in sub)
                    {
                        subtitle.Paragraphs.Add(new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds));
                    }
                    form.Initialize(subtitle, new SubRip(), BatchConvert.BdnXmlSubtitle, fileName, videoInfo, fileName);
                    var sb = new StringBuilder();
                    var imagesSavedCount = 0;
                    for (int index = 0; index < sub.Count; index++)
                    {
                        var p    = sub[index];
                        var pos  = p.GetPosition();
                        var bmp  = sub[index].GetBitmap();
                        var nbmp = new NikseBitmap(bmp);
                        pos.Top  += nbmp.CropTopTransparent(0);
                        pos.Left += nbmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
                        bmp.Dispose();
                        bmp = nbmp.GetBitmap();
                        var mp = form.MakeMakeBitmapParameter(index, width, height);
                        mp.Bitmap       = bmp;
                        mp.P            = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
                        mp.ScreenWidth  = width;
                        mp.ScreenHeight = height;
                        int bottomMarginInPixels;
                        if (Configuration.Settings.Tools.BatchConvertTsOverridePosition ||
                            Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
                            Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 &&
                            Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0)
                        {
                            mp.BottomMargin      = Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin;
                            bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin;
                            mp.Alignment         = ContentAlignment.BottomCenter;
                        }
                        else
                        {
                            mp.OverridePosition  = new Point(pos.Left, pos.Top); // use original position
                            bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
                        }
                        imagesSavedCount = form.WriteBdnXmlParagraph(width, sb, bottomMarginInPixels, height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
                        if (mp.Bitmap != null)
                        {
                            mp.Bitmap.Dispose();
                            mp.Bitmap = null;
                        }
                    }
                    form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
                }
            }
            return(true);
        }
コード例 #16
0
        private void PopulateTreeView(TransportStreamParser TsParser)
        {
            this.textBoxDesc.Text = "";
            this.listViewDetails.Items.Clear();
            this.treeViewMain.Nodes.Clear();
            foreach (KeyValuePair <int, List <DvbSubPes> > aPid in TsParser.SubtitlesLookup)
            {
                TreeNode pidNode = this.treeViewMain.Nodes.Add("PID " + aPid.Key.ToString());
                // would be nice to add descriptors
                //TODO add descriptors


                //do the PES
                foreach (DvbSubPes aDvbPes in aPid.Value)
                {
                    TreeNode pesNode = pidNode.Nodes.Add("PTS " + aDvbPes.PresentationTimestampToString() + " (" + aDvbPes.SubtitleSegments.Count.ToString() + " segments)");

                    // add the PES object, bitmap creates a huge memeory footprint and better to build bitmap on selection only.
                    pesNode.Tag = aDvbPes;

                    foreach (SubtitleSegment aSubtitleSegment in aDvbPes.SubtitleSegments)
                    {
                        TreeNode segmentNode = pesNode.Nodes.Add(aSubtitleSegment.SegmentTypeDescription);
                        segmentNode.Tag = aSubtitleSegment;

                        switch (aSubtitleSegment.SegmentType)
                        {
                        case SubtitleSegment.PageCompositionSegment:
                            segmentNode.Tag = aSubtitleSegment.PageComposition;
                            break;

                        case SubtitleSegment.RegionCompositionSegment:
                            segmentNode.Tag = aSubtitleSegment.RegionComposition;
                            break;

                        case SubtitleSegment.ClutDefinitionSegment:
                            segmentNode.Tag = aSubtitleSegment.ClutDefinition;
                            break;

                        case SubtitleSegment.ObjectDataSegment:
                            segmentNode.Tag = aSubtitleSegment.ObjectData;
                            break;

                        case SubtitleSegment.DisplayDefinitionSegment:
                            segmentNode.Tag = aSubtitleSegment.DisplayDefinition;
                            break;

                        case SubtitleSegment.EndOfDisplaySetSegment:
                            segmentNode.Tag = aSubtitleSegment.EndOfDisplaySet;
                            break;

                        default:
                            Console.WriteLine("Unknown Segment Type");
                            break;
                        }
                    }
                }
            }

            foreach (KeyValuePair <int, SortedDictionary <int, List <Paragraph> > > aPid in TsParser.TeletextSubtitlesLookup)
            {
                TreeNode pidNode = this.treeViewMain.Nodes.Add("PID " + aPid.Key.ToString());
                foreach (KeyValuePair <int, List <Paragraph> > item in aPid.Value)
                {
                    TreeNode pidTeletextPage = pidNode.Nodes.Add("Page " + item.Key.ToString());
                    foreach (Paragraph aParagraph in item.Value)
                    {
                        TreeNode nodeParagram = pidTeletextPage.Nodes.Add(aParagraph.StartTime.ToString() + " - " + aParagraph.EndTime.ToString());
                        nodeParagram.Tag = aParagraph;
                    }
                }
            }
        }