Ejemplo n.º 1
0
        /// <summary>
        /// Synchronize the first subtitle timing with second subtitle timing
        /// </summary>
        /// <param name="recipientNum">Represents the number of line in first subtitle file starting FROM which
        ///                         it will sync timing with second file</param>
        /// <param name="donorNum">Represents the number of line WITH which first subtitle file would sync</param>
        private void Synchronize(int recipientNum, int donorNum)
        {
            using (recipientStream)
            using (donorStream)
            {
                var recipientList = parser.ParseStream(recipientStream);
                var donorList = parser.ParseStream(donorStream);

                int[] recipientStartTimings = recipientList.Select(x => x.StartTime).ToArray();
                int[] recipientEndTimings = recipientList.Select(x => x.EndTime).ToArray();

                int[] donorStartTimings = donorList.Select(x => x.StartTime).ToArray();
                int[] donorEndTimings = donorList.Select(x => x.EndTime).ToArray();

                List<SubtitleFile> resultSubtitleList = new List<SubtitleFile>();

                try
                {
                    for (; recipientNum < recipientStartTimings.Length; recipientNum++, donorNum++)
                    {
                        recipientStartTimings[recipientNum] = donorStartTimings[donorNum];
                        recipientEndTimings[recipientNum] = donorEndTimings[donorNum];
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }

                for (int k = 0; k < recipientList.Count; k++)
                {
                    resultSubtitleList.Add(new SubtitleFile(recipientStartTimings[k], recipientEndTimings[k], recipientList[k].Lines));
                }

                string resultSubtitleString = string.Empty;
                foreach (var subtitleFile in resultSubtitleList)
                {
                    resultSubtitleString += subtitleFile;
                }

                recipientText.Text = resultSubtitleString;
            }
        }
Ejemplo n.º 2
0
        private async void LoadCC_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/secondCC.srt"));

            if (file == null)
            {
                return;
            }

            try
            {
                Stream stream = await file.OpenStreamForReadAsync();

                var parser   = new SubParser();
                var fileName = Path.GetFileName(file.Path);
                SubtitleEncoding Subencoding = SubtitleEncodingHelper.GetSubtitleEncoding(stream);
                Encoding         encoding    = new UTF8Encoding();
                if (Subencoding == SubtitleEncoding.ASCII)
                {
                    encoding = new ASCIIEncoding();
                }
                else if (Subencoding == SubtitleEncoding.Unicode)
                {
                    encoding = new UnicodeEncoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF8)
                {
                    encoding = new UTF8Encoding();
                }
                else if (Subencoding == SubtitleEncoding.Windows1256)
                {
                    encoding = new CustomCP1256Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF7)
                {
                    encoding = new UTF7Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF32)
                {
                    encoding = Encoding.UTF32;
                }
                else if (Subencoding == SubtitleEncoding.BigEndianUnicode)
                {
                    encoding = Encoding.BigEndianUnicode;
                }
                var mostLikelyFormat = parser.GetMostLikelyFormat(fileName, stream, encoding);

                Debug.WriteLine("mostLikelyFormat: " + mostLikelyFormat.Name);
                SubtitleList = parser.ParseStream(await file.OpenStreamForReadAsync(), encoding, mostLikelyFormat);
                dt.Start();
            }
            catch (Exception ex) { }
        }
Ejemplo n.º 3
0
        public IEnumerable <SubtitleItem> ParseSubtitleStream(Stream stream)
        {
            var parser = new SubParser();
            var items  = parser.ParseStream(stream, Encoding.UTF8);

            return(items.Select(s => new SubtitleItem()
            {
                StartTime = s.StartTime,
                EndTime = s.EndTime,
                Lines = s.Lines.ToList()
            }).ToList());
        }
Ejemplo n.º 4
0
 public void Parse_successfully()
 {
     foreach (var filePath in TestFiles.ValidFiles)
     {
         var fileName = Path.GetFileName(filePath);
         using (var fileStream = File.OpenRead(filePath))
         {
             var mostLikelyFormat = parser.GetMostLikelyFormat(fileName);
             _ = parser.ParseStream(fileStream, Encoding.UTF8, mostLikelyFormat);
         }
     }
 }
 public void Throw_on_parse()
 {
     foreach (var filePath in TestFiles.InvalidFiles)
     {
         var fileName = Path.GetFileName(filePath);
         using (var fileStream = File.OpenRead(filePath))
         {
             var mostLikelyFormat = parser.GetMostLikelyFormat(fileName);
             Assert.Throws <FormatException>(() => parser.ParseStream(fileStream, Encoding.UTF8, mostLikelyFormat));
         }
     }
 }
Ejemplo n.º 6
0
        async public void ImportSubtitle(StorageFile file)
        {
            try
            {
                if (file == null)
                {
                    return;
                }
                Stream stream = await file.OpenStreamForReadAsync();


                var parser   = new SubParser();
                var fileName = Path.GetFileName(file.Path);
                SubtitleEncoding Subencoding = HelperUP.GetSubtitleEncoding(stream);
                Encoding         encoding    = new UTF8Encoding();
                if (Subencoding == SubtitleEncoding.ASCII)
                {
                    encoding = new ASCIIEncoding();
                }
                else if (Subencoding == SubtitleEncoding.Unicode)
                {
                    encoding = new UnicodeEncoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF8)
                {
                    encoding = new UTF8Encoding();
                }
                else if (Subencoding == SubtitleEncoding.Windows1256)
                {
                    encoding = new CustomCP1256Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF7)
                {
                    encoding = new UTF7Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF32)
                {
                    encoding = Encoding.UTF32;
                }
                else if (Subencoding == SubtitleEncoding.BigEndianUnicode)
                {
                    encoding = Encoding.BigEndianUnicode;
                }
                var mostLikelyFormat = parser.GetMostLikelyFormat(fileName, stream, encoding);

                HelperUP.Output("mostLikelyFormat: " + mostLikelyFormat.Name);
                SubtitleList = parser.ParseStream(await file.OpenStreamForReadAsync(), encoding, mostLikelyFormat);
                dt.Start();
            }
            catch (Exception ex) { HelperUP.Output("ImportSubtitle ex: " + ex.Message); }
        }
Ejemplo n.º 7
0
        private ISubtitle GetSubtitleFromStream(string filePath, Stream stream)
        {
            Subtitle subtitle;
            var      parser = new SubParser();

            try
            {
                var items = parser.ParseStream(stream);
                subtitle = new Subtitle(filePath, items);
            }
            catch (ArgumentException e)
            {
                throw new FormatException(e.Message, e);
            }

            return(subtitle);
        }
Ejemplo n.º 8
0
        private static NSArray ParseSubtitles(NSUrl url)
        {
            var subtitles = new NSMutableArray();

            try {
                NSData data   = NSData.FromUrl(url);
                var    parser = new SubParser();
                byte[] bytes  = data.ToArray();
                using (MemoryStream stream = new MemoryStream(bytes)) {
                    foreach (var item in parser.ParseStream(stream))
                    {
                        var subtitle = new NSMutableDictionary();
                        subtitle.Add(
                            new NSString("from"),
                            NSNumber.FromFloat(item.StartTime / 1000.0f)
                            );
                        subtitle.Add(
                            new NSString("to"),
                            NSNumber.FromFloat(item.EndTime / 1000.0f)
                            );

                        string content = string.Empty;
                        item.Lines.ForEach(l => content += (l + "\n"));
                        content = content.Trim('\n');
                        subtitle.Add(
                            new NSString("text"),
                            new NSString(content)
                            );

                        subtitles.Add(
                            subtitle
                            );
                    }
                }

                return(subtitles);
            }
            catch (Exception e) {
                Console.WriteLine("Error parsing subtitles: " + e.Message);
                return(new NSArray());
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("-------Welcome To Batch Subtitle Editor-------");
            Console.WriteLine("\n" +
                              "Enter the path to the folder containing your subtitles." +
                              "\n" +
                              "The program will automatically search for .srt files. (Also scanning for subdirectories)");
            var path = Console.ReadLine();

            Console.WriteLine("Enter the timeshift you want to apply: (in milliseconds) ");
            var timeshiftString = Console.ReadLine();
            int timeShift       = 0;

            try
            {
                timeShift = Convert.ToInt32(timeshiftString);
            } catch
            {
                Console.WriteLine("Please enter a number.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            var files = Directory.GetFiles(path, "*.srt", SearchOption.AllDirectories);

            foreach (var filePath in files)
            {
                var parser = new SubParser();
                List <SubtitleItem> itemsShifted  = null;
                List <SubtitleItem> itemsOriginal = new List <SubtitleItem>();
                using (var fileStream = File.OpenRead(filePath))
                {
                    try
                    {
                        itemsShifted = parser.ParseStream(fileStream);

                        //Copy to original list without copying the reference
                        foreach (var _ in itemsShifted)
                        {
                            itemsOriginal.Add(new SubtitleItem()
                            {
                                EndTime   = _.EndTime,
                                StartTime = _.StartTime,
                                Lines     = _.Lines
                            });
                        }


                        Console.WriteLine($"Shifting Subtitle: {Path.GetFileName(filePath)} by {timeShift} milliseconds.");
                        foreach (var item in itemsShifted)
                        {
                            item.StartTime += timeShift;
                            item.EndTime   += timeShift;
                        }
                    }
                    catch
                    {
                        Console.WriteLine($"Not a valid subtitle format: {filePath}");
                    }
                }
                parser = null;

                var res = SubtitleEditor.SaveItemsToFile(itemsShifted, itemsOriginal, filePath);
                if (!res)
                {
                    Console.WriteLine("Error writing to subtitle.");
                }
                else
                {
                    Console.WriteLine("Subtitle successfully edited!");
                }
            }
        }